]> git.mxchange.org Git - core.git/blob - framework/main/classes/criteria/class_BaseCriteria.php
Continued:
[core.git] / framework / main / classes / criteria / class_BaseCriteria.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Criteria;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Criteria\Search\SearchCriteria;
8 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
9 use Org\Mxchange\CoreFramework\String\Utils\StringUtils;
10
11 /**
12  * A general crtieria class
13  *
14  * @author              Roland Haeder <webmaster@shipsimu.org>
15  * @version             0.0.0
16  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
17  * @license             GNU GPL 3.0 or any newer version
18  * @link                http://www.shipsimu.org
19  *
20  * This program is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation, either version 3 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32  */
33 abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria {
34         /**
35          * Wrapper class name stored in config entry
36          */
37         private $wrapperConfigEntry = '';
38
39         /**
40          * Protected constructor
41          *
42          * @param       $className      Name of the class
43          * @return      void
44          */
45         protected function __construct ($className) {
46                 // Call parent constructor
47                 parent::__construct($className);
48
49                 // Initialize all criteria arrays
50                 foreach (array('default', 'choice', 'exclude') as $criteriaType) {
51                         // Init it
52                         $this->initGenericArrayKey('criteria', $criteriaType, 'entries');
53                 } // END - foreach
54         }
55
56         /**
57          * Checks whether given key is set
58          *
59          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
60          * @param       $criteriaKey    Criteria key
61          * @return      $isSet                  Whether key is set
62          */
63         public function isKeySet ($criteriaType, $criteriaKey) {
64                 // Make sure no 'my-' or 'my_' passes this point
65                 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false));
66
67                 // Determine it
68                 $isSet = $this->isGenericArrayElementSet('criteria', $criteriaType, 'entries', $criteriaKey);
69
70                 // Return it
71                 return $isSet;
72         }
73
74         /**
75          * Checks whether given key is set for 'choice' type
76          *
77          * @param       $criteriaKey    Criteria key
78          * @return      $isSet                  Whether key is set
79          */
80         public function isChoiceKeySet ($criteriaKey) {
81                 // Call inner method
82                 return $this->isKeySet('choice', $criteriaKey);
83         }
84
85         /**
86          * Checks whether given key is set for 'exclude' type
87          *
88          * @param       $criteriaKey    Criteria key
89          * @return      $isSet                  Whether key is set
90          */
91         public function isExcludeKeySet ($criteriaKey) {
92                 // Call inner method
93                 return $this->isKeySet('exclude', $criteriaKey);
94         }
95
96         /**
97          * Setter for wrapper class name
98          *
99          * @param       $wrapperConfigEntry             Configuration entry which hold the wrapper class' name
100          * @return      void
101          */
102         public final function setWrapperConfigEntry ($wrapperConfigEntry) {
103                 $this->wrapperConfigEntry = (string) $wrapperConfigEntry;
104         }
105
106         /**
107          * Getter for wrapper class name
108          *
109          * @return      $wrapperConfigEntry             Configuration entry which hold the wrapper class' name
110          */
111         public final function getWrapperConfigEntry () {
112                 return $this->wrapperConfigEntry;
113         }
114
115         /**
116          * Getter for criteria array
117          *
118          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
119          * @return      $criteria
120          */
121         public final function getCriteriaArray ($criteriaType = 'default') {
122                 return $this->getGenericArrayKey('criteria', $criteriaType, 'entries');
123         }
124
125         /**
126          * Getter for criteria array 'choice' type
127          *
128          * @return      $criteria
129          */
130         public final function getCriteriaChoiceArray () {
131                 return $this->getCriteriaArray('choice');
132         }
133
134         /**
135          * Getter for criteria array 'exclude' type
136          *
137          * @return      $criteria
138          */
139         public final function getCriteriaExcludeArray () {
140                 return $this->getCriteriaArray('exclude');
141         }
142
143         /**
144          * Unsets a criteria key from all criteria types
145          *
146          * @param       $criteriaKey    Criteria key to unset
147          * @return      void
148          */
149         public final function unsetCriteria ($criteriaKey) {
150                 // Make sure no 'my-' or 'my_' passes this point
151                 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false));
152
153                 // Convert dashes to underscore
154                 $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey);
155
156                 // "Walk" through all criterias
157                 foreach ($this->getGenericArray('criteria') as $criteriaType => $dummy) {
158                         // Remove it
159                         $this->unsetGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey);
160                 } // END - foreach
161         }
162
163         /**
164          * Add criteria, this method converts dashes to underscores because dashes
165          * are not valid for criteria keys.
166          *
167          * @param       $criteriaKey    Criteria key
168          * @param       $criteriaValue  Criteria value
169          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
170          * @return      void
171          */
172         public final function addCriteria ($criteriaKey, $criteriaValue, $criteriaType = 'default') {
173                 // Debug message
174                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue . ',criteriaType=' . $criteriaType . ' - CALLED!');
175
176                 // Make sure no 'my-' or 'my_' passes this point
177                 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
178
179                 // Convert dashes to underscore
180                 $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey);
181
182                 // Debug message
183                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA: criteriaKey=' . $criteriaKey);
184
185                 // Append it
186                 $this->appendStringToGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey, $criteriaValue);
187         }
188
189         /**
190          * Set criteria, this method converts dashes to underscores because dashes
191          * are not valid for criteria keys.
192          *
193          * @param       $criteriaKey    Criteria key
194          * @param       $criteriaValue  Criteria value
195          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
196          * @return      void
197          */
198         public final function setCriteria ($criteriaKey, $criteriaValue, $criteriaType = 'default') {
199                 // Debug message
200                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue . ',criteriaType=' . $criteriaType . ' - CALLED!');
201
202                 // Make sure no 'my-' or 'my_' passes this point
203                 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
204
205                 // Convert dashes to underscore
206                 $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey);
207
208                 // Debug message
209                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA: criteriaKey=' . $criteriaKey);
210
211                 // Set it
212                 $this->setStringGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey, $criteriaValue);
213         }
214
215         /**
216          * Add "choice" criteria, this method converts dashes to underscores because
217          * dashes are not valid for criteria keys.
218          *
219          * @param       $criteriaKey    Criteria key
220          * @param       $criteriaValue  Criteria value
221          * @return      void
222          */
223         public final function addChoiceCriteria ($criteriaKey, $criteriaValue) {
224                 // Make sure no 'my-' or 'my_' passes this point
225                 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
226
227                 // Debug message
228                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
229
230                 // Add it
231                 $this->pushValueToGenericArrayElement('criteria', 'choice', 'entries', StringUtils::convertDashesToUnderscores($criteriaKey), (string) $criteriaValue);
232         }
233
234         /**
235          * Add "exclude" criteria, this method converts dashes to underscores because
236          * dashes are not valid for criteria keys.
237          *
238          * @param       $criteriaKey    Criteria key
239          * @param       $criteriaValue  Criteria value
240          * @return      void
241          */
242         public final function addExcludeCriteria ($criteriaKey, $criteriaValue) {
243                 // Add it with generic method
244                 $this->addCriteria($criteriaKey, $criteriaValue, 'exclude');
245         }
246
247         /**
248          * Add configured criteria
249          *
250          * @param       $criteriaKey    Criteria key
251          * @param       $configEntry    Configuration entry
252          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
253          * @return      void
254          */
255         public final function addConfiguredCriteria ($criteriaKey, $configEntry, $criteriaType = 'default') {
256                 // Add the configuration entry as a criteria
257                 $value = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configEntry);
258                 $this->addCriteria($criteriaKey, $value, $criteriaType);
259         }
260
261         /**
262          * Get criteria element or false if not found
263          *
264          * @param       $criteriaKey    The requested criteria key
265          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
266          * @return      $value                  Whether the value of the critera or false
267          */
268         public function getCriteriaElemnent ($criteriaKey, $criteriaType = 'default') {
269                 // Debug message
270                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaType=' . $criteriaType . ' - CALLED!');
271
272                 // Make sure no 'my-' or 'my_' passes this point
273                 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false));
274
275                 // Convert dashes to underscore
276                 $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey);
277
278                 // Debug message
279                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: criteriaKey=' . $criteriaKey . ',criteria()=' . $this->countGenericArrayGroup('criteria', $criteriaType));
280
281                 // Default is not found
282                 $value = false;
283
284                 // Is the criteria there?
285                 if ($this->isKeySet($criteriaType, $criteriaKey)) {
286                         // Then use it
287                         $value = $this->getGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey);
288                 } // END - if
289
290                 // Debug message
291                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: value=' . $value . ' - EXIT!');
292
293                 // Return the value
294                 return $value;
295         }
296
297         /**
298          * Get criteria element or false if not found for 'choice' type
299          *
300          * @param       $criteriaKey    The requested criteria key
301          * @return      $value                  Whether the value of the critera or false
302          */
303         public function getCriteriaChoiceElemnent ($criteriaKey) {
304                 // Call inner method
305                 return $this->getCriteriaElemnent($criteriaKey, 'choice');
306         }
307
308         /**
309          * Get criteria element or false if not found for 'exclude' type
310          *
311          * @param       $criteriaKey    The requested criteria key
312          * @return      $value                  Whether the value of the critera or false
313          */
314         public function getCriteriaExcludeElemnent ($criteriaKey) {
315                 // Call inner method
316                 return $this->getCriteriaElemnent($criteriaKey, 'exclude');
317         }
318
319         /**
320          * Checks whether given array entry matches
321          *
322          * @param       $entryArray             Array with the entries to find
323          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
324          * @return      $matches                Whether the entry matches or not
325          */
326         public function ifEntryMatches (array $entryArray, $criteriaType = 'default') {
327                 // First nothing matches and nothing is counted
328                 $matches = false;
329                 $counted = 0;
330
331                 // Walk through all entries
332                 foreach ($entryArray as $key => $entry) {
333                         // Make sure no 'my-' or 'my_' passes this point
334                         assert((strpos($key, 'my-') === false) && (strpos($key, 'my_') === false));
335
336                         // Convert dashes to underscore
337                         $key = StringUtils::convertDashesToUnderscores($key);
338
339                         // Then walk through all search criteria
340                         foreach ($this->getGenericArrayKey('criteria', $criteriaType, 'entries') as $criteriaKey => $criteriaValue) {
341                                 // Make sure no 'my-' or 'my_' passes this point
342                                 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
343
344                                 // Convert dashes to underscore
345                                 $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey);
346
347                                 // Is the element found and does it match?
348                                 if (($key == $criteriaKey) && ($criteriaValue == $entry)) {
349                                         // Then count this one up
350                                         $counted++;
351                                 } // END - if
352                         } // END - foreach
353                 } // END - foreach
354
355                 // Now check if expected criteria counts match
356                 $matches = ($counted == $this->countGenericArrayGroup('criteria', $criteriaType));
357
358                 // Return the result
359                 return $matches;
360         }
361
362         /**
363          * Checks whether given array 'choice' entry matches
364          *
365          * @param       $entryArray             Array with the entries to find
366          * @return      $matches                Whether the entry matches or not
367          */
368         public function ifChoiceMatches (array $entryArray) {
369                 // Call inner method
370                 return $this->ifEntryMatches($entryArray, 'choice');
371         }
372
373         /**
374          * Checks whether given array 'exclude' entry matches
375          *
376          * @param       $entryArray             Array with the entries to find
377          * @return      $matches                Whether the entry matches or not
378          */
379         public function ifExcludeMatches (array $entryArray) {
380                 // Call inner method
381                 return $this->ifEntryMatches($entryArray, 'exclude');
382         }
383
384         /**
385          * "Getter" for a cache key
386          *
387          * @param       $onlyKeys       Only use these keys for a cache key
388          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
389          * @return      $cacheKey       The key suitable for the cache system
390          */
391         public function getCacheKey ($onlyKeys = array(), $criteriaType = 'default') {
392                 // Debug message
393                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($this->__toString() . ': criteriaType=' . $criteriaType . ',count()=' . $this->countGenericArray('criteria')));
394
395                 // Make sure the criteria is there
396                 assert($this->isValidGenericArrayGroup('criteria', $criteriaType));
397
398                 // Initialize the key
399                 $cacheKey = '';
400
401                 // Now walk through all criterias
402                 foreach ($this->getGenericArrayKey('criteria', $criteriaType, 'entries') as $criteriaKey => $criteriaValue) {
403                         // Make sure no 'my-' or 'my_' passes this point
404                         assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
405
406                         // $criteriaValue cannot be an array
407                         assert(!is_array($criteriaValue));
408
409                         // Convert dashes to underscore
410                         $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey);
411
412                         // Is the value in array or is $onlyKeys empty?
413                         if ((isset($onlyKeys[$criteriaKey])) || (count($onlyKeys) == 0)) {
414                                 // Add the value URL encoded to avoid any trouble with special characters
415                                 $cacheKey .= sprintf('%s=%s;',
416                                         $criteriaKey,
417                                         urlencode($criteriaValue)
418                                 );
419                         } // END - if
420                 } // END - foreach
421
422                 // Remove last semicolon
423                 $cacheKey = substr($cacheKey, 0, -1);
424
425                 // Is the instance SearchCriteria?
426                 if ($this instanceof SearchCriteria) {
427                         // Check if 'limit' and 'skip' are in
428                         if (((isset($onlyKeys['limit'])) && (isset($onlyKeys['skip']))) || (count($onlyKeys) == 0)) {
429                                 // Add limit and skip values
430                                 $cacheKey .= sprintf(';%%limit%%=%s;%%skip%%=%s',
431                                         $this->getLimit(),
432                                         $this->getSkip()
433                                 );
434                         } // END - if
435                 } // END - if
436
437                 // Return the cache key
438                 return $cacheKey;
439         }
440
441         /**
442          * "Getter" for a cache key ('choice' type)
443          *
444          * @param       $onlyKeys       Only use these keys for a cache key
445          * @return      $cacheKey       The key suitable for the cache system
446          */
447         public function getCacheKeyChoice ($onlyKeys = array()) {
448                 // Call inner method
449                 return $this->getCacheKey($onlyKeys, 'choice');
450         }
451
452         /**
453          * "Getter" for a cache key ('exclude' type)
454          *
455          * @param       $onlyKeys       Only use these keys for a cache key
456          * @return      $cacheKey       The key suitable for the cache system
457          */
458         public function getCacheKeyExclude ($onlyKeys = array()) {
459                 // Call inner method
460                 return $this->getCacheKey($onlyKeys, 'exclude');
461         }
462
463         /**
464          * Count the criteria, e.g. useful to find out if a database query has no
465          * limitation (search criteria).
466          *
467          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
468          * @return      $count  Count of all criteria entries
469          */
470         public final function count ($criteriaType = 'default') {
471                 // Return it
472                 return $this->countGenericArrayGroup('criteria', $criteriaType);
473         }
474
475         /**
476          * Count 'choice' criteria, e.g. useful to find out if a database query
477          * has no limitation (search criteria).
478          *
479          * @return      $count  Count of all criteria entries
480          */
481         public final function countChoice () {
482                 return $this->count('choice');
483         }
484
485         /**
486          * Count 'exclude' criteria, e.g. useful to find out if a database query
487          * has no limitation (search criteria).
488          *
489          * @return      $count  Count of all criteria entries
490          */
491         public final function countExclude () {
492                 return $this->count('exclude');
493         }
494
495 }