3 namespace Org\Mxchange\CoreFramework\Criteria;
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;
12 * A general crtieria class
14 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
33 abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria {
35 * Wrapper class name stored in config entry
37 private $wrapperConfigEntry = '';
40 * Protected constructor
42 * @param $className Name of the class
45 protected function __construct (string $className) {
46 // Call parent constructor
47 parent::__construct($className);
49 // Initialize all criteria arrays
50 foreach (array('default', 'choice', 'exclude') as $criteriaType) {
52 $this->initGenericArrayKey('criteria', $criteriaType, 'entries');
57 * Count the criteria, e.g. useful to find out if a database query has no
58 * limitation (search criteria).
60 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
61 * @return $count Count of all criteria entries
63 protected final function count (string $criteriaType = 'default') {
65 return $this->countGenericArrayGroup('criteria', $criteriaType);
69 * Checks whether given key is set
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
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));
80 $isSet = $this->isGenericArrayElementSet('criteria', $criteriaType, 'entries', $criteriaKey);
87 * Checks whether given key is set for 'choice' type
89 * @param $criteriaKey Criteria key
90 * @return $isSet Whether key is set
92 public function isChoiceKeySet (string $criteriaKey) {
94 return $this->isKeySet('choice', $criteriaKey);
98 * Checks whether given key is set for 'exclude' type
100 * @param $criteriaKey Criteria key
101 * @return $isSet Whether key is set
103 public function isExcludeKeySet (string $criteriaKey) {
105 return $this->isKeySet('exclude', $criteriaKey);
109 * Setter for wrapper class name
111 * @param $wrapperConfigEntry Configuration entry which hold the wrapper class' name
114 public final function setWrapperConfigEntry (string $wrapperConfigEntry) {
115 $this->wrapperConfigEntry = $wrapperConfigEntry;
119 * Getter for wrapper class name
121 * @return $wrapperConfigEntry Configuration entry which hold the wrapper class' name
123 public final function getWrapperConfigEntry () {
124 return $this->wrapperConfigEntry;
128 * Getter for criteria array
130 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
133 public final function getCriteriaArray (string $criteriaType = 'default') {
134 return $this->getGenericArrayKey('criteria', $criteriaType, 'entries');
138 * Getter for criteria array 'choice' type
142 public final function getCriteriaChoiceArray () {
143 return $this->getCriteriaArray('choice');
147 * Getter for criteria array 'exclude' type
151 public final function getCriteriaExcludeArray () {
152 return $this->getCriteriaArray('exclude');
156 * Unsets a criteria key from all criteria types
158 * @param $criteriaKey Criteria key to unset
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));
165 // Convert dashes to underscore
166 $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey);
168 // "Walk" through all criterias
169 foreach ($this->getGenericArray('criteria') as $criteriaType => $dummy) {
171 $this->unsetGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey);
176 * Add criteria, this method converts dashes to underscores because dashes
177 * are not valid for criteria keys.
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'
184 public final function addCriteria (string $criteriaKey, $criteriaValue, string $criteriaType = 'default') {
186 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue . ',criteriaType=' . $criteriaType . ' - CALLED!');
188 // Make sure no 'my-' or 'my_' passes this point
189 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
191 // Convert dashes to underscore
192 $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey);
195 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA: criteriaKey=' . $criteriaKey);
198 $this->appendStringToGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey, $criteriaValue);
202 * Set criteria, this method converts dashes to underscores because dashes
203 * are not valid for criteria keys.
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'
210 public final function setCriteria (string $criteriaKey, $criteriaValue, string $criteriaType = 'default') {
212 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue . ',criteriaType=' . $criteriaType . ' - CALLED!');
214 // Make sure no 'my-' or 'my_' passes this point
215 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
217 // Convert dashes to underscore
218 $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey);
221 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA: criteriaKey=' . $criteriaKey);
224 $this->setStringGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey, $criteriaValue);
228 * Add "choice" criteria, this method converts dashes to underscores because
229 * dashes are not valid for criteria keys.
231 * @param $criteriaKey Criteria key
232 * @param $criteriaValue Criteria value
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)));
240 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
243 $this->pushValueToGenericArrayElement('criteria', 'choice', 'entries', StringUtils::convertDashesToUnderscores($criteriaKey), (string) $criteriaValue);
247 * Add "exclude" criteria, this method converts dashes to underscores because
248 * dashes are not valid for criteria keys.
250 * @param $criteriaKey Criteria key
251 * @param $criteriaValue Criteria value
254 public final function addExcludeCriteria (string $criteriaKey, $criteriaValue) {
255 // Add it with generic method
256 $this->addCriteria($criteriaKey, $criteriaValue, 'exclude');
260 * Add configured criteria
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'
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);
274 * Get criteria element or false if not found
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
280 public function getCriteriaElemnent ($criteriaKey, string $criteriaType = 'default') {
282 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaType=' . $criteriaType . ' - CALLED!');
284 // Make sure no 'my-' or 'my_' passes this point
285 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false));
287 // Convert dashes to underscore
288 $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey);
291 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: criteriaKey=' . $criteriaKey . ',criteria()=' . $this->countGenericArrayGroup('criteria', $criteriaType));
293 // Default is not found
296 // Is the criteria there?
297 if ($this->isKeySet($criteriaType, $criteriaKey)) {
299 $value = $this->getGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey);
303 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: value=' . $value . ' - EXIT!');
310 * Get criteria element or false if not found for 'choice' type
312 * @param $criteriaKey The requested criteria key
313 * @return $value Whether the value of the critera or false
315 public function getCriteriaChoiceElemnent (string $criteriaKey) {
317 return $this->getCriteriaElemnent($criteriaKey, 'choice');
321 * Get criteria element or false if not found for 'exclude' type
323 * @param $criteriaKey The requested criteria key
324 * @return $value Whether the value of the critera or false
326 public function getCriteriaExcludeElemnent (string $criteriaKey) {
328 return $this->getCriteriaElemnent($criteriaKey, 'exclude');
332 * Checks whether given array entry matches
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
338 public function ifEntryMatches (array $entryArray, string $criteriaType = 'default') {
339 // First nothing matches and nothing is counted
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));
348 // Convert dashes to underscore
349 $key = StringUtils::convertDashesToUnderscores($key);
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)));
356 // Convert dashes to underscore
357 $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey);
359 // Is the element found and does it match?
360 if (($key == $criteriaKey) && ($criteriaValue == $entry)) {
361 // Then count this one up
367 // Now check if expected criteria counts match
368 $matches = ($counted == $this->countGenericArrayGroup('criteria', $criteriaType));
375 * Checks whether given array 'choice' entry matches
377 * @param $entryArray Array with the entries to find
378 * @return $matches Whether the entry matches or not
380 public function ifChoiceMatches (array $entryArray) {
382 return $this->ifEntryMatches($entryArray, 'choice');
386 * Checks whether given array 'exclude' entry matches
388 * @param $entryArray Array with the entries to find
389 * @return $matches Whether the entry matches or not
391 public function ifExcludeMatches (array $entryArray) {
393 return $this->ifEntryMatches($entryArray, 'exclude');
397 * "Getter" for a cache key
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
403 public function getCacheKey (array $onlyKeys = [], string $criteriaType = 'default') {
405 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($this->__toString() . ': criteriaType=' . $criteriaType . ',count()=' . $this->countGenericArray('criteria')));
407 // Make sure the criteria is there
408 assert($this->isValidGenericArrayGroup('criteria', $criteriaType));
410 // Initialize the key
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)));
418 // $criteriaValue cannot be an array
419 assert(!is_array($criteriaValue));
421 // Convert dashes to underscore
422 $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey);
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;',
429 urlencode($criteriaValue)
434 // Remove last semicolon
435 $cacheKey = substr($cacheKey, 0, -1);
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',
449 // Return the cache key
454 * "Getter" for a cache key ('choice' type)
456 * @param $onlyKeys Only use these keys for a cache key
457 * @return $cacheKey The key suitable for the cache system
459 public function getCacheKeyChoice (array $onlyKeys = []) {
461 return $this->getCacheKey($onlyKeys, 'choice');
465 * "Getter" for a cache key ('exclude' type)
467 * @param $onlyKeys Only use these keys for a cache key
468 * @return $cacheKey The key suitable for the cache system
470 public function getCacheKeyExclude (array $onlyKeys = []) {
472 return $this->getCacheKey($onlyKeys, 'exclude');
476 * Count 'choice' criteria, e.g. useful to find out if a database query
477 * has no limitation (search criteria).
479 * @return $count Count of all criteria entries
481 public final function countChoice () {
482 return $this->count('choice');
486 * Count 'exclude' criteria, e.g. useful to find out if a database query
487 * has no limitation (search criteria).
489 * @return $count Count of all criteria entries
491 public final function countExclude () {
492 return $this->count('exclude');