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 <<<<<<< HEAD:framework/main/classes/criteria/class_BaseCriteria.php
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
18 >>>>>>> Some updates::inc/main/classes/criteria/class_BaseCriteria.php
19 * @license GNU GPL 3.0 or any newer version
20 * @link http://www.shipsimu.org
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria {
37 * Wrapper class name stored in config entry
39 private $wrapperConfigEntry = '';
42 * Protected constructor
44 * @param $className Name of the class
47 protected function __construct ($className) {
48 // Call parent constructor
49 parent::__construct($className);
51 // Initialize all criteria arrays
52 foreach (array('default', 'choice', 'exclude') as $criteriaType) {
54 $this->initGenericArrayKey('criteria', $criteriaType, 'entries');
59 * Checks whether given key is set
61 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
62 * @param $criteriaKey Criteria key
63 * @return $isSet Whether key is set
65 public function isKeySet ($criteriaType, $criteriaKey) {
66 // Make sure no 'my-' or 'my_' passes this point
67 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false));
70 $isSet = $this->isGenericArrayElementSet('criteria', $criteriaType, 'entries', $criteriaKey);
77 * Checks whether given key is set for 'choice' type
79 * @param $criteriaKey Criteria key
80 * @return $isSet Whether key is set
82 public function isChoiceKeySet ($criteriaKey) {
84 return $this->isKeySet('choice', $criteriaKey);
88 * Checks whether given key is set for 'exclude' type
90 * @param $criteriaKey Criteria key
91 * @return $isSet Whether key is set
93 public function isExcludeKeySet ($criteriaKey) {
95 return $this->isKeySet('exclude', $criteriaKey);
99 * Setter for wrapper class name
101 * @param $wrapperConfigEntry Configuration entry which hold the wrapper class' name
104 public final function setWrapperConfigEntry ($wrapperConfigEntry) {
105 $this->wrapperConfigEntry = (string) $wrapperConfigEntry;
109 * Getter for wrapper class name
111 * @return $wrapperConfigEntry Configuration entry which hold the wrapper class' name
113 public final function getWrapperConfigEntry () {
114 return $this->wrapperConfigEntry;
118 * Getter for criteria array
120 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
123 public final function getCriteriaArray ($criteriaType = 'default') {
124 return $this->getGenericArrayKey('criteria', $criteriaType, 'entries');
128 * Getter for criteria array 'choice' type
132 public final function getCriteriaChoiceArray () {
133 return $this->getCriteriaArray('choice');
137 * Getter for criteria array 'exclude' type
141 public final function getCriteriaExcludeArray () {
142 return $this->getCriteriaArray('exclude');
146 * Unsets a criteria key from all criteria types
148 * @param $criteriaKey Criteria key to unset
151 public final function unsetCriteria ($criteriaKey) {
152 // Make sure no 'my-' or 'my_' passes this point
153 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false));
155 // Convert dashes to underscore
156 $criteriaKey = self::convertDashesToUnderscores($criteriaKey);
158 // "Walk" through all criterias
159 foreach ($this->getGenericArray('criteria') as $criteriaType => $dummy) {
161 $this->unsetGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey);
166 * Add criteria, this method converts dashes to underscores because dashes
167 * are not valid for criteria keys.
169 * @param $criteriaKey Criteria key
170 * @param $criteriaValue Criteria value
171 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
174 public final function addCriteria ($criteriaKey, $criteriaValue, $criteriaType = 'default') {
176 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue . ',criteriaType=' . $criteriaType . ' - CALLED!');
178 // Make sure no 'my-' or 'my_' passes this point
179 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
181 // Convert dashes to underscore
182 $criteriaKey = self::convertDashesToUnderscores($criteriaKey);
185 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: criteriaKey=' . $criteriaKey);
188 $this->appendStringToGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey, $criteriaValue);
192 * Set criteria, this method converts dashes to underscores because dashes
193 * are not valid for criteria keys.
195 * @param $criteriaKey Criteria key
196 * @param $criteriaValue Criteria value
197 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
200 public final function setCriteria ($criteriaKey, $criteriaValue, $criteriaType = 'default') {
202 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue . ',criteriaType=' . $criteriaType . ' - CALLED!');
204 // Make sure no 'my-' or 'my_' passes this point
205 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
207 // Convert dashes to underscore
208 $criteriaKey = self::convertDashesToUnderscores($criteriaKey);
211 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: criteriaKey=' . $criteriaKey);
214 $this->setStringGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey, $criteriaValue);
218 * Add "choice" criteria, this method converts dashes to underscores because
219 * dashes are not valid for criteria keys.
221 * @param $criteriaKey Criteria key
222 * @param $criteriaValue Criteria value
225 public final function addChoiceCriteria ($criteriaKey, $criteriaValue) {
226 // Make sure no 'my-' or 'my_' passes this point
227 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
230 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
233 $this->pushValueToGenericArrayElement('criteria', 'choice', 'entries', self::convertDashesToUnderscores($criteriaKey), (string) $criteriaValue);
237 * Add "exclude" criteria, this method converts dashes to underscores because
238 * dashes are not valid for criteria keys.
240 * @param $criteriaKey Criteria key
241 * @param $criteriaValue Criteria value
244 public final function addExcludeCriteria ($criteriaKey, $criteriaValue) {
245 // Add it with generic method
246 $this->addCriteria($criteriaKey, $criteriaValue, 'exclude');
250 * Add configured criteria
252 * @param $criteriaKey Criteria key
253 * @param $configEntry Configuration entry
254 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
257 public final function addConfiguredCriteria ($criteriaKey, $configEntry, $criteriaType = 'default') {
258 // Add the configuration entry as a criteria
259 $value = $this->getConfigInstance()->getConfigEntry($configEntry);
260 $this->addCriteria($criteriaKey, $value, $criteriaType);
264 * Get criteria element or false if not found
266 * @param $criteriaKey The requested criteria key
267 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
268 * @return $value Whether the value of the critera or false
270 public function getCriteriaElemnent ($criteriaKey, $criteriaType = 'default') {
272 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaType=' . $criteriaType . ' - CALLED!');
274 // Make sure no 'my-' or 'my_' passes this point
275 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false));
277 // Convert dashes to underscore
278 $criteriaKey = self::convertDashesToUnderscores($criteriaKey);
281 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteria()=' . $this->countGenericArrayGroup('criteria', $criteriaType));
283 // Default is not found
286 // Is the criteria there?
287 if ($this->isKeySet($criteriaType, $criteriaKey)) {
289 $value = $this->getGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey);
293 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: value=' . $value . ' - EXIT!');
300 * Get criteria element or false if not found for 'choice' type
302 * @param $criteriaKey The requested criteria key
303 * @return $value Whether the value of the critera or false
305 public function getCriteriaChoiceElemnent ($criteriaKey) {
307 return $this->getCriteriaElemnent($criteriaKey, 'choice');
311 * Get criteria element or false if not found for 'exclude' type
313 * @param $criteriaKey The requested criteria key
314 * @return $value Whether the value of the critera or false
316 public function getCriteriaExcludeElemnent ($criteriaKey) {
318 return $this->getCriteriaElemnent($criteriaKey, 'exclude');
322 * Checks whether given array entry matches
324 * @param $entryArray Array with the entries to find
325 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
326 * @return $matches Whether the entry matches or not
328 public function ifEntryMatches (array $entryArray, $criteriaType = 'default') {
329 // First nothing matches and nothing is counted
333 // Walk through all entries
334 foreach ($entryArray as $key => $entry) {
335 // Make sure no 'my-' or 'my_' passes this point
336 assert((strpos($key, 'my-') === false) && (strpos($key, 'my_') === false));
338 // Convert dashes to underscore
339 $key = self::convertDashesToUnderscores($key);
341 // Then walk through all search criteria
342 foreach ($this->getGenericArrayKey('criteria', $criteriaType, 'entries') as $criteriaKey => $criteriaValue) {
343 // Make sure no 'my-' or 'my_' passes this point
344 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
346 // Convert dashes to underscore
347 $criteriaKey = self::convertDashesToUnderscores($criteriaKey);
349 // Is the element found and does it match?
350 if (($key == $criteriaKey) && ($criteriaValue == $entry)) {
351 // Then count this one up
357 // Now check if expected criteria counts match
358 $matches = ($counted == $this->countGenericArrayGroup('criteria', $criteriaType));
365 * Checks whether given array 'choice' entry matches
367 * @param $entryArray Array with the entries to find
368 * @return $matches Whether the entry matches or not
370 public function ifChoiceMatches (array $entryArray) {
372 return $this->ifEntryMatches($entryArray, 'choice');
376 * Checks whether given array 'exclude' entry matches
378 * @param $entryArray Array with the entries to find
379 * @return $matches Whether the entry matches or not
381 public function ifExcludeMatches (array $entryArray) {
383 return $this->ifEntryMatches($entryArray, 'exclude');
387 * "Getter" for a cache key
389 * @param $onlyKeys Only use these keys for a cache key
390 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
391 * @return $cacheKey The key suitable for the cache system
393 public function getCacheKey ($onlyKeys = array(), $criteriaType = 'default') {
395 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($this->__toString() . ': criteriaType=' . $criteriaType . ',count()=' . $this->countGenericArray('criteria')));
397 // Make sure the criteria is there
398 assert($this->isValidGenericArrayGroup('criteria', $criteriaType));
400 // Initialize the key
403 // Now walk through all criterias
404 foreach ($this->getGenericArrayKey('criteria', $criteriaType, 'entries') as $criteriaKey => $criteriaValue) {
405 // Make sure no 'my-' or 'my_' passes this point
406 assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue)));
408 // $criteriaValue cannot be an array
409 assert(!is_array($criteriaValue));
411 // Convert dashes to underscore
412 $criteriaKey = self::convertDashesToUnderscores($criteriaKey);
414 // Is the value in array or is $onlyKeys empty?
415 if ((isset($onlyKeys[$criteriaKey])) || (count($onlyKeys) == 0)) {
416 // Add the value URL encoded to avoid any trouble with special characters
417 $cacheKey .= sprintf('%s=%s;',
419 urlencode($criteriaValue)
424 // Remove last semicolon
425 $cacheKey = substr($cacheKey, 0, -1);
427 // Is the instance SearchCriteria?
428 if ($this instanceof SearchCriteria) {
429 // Check if 'limit' and 'skip' are in
430 if (((isset($onlyKeys['limit'])) && (isset($onlyKeys['skip']))) || (count($onlyKeys) == 0)) {
431 // Add limit and skip values
432 $cacheKey .= sprintf(';%%limit%%=%s;%%skip%%=%s',
439 // Return the cache key
444 * "Getter" for a cache key ('choice' type)
446 * @param $onlyKeys Only use these keys for a cache key
447 * @return $cacheKey The key suitable for the cache system
449 public function getCacheKeyChoice ($onlyKeys = array()) {
451 return $this->getCacheKey($onlyKeys, 'choice');
455 * "Getter" for a cache key ('exclude' type)
457 * @param $onlyKeys Only use these keys for a cache key
458 * @return $cacheKey The key suitable for the cache system
460 public function getCacheKeyExclude ($onlyKeys = array()) {
462 return $this->getCacheKey($onlyKeys, 'exclude');
466 * Count the criteria, e.g. useful to find out if a database query has no
467 * limitation (search criteria).
469 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
470 * @return $count Count of all criteria entries
472 public final function count ($criteriaType = 'default') {
474 return $this->countGenericArrayGroup('criteria', $criteriaType);
478 * Count 'choice' criteria, e.g. useful to find out if a database query
479 * has no limitation (search criteria).
481 * @return $count Count of all criteria entries
483 public final function countChoice () {
484 return $this->count('choice');
488 * Count 'exclude' criteria, e.g. useful to find out if a database query
489 * has no limitation (search criteria).
491 * @return $count Count of all criteria entries
493 public final function countExclude () {
494 return $this->count('exclude');