3 namespace Org\Mxchange\CoreFramework\Criteria;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Criteria\Search\SearchCriteria;
7 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
10 * A general crtieria class
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria {
33 * Wrapper class name stored in config entry
35 private $wrapperConfigEntry = '';
38 * Protected constructor
40 * @param $className Name of the class
43 protected function __construct ($className) {
44 // Call parent constructor
45 parent::__construct($className);
47 // Initialize all criteria arrays
48 foreach (array('default', 'choice', 'exclude') as $criteriaType) {
50 $this->initGenericArrayKey('criteria', $criteriaType, 'entries');
55 * Checks whether given key is set
57 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
58 * @param $criteriaKey Criteria key
59 * @return $isSet Whether key is set
61 public function isKeySet ($criteriaType, $criteriaKey) {
62 // Make sure no 'my-' or 'my_' passes this point
63 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false));
66 $isSet = $this->isGenericArrayElementSet('criteria', $criteriaType, 'entries', $criteriaKey);
73 * Checks whether given key is set for 'choice' type
75 * @param $criteriaKey Criteria key
76 * @return $isSet Whether key is set
78 public function isChoiceKeySet ($criteriaKey) {
80 return $this->isKeySet('choice', $criteriaKey);
84 * Checks whether given key is set for 'exclude' type
86 * @param $criteriaKey Criteria key
87 * @return $isSet Whether key is set
89 public function isExcludeKeySet ($criteriaKey) {
91 return $this->isKeySet('exclude', $criteriaKey);
95 * Setter for wrapper class name
97 * @param $wrapperConfigEntry Configuration entry which hold the wrapper class' name
100 public final function setWrapperConfigEntry ($wrapperConfigEntry) {
101 $this->wrapperConfigEntry = (string) $wrapperConfigEntry;
105 * Getter for wrapper class name
107 * @return $wrapperConfigEntry Configuration entry which hold the wrapper class' name
109 public final function getWrapperConfigEntry () {
110 return $this->wrapperConfigEntry;
114 * Getter for criteria array
116 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
119 public final function getCriteriaArray ($criteriaType = 'default') {
120 return $this->getGenericArrayKey('criteria', $criteriaType, 'entries');
124 * Getter for criteria array 'choice' type
128 public final function getCriteriaChoiceArray () {
129 return $this->getCriteriaArray('choice');
133 * Getter for criteria array 'exclude' type
137 public final function getCriteriaExcludeArray () {
138 return $this->getCriteriaArray('exclude');
142 * Unsets a criteria key from all criteria types
144 * @param $criteriaKey Criteria key to unset
147 public final function unsetCriteria ($criteriaKey) {
148 // Make sure no 'my-' or 'my_' passes this point
149 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false));
151 // Convert dashes to underscore
152 $criteriaKey = self::convertDashesToUnderscores($criteriaKey);
154 // "Walk" through all criterias
155 foreach ($this->getGenericArray('criteria') as $criteriaType => $dummy) {
157 $this->unsetGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey);
162 * Add criteria, this method converts dashes to underscores because dashes
163 * are not valid for criteria keys.
165 * @param $criteriaKey Criteria key
166 * @param $criteriaValue Criteria value
167 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
170 public final function addCriteria ($criteriaKey, $criteriaValue, $criteriaType = 'default') {
172 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue . ',criteriaType=' . $criteriaType . ' - CALLED!');
174 // Make sure no 'my-' or 'my_' passes this point
175 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
177 // Convert dashes to underscore
178 $criteriaKey = self::convertDashesToUnderscores($criteriaKey);
181 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: criteriaKey=' . $criteriaKey);
184 $this->appendStringToGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey, $criteriaValue);
188 * Set criteria, this method converts dashes to underscores because dashes
189 * are not valid for criteria keys.
191 * @param $criteriaKey Criteria key
192 * @param $criteriaValue Criteria value
193 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
196 public final function setCriteria ($criteriaKey, $criteriaValue, $criteriaType = 'default') {
198 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue . ',criteriaType=' . $criteriaType . ' - CALLED!');
200 // Make sure no 'my-' or 'my_' passes this point
201 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
203 // Convert dashes to underscore
204 $criteriaKey = self::convertDashesToUnderscores($criteriaKey);
207 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: criteriaKey=' . $criteriaKey);
210 $this->setStringGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey, $criteriaValue);
214 * Add "choice" criteria, this method converts dashes to underscores because
215 * dashes are not valid for criteria keys.
217 * @param $criteriaKey Criteria key
218 * @param $criteriaValue Criteria value
221 public final function addChoiceCriteria ($criteriaKey, $criteriaValue) {
222 // Make sure no 'my-' or 'my_' passes this point
223 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
226 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
229 $this->pushValueToGenericArrayElement('criteria', 'choice', 'entries', self::convertDashesToUnderscores($criteriaKey), (string) $criteriaValue);
233 * Add "exclude" criteria, this method converts dashes to underscores because
234 * dashes are not valid for criteria keys.
236 * @param $criteriaKey Criteria key
237 * @param $criteriaValue Criteria value
240 public final function addExcludeCriteria ($criteriaKey, $criteriaValue) {
241 // Add it with generic method
242 $this->addCriteria($criteriaKey, $criteriaValue, 'exclude');
246 * Add configured criteria
248 * @param $criteriaKey Criteria key
249 * @param $configEntry Configuration entry
250 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
253 public final function addConfiguredCriteria ($criteriaKey, $configEntry, $criteriaType = 'default') {
254 // Add the configuration entry as a criteria
255 $value = $this->getConfigInstance()->getConfigEntry($configEntry);
256 $this->addCriteria($criteriaKey, $value, $criteriaType);
260 * Get criteria element or false if not found
262 * @param $criteriaKey The requested criteria key
263 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
264 * @return $value Whether the value of the critera or false
266 public function getCriteriaElemnent ($criteriaKey, $criteriaType = 'default') {
268 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaType=' . $criteriaType . ' - CALLED!');
270 // Make sure no 'my-' or 'my_' passes this point
271 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false));
273 // Convert dashes to underscore
274 $criteriaKey = self::convertDashesToUnderscores($criteriaKey);
277 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteria()=' . $this->countGenericArrayGroup('criteria', $criteriaType));
279 // Default is not found
282 // Is the criteria there?
283 if ($this->isKeySet($criteriaType, $criteriaKey)) {
285 $value = $this->getGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey);
289 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: value=' . $value . ' - EXIT!');
296 * Get criteria element or false if not found for 'choice' type
298 * @param $criteriaKey The requested criteria key
299 * @return $value Whether the value of the critera or false
301 public function getCriteriaChoiceElemnent ($criteriaKey) {
303 return $this->getCriteriaElemnent($criteriaKey, 'choice');
307 * Get criteria element or false if not found for 'exclude' type
309 * @param $criteriaKey The requested criteria key
310 * @return $value Whether the value of the critera or false
312 public function getCriteriaExcludeElemnent ($criteriaKey) {
314 return $this->getCriteriaElemnent($criteriaKey, 'exclude');
318 * Checks whether given array entry matches
320 * @param $entryArray Array with the entries to find
321 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
322 * @return $matches Whether the entry matches or not
324 public function ifEntryMatches (array $entryArray, $criteriaType = 'default') {
325 // First nothing matches and nothing is counted
329 // Walk through all entries
330 foreach ($entryArray as $key => $entry) {
331 // Make sure no 'my-' or 'my_' passes this point
332 assert((strpos($key, 'my-') === false) && (strpos($key, 'my_') === false));
334 // Convert dashes to underscore
335 $key = self::convertDashesToUnderscores($key);
337 // Then walk through all search criteria
338 foreach ($this->getGenericArrayKey('criteria', $criteriaType, 'entries') as $criteriaKey => $criteriaValue) {
339 // Make sure no 'my-' or 'my_' passes this point
340 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
342 // Convert dashes to underscore
343 $criteriaKey = self::convertDashesToUnderscores($criteriaKey);
345 // Is the element found and does it match?
346 if (($key == $criteriaKey) && ($criteriaValue == $entry)) {
347 // Then count this one up
353 // Now check if expected criteria counts match
354 $matches = ($counted == $this->countGenericArrayGroup('criteria', $criteriaType));
361 * Checks whether given array 'choice' entry matches
363 * @param $entryArray Array with the entries to find
364 * @return $matches Whether the entry matches or not
366 public function ifChoiceMatches (array $entryArray) {
368 return $this->ifEntryMatches($entryArray, 'choice');
372 * Checks whether given array 'exclude' entry matches
374 * @param $entryArray Array with the entries to find
375 * @return $matches Whether the entry matches or not
377 public function ifExcludeMatches (array $entryArray) {
379 return $this->ifEntryMatches($entryArray, 'exclude');
383 * "Getter" for a cache key
385 * @param $onlyKeys Only use these keys for a cache key
386 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
387 * @return $cacheKey The key suitable for the cache system
389 public function getCacheKey ($onlyKeys = array(), $criteriaType = 'default') {
391 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($this->__toString() . ': criteriaType=' . $criteriaType . ',count()=' . $this->countGenericArray('criteria')));
393 // Make sure the criteria is there
394 assert($this->isValidGenericArrayGroup('criteria', $criteriaType));
396 // Initialize the key
399 // Now walk through all criterias
400 foreach ($this->getGenericArrayKey('criteria', $criteriaType, 'entries') as $criteriaKey => $criteriaValue) {
401 // Make sure no 'my-' or 'my_' passes this point
402 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
404 // $criteriaValue cannot be an array
405 assert(!is_array($criteriaValue));
407 // Convert dashes to underscore
408 $criteriaKey = self::convertDashesToUnderscores($criteriaKey);
410 // Is the value in array or is $onlyKeys empty?
411 if ((isset($onlyKeys[$criteriaKey])) || (count($onlyKeys) == 0)) {
412 // Add the value URL encoded to avoid any trouble with special characters
413 $cacheKey .= sprintf('%s=%s;',
415 urlencode($criteriaValue)
420 // Remove last semicolon
421 $cacheKey = substr($cacheKey, 0, -1);
423 // Is the instance SearchCriteria?
424 if ($this instanceof SearchCriteria) {
425 // Check if 'limit' and 'skip' are in
426 if (((isset($onlyKeys['limit'])) && (isset($onlyKeys['skip']))) || (count($onlyKeys) == 0)) {
427 // Add limit and skip values
428 $cacheKey .= sprintf(';%%limit%%=%s;%%skip%%=%s',
435 // Return the cache key
440 * "Getter" for a cache key ('choice' type)
442 * @param $onlyKeys Only use these keys for a cache key
443 * @return $cacheKey The key suitable for the cache system
445 public function getCacheKeyChoice ($onlyKeys = array()) {
447 return $this->getCacheKey($onlyKeys, 'choice');
451 * "Getter" for a cache key ('exclude' type)
453 * @param $onlyKeys Only use these keys for a cache key
454 * @return $cacheKey The key suitable for the cache system
456 public function getCacheKeyExclude ($onlyKeys = array()) {
458 return $this->getCacheKey($onlyKeys, 'exclude');
462 * Count the criteria, e.g. useful to find out if a database query has no
463 * limitation (search criteria).
465 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
466 * @return $count Count of all criteria entries
468 public final function count ($criteriaType = 'default') {
470 return $this->countGenericArrayGroup('criteria', $criteriaType);
474 * Count 'choice' criteria, e.g. useful to find out if a database query
475 * has no limitation (search criteria).
477 * @return $count Count of all criteria entries
479 public final function countChoice () {
480 return $this->count('choice');
484 * Count 'exclude' criteria, e.g. useful to find out if a database query
485 * has no limitation (search criteria).
487 * @return $count Count of all criteria entries
489 public final function countExclude () {
490 return $this->count('exclude');