]> 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\Utils\String\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 (string $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          * Count the criteria, e.g. useful to find out if a database query has no
58          * limitation (search criteria).
59          *
60          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
61          * @return      $count  Count of all criteria entries
62          */
63         protected final function count (string $criteriaType = 'default') {
64                 // Return it
65                 return $this->countGenericArrayGroup('criteria', $criteriaType);
66         }
67
68         /**
69          * Checks whether given key is set
70          *
71          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
72          * @param       $criteriaKey    Criteria key
73          * @return      $isSet                  Whether key is set
74          */
75         public function isKeySet (string $criteriaType, string $criteriaKey) {
76                 // Make sure no 'my-' or 'my_' passes this point
77                 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false));
78
79                 // Determine it
80                 $isSet = $this->isGenericArrayElementSet('criteria', $criteriaType, 'entries', $criteriaKey);
81
82                 // Return it
83                 return $isSet;
84         }
85
86         /**
87          * Checks whether given key is set for 'choice' type
88          *
89          * @param       $criteriaKey    Criteria key
90          * @return      $isSet                  Whether key is set
91          */
92         public function isChoiceKeySet (string $criteriaKey) {
93                 // Call inner method
94                 return $this->isKeySet('choice', $criteriaKey);
95         }
96
97         /**
98          * Checks whether given key is set for 'exclude' type
99          *
100          * @param       $criteriaKey    Criteria key
101          * @return      $isSet                  Whether key is set
102          */
103         public function isExcludeKeySet (string $criteriaKey) {
104                 // Call inner method
105                 return $this->isKeySet('exclude', $criteriaKey);
106         }
107
108         /**
109          * Setter for wrapper class name
110          *
111          * @param       $wrapperConfigEntry             Configuration entry which hold the wrapper class' name
112          * @return      void
113          */
114         public final function setWrapperConfigEntry (string $wrapperConfigEntry) {
115                 $this->wrapperConfigEntry = $wrapperConfigEntry;
116         }
117
118         /**
119          * Getter for wrapper class name
120          *
121          * @return      $wrapperConfigEntry             Configuration entry which hold the wrapper class' name
122          */
123         public final function getWrapperConfigEntry () {
124                 return $this->wrapperConfigEntry;
125         }
126
127         /**
128          * Getter for criteria array
129          *
130          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
131          * @return      $criteria
132          */
133         public final function getCriteriaArray (string $criteriaType = 'default') {
134                 return $this->getGenericArrayKey('criteria', $criteriaType, 'entries');
135         }
136
137         /**
138          * Getter for criteria array 'choice' type
139          *
140          * @return      $criteria
141          */
142         public final function getCriteriaChoiceArray () {
143                 return $this->getCriteriaArray('choice');
144         }
145
146         /**
147          * Getter for criteria array 'exclude' type
148          *
149          * @return      $criteria
150          */
151         public final function getCriteriaExcludeArray () {
152                 return $this->getCriteriaArray('exclude');
153         }
154
155         /**
156          * Unsets a criteria key from all criteria types
157          *
158          * @param       $criteriaKey    Criteria key to unset
159          * @return      void
160          */
161         public final function unsetCriteria (string $criteriaKey) {
162                 // Make sure no 'my-' or 'my_' passes this point
163                 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false));
164
165                 // Convert dashes to underscore
166                 $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey);
167
168                 // "Walk" through all criterias
169                 foreach ($this->getGenericArray('criteria') as $criteriaType => $dummy) {
170                         // Remove it
171                         $this->unsetGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey);
172                 } // END - foreach
173         }
174
175         /**
176          * Add criteria, this method converts dashes to underscores because dashes
177          * are not valid for criteria keys.
178          *
179          * @param       $criteriaKey    Criteria key
180          * @param       $criteriaValue  Criteria value
181          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
182          * @return      void
183          */
184         public final function addCriteria (string $criteriaKey, $criteriaValue, string $criteriaType = 'default') {
185                 // Debug message
186                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue . ',criteriaType=' . $criteriaType . ' - CALLED!');
187
188                 // Make sure no 'my-' or 'my_' passes this point
189                 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
190
191                 // Convert dashes to underscore
192                 $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey);
193
194                 // Debug message
195                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA: criteriaKey=' . $criteriaKey);
196
197                 // Append it
198                 $this->appendStringToGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey, $criteriaValue);
199         }
200
201         /**
202          * Set criteria, this method converts dashes to underscores because dashes
203          * are not valid for criteria keys.
204          *
205          * @param       $criteriaKey    Criteria key
206          * @param       $criteriaValue  Criteria value
207          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
208          * @return      void
209          */
210         public final function setCriteria (string $criteriaKey, $criteriaValue, string $criteriaType = 'default') {
211                 // Debug message
212                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue . ',criteriaType=' . $criteriaType . ' - CALLED!');
213
214                 // Make sure no 'my-' or 'my_' passes this point
215                 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
216
217                 // Convert dashes to underscore
218                 $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey);
219
220                 // Debug message
221                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA: criteriaKey=' . $criteriaKey);
222
223                 // Set it
224                 $this->setStringGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey, $criteriaValue);
225         }
226
227         /**
228          * Add "choice" criteria, this method converts dashes to underscores because
229          * dashes are not valid for criteria keys.
230          *
231          * @param       $criteriaKey    Criteria key
232          * @param       $criteriaValue  Criteria value
233          * @return      void
234          */
235         public final function addChoiceCriteria (string $criteriaKey, $criteriaValue) {
236                 // Make sure no 'my-' or 'my_' passes this point
237                 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
238
239                 // Debug message
240                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
241
242                 // Add it
243                 $this->pushValueToGenericArrayElement('criteria', 'choice', 'entries', StringUtils::convertDashesToUnderscores($criteriaKey), (string) $criteriaValue);
244         }
245
246         /**
247          * Add "exclude" criteria, this method converts dashes to underscores because
248          * dashes are not valid for criteria keys.
249          *
250          * @param       $criteriaKey    Criteria key
251          * @param       $criteriaValue  Criteria value
252          * @return      void
253          */
254         public final function addExcludeCriteria (string $criteriaKey, $criteriaValue) {
255                 // Add it with generic method
256                 $this->addCriteria($criteriaKey, $criteriaValue, 'exclude');
257         }
258
259         /**
260          * Add configured criteria
261          *
262          * @param       $criteriaKey    Criteria key
263          * @param       $configEntry    Configuration entry
264          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
265          * @return      void
266          */
267         public final function addConfiguredCriteria ($criteriaKey, $configEntry, string $criteriaType = 'default') {
268                 // Add the configuration entry as a criteria
269                 $value = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configEntry);
270                 $this->addCriteria($criteriaKey, $value, $criteriaType);
271         }
272
273         /**
274          * Get criteria element or false if not found
275          *
276          * @param       $criteriaKey    The requested criteria key
277          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
278          * @return      $value                  Whether the value of the critera or false
279          */
280         public function getCriteriaElemnent ($criteriaKey, string $criteriaType = 'default') {
281                 // Debug message
282                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaType=' . $criteriaType . ' - CALLED!');
283
284                 // Make sure no 'my-' or 'my_' passes this point
285                 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false));
286
287                 // Convert dashes to underscore
288                 $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey);
289
290                 // Debug message
291                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: criteriaKey=' . $criteriaKey . ',criteria()=' . $this->countGenericArrayGroup('criteria', $criteriaType));
292
293                 // Default is not found
294                 $value = false;
295
296                 // Is the criteria there?
297                 if ($this->isKeySet($criteriaType, $criteriaKey)) {
298                         // Then use it
299                         $value = $this->getGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey);
300                 } // END - if
301
302                 // Debug message
303                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: value=' . $value . ' - EXIT!');
304
305                 // Return the value
306                 return $value;
307         }
308
309         /**
310          * Get criteria element or false if not found for 'choice' type
311          *
312          * @param       $criteriaKey    The requested criteria key
313          * @return      $value                  Whether the value of the critera or false
314          */
315         public function getCriteriaChoiceElemnent (string $criteriaKey) {
316                 // Call inner method
317                 return $this->getCriteriaElemnent($criteriaKey, 'choice');
318         }
319
320         /**
321          * Get criteria element or false if not found for 'exclude' type
322          *
323          * @param       $criteriaKey    The requested criteria key
324          * @return      $value                  Whether the value of the critera or false
325          */
326         public function getCriteriaExcludeElemnent (string $criteriaKey) {
327                 // Call inner method
328                 return $this->getCriteriaElemnent($criteriaKey, 'exclude');
329         }
330
331         /**
332          * Checks whether given array entry matches
333          *
334          * @param       $entryArray             Array with the entries to find
335          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
336          * @return      $matches                Whether the entry matches or not
337          */
338         public function ifEntryMatches (array $entryArray, string $criteriaType = 'default') {
339                 // First nothing matches and nothing is counted
340                 $matches = false;
341                 $counted = 0;
342
343                 // Walk through all entries
344                 foreach ($entryArray as $key => $entry) {
345                         // Make sure no 'my-' or 'my_' passes this point
346                         assert((strpos($key, 'my-') === false) && (strpos($key, 'my_') === false));
347
348                         // Convert dashes to underscore
349                         $key = StringUtils::convertDashesToUnderscores($key);
350
351                         // Then walk through all search criteria
352                         foreach ($this->getGenericArrayKey('criteria', $criteriaType, 'entries') as $criteriaKey => $criteriaValue) {
353                                 // Make sure no 'my-' or 'my_' passes this point
354                                 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
355
356                                 // Convert dashes to underscore
357                                 $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey);
358
359                                 // Is the element found and does it match?
360                                 if (($key == $criteriaKey) && ($criteriaValue == $entry)) {
361                                         // Then count this one up
362                                         $counted++;
363                                 } // END - if
364                         } // END - foreach
365                 } // END - foreach
366
367                 // Now check if expected criteria counts match
368                 $matches = ($counted == $this->countGenericArrayGroup('criteria', $criteriaType));
369
370                 // Return the result
371                 return $matches;
372         }
373
374         /**
375          * Checks whether given array 'choice' entry matches
376          *
377          * @param       $entryArray             Array with the entries to find
378          * @return      $matches                Whether the entry matches or not
379          */
380         public function ifChoiceMatches (array $entryArray) {
381                 // Call inner method
382                 return $this->ifEntryMatches($entryArray, 'choice');
383         }
384
385         /**
386          * Checks whether given array 'exclude' entry matches
387          *
388          * @param       $entryArray             Array with the entries to find
389          * @return      $matches                Whether the entry matches or not
390          */
391         public function ifExcludeMatches (array $entryArray) {
392                 // Call inner method
393                 return $this->ifEntryMatches($entryArray, 'exclude');
394         }
395
396         /**
397          * "Getter" for a cache key
398          *
399          * @param       $onlyKeys       Only use these keys for a cache key
400          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
401          * @return      $cacheKey       The key suitable for the cache system
402          */
403         public function getCacheKey (array $onlyKeys = [], string $criteriaType = 'default') {
404                 // Debug message
405                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($this->__toString() . ': criteriaType=' . $criteriaType . ',count()=' . $this->countGenericArray('criteria')));
406
407                 // Make sure the criteria is there
408                 assert($this->isValidGenericArrayGroup('criteria', $criteriaType));
409
410                 // Initialize the key
411                 $cacheKey = '';
412
413                 // Now walk through all criterias
414                 foreach ($this->getGenericArrayKey('criteria', $criteriaType, 'entries') as $criteriaKey => $criteriaValue) {
415                         // Make sure no 'my-' or 'my_' passes this point
416                         assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
417
418                         // $criteriaValue cannot be an array
419                         assert(!is_array($criteriaValue));
420
421                         // Convert dashes to underscore
422                         $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey);
423
424                         // Is the value in array or is $onlyKeys empty?
425                         if ((isset($onlyKeys[$criteriaKey])) || (count($onlyKeys) == 0)) {
426                                 // Add the value URL encoded to avoid any trouble with special characters
427                                 $cacheKey .= sprintf('%s=%s;',
428                                         $criteriaKey,
429                                         urlencode($criteriaValue)
430                                 );
431                         } // END - if
432                 } // END - foreach
433
434                 // Remove last semicolon
435                 $cacheKey = substr($cacheKey, 0, -1);
436
437                 // Is the instance SearchCriteria?
438                 if ($this instanceof SearchCriteria) {
439                         // Check if 'limit' and 'skip' are in
440                         if (((isset($onlyKeys['limit'])) && (isset($onlyKeys['skip']))) || (count($onlyKeys) == 0)) {
441                                 // Add limit and skip values
442                                 $cacheKey .= sprintf(';%%limit%%=%s;%%skip%%=%s',
443                                         $this->getLimit(),
444                                         $this->getSkip()
445                                 );
446                         } // END - if
447                 } // END - if
448
449                 // Return the cache key
450                 return $cacheKey;
451         }
452
453         /**
454          * "Getter" for a cache key ('choice' type)
455          *
456          * @param       $onlyKeys       Only use these keys for a cache key
457          * @return      $cacheKey       The key suitable for the cache system
458          */
459         public function getCacheKeyChoice (array $onlyKeys = []) {
460                 // Call inner method
461                 return $this->getCacheKey($onlyKeys, 'choice');
462         }
463
464         /**
465          * "Getter" for a cache key ('exclude' type)
466          *
467          * @param       $onlyKeys       Only use these keys for a cache key
468          * @return      $cacheKey       The key suitable for the cache system
469          */
470         public function getCacheKeyExclude (array $onlyKeys = []) {
471                 // Call inner method
472                 return $this->getCacheKey($onlyKeys, 'exclude');
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 }