3 * A general crtieria class
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class BaseCriteria extends BaseFrameworkSystem implements Criteria {
26 * Wrapper class name stored in config entry
28 private $wrapperConfigEntry = '';
31 * Protected constructor
33 * @param $className Name of the class
36 protected function __construct ($className) {
37 // Call parent constructor
38 parent::__construct($className);
40 // Initialize all criteria arrays
41 foreach (array('default', 'choice', 'exclude') as $criteriaType) {
43 $this->initGenericArrayKey('criteria', $criteriaType, 'entries');
48 * Checks whether given key is set
50 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
51 * @param $criteriaKey Criteria key
52 * @return $isSet Whether key is set
54 public function isKeySet ($criteriaType, $criteriaKey) {
55 // Make sure no 'my-' or 'my_' passes this point
56 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE));
59 $isSet = $this->isGenericArrayElementSet('criteria', $criteriaType, 'entries', $criteriaKey);
66 * Checks whether given key is set for 'choice' type
68 * @param $criteriaKey Criteria key
69 * @return $isSet Whether key is set
71 public function isChoiceKeySet ($criteriaKey) {
73 return $this->isKeySet('choice', $criteriaKey);
77 * Checks whether given key is set for 'exclude' type
79 * @param $criteriaKey Criteria key
80 * @return $isSet Whether key is set
82 public function isExcludeKeySet ($criteriaKey) {
84 return $this->isKeySet('exclude', $criteriaKey);
88 * Setter for wrapper class name
90 * @param $wrapperConfigEntry Configuration entry which hold the wrapper class' name
93 public final function setWrapperConfigEntry ($wrapperConfigEntry) {
94 $this->wrapperConfigEntry = (string) $wrapperConfigEntry;
98 * Getter for wrapper class name
100 * @return $wrapperConfigEntry Configuration entry which hold the wrapper class' name
102 public final function getWrapperConfigEntry () {
103 return $this->wrapperConfigEntry;
107 * Getter for criteria array
109 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
112 public final function getCriteriaArray ($criteriaType = 'default') {
113 return $this->getGenericArrayKey('criteria', $criteriaType, 'entries');
117 * Getter for criteria array 'choice' type
121 public final function getCriteriaChoiceArray () {
122 return $this->getCriteriaArray('choice');
126 * Getter for criteria array 'exclude' type
130 public final function getCriteriaExcludeArray () {
131 return $this->getCriteriaArray('exclude');
135 * Unsets a criteria key from all criteria types
137 * @param $criteriaKey Criteria key to unset
140 public final function unsetCriteria ($criteriaKey) {
141 // Make sure no 'my-' or 'my_' passes this point
142 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE));
144 // Convert dashes to underscore
145 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
147 // "Walk" through all criterias
148 foreach ($this->getGenericArray('criteria') as $criteriaType => $dummy) {
150 $this->unsetGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey);
155 * Add criteria, this method converts dashes to underscores because dashes
156 * are not valid for criteria keys.
158 * @param $criteriaKey Criteria key
159 * @param $criteriaValue Criteria value
160 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
163 public final function addCriteria ($criteriaKey, $criteriaValue, $criteriaType = 'default') {
164 // Make sure no 'my-' or 'my_' passes this point
165 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE) && (!is_bool($criteriaValue)));
167 // Convert dashes to underscore
168 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
171 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
174 $this->appendStringToGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey, $criteriaValue);
178 * Add "choice" criteria, this method converts dashes to underscores because
179 * dashes are not valid for criteria keys.
181 * @param $criteriaKey Criteria key
182 * @param $criteriaValue Criteria value
185 public final function addChoiceCriteria ($criteriaKey, $criteriaValue) {
186 // Make sure no 'my-' or 'my_' passes this point
187 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE) && (!is_bool($criteriaValue)));
190 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
193 $this->pushValueToGenericArrayElement('criteria', 'choice', 'entries', $this->convertDashesToUnderscores($criteriaKey), (string) $criteriaValue);
197 * Add "exclude" criteria, this method converts dashes to underscores because
198 * dashes are not valid for criteria keys.
200 * @param $criteriaKey Criteria key
201 * @param $criteriaValue Criteria value
204 public final function addExcludeCriteria ($criteriaKey, $criteriaValue) {
205 // Add it with generic method
206 $this->addCriteria($criteriaKey, $criteriaValue, 'exclude');
210 * Add configured criteria
212 * @param $criteriaKey Criteria key
213 * @param $configEntry Configuration entry
214 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
217 public final function addConfiguredCriteria ($criteriaKey, $configEntry, $criteriaType = 'default') {
218 // Add the configuration entry as a criteria
219 $value = $this->getConfigInstance()->getConfigEntry($configEntry);
220 $this->addCriteria($criteriaKey, $value, $criteriaType);
224 * Get criteria element or FALSE if not found
226 * @param $criteriaKey The requested criteria key
227 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
228 * @return $value Whether the value of the critera or FALSE
230 public function getCriteriaElemnent ($criteriaKey, $criteriaType = 'default') {
231 // Make sure no 'my-' or 'my_' passes this point
232 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE));
234 // Convert dashes to underscore
235 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
238 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteria()=' . $this->countGenericArrayGroup('criteria', $criteriaType)));
240 // Default is not found
243 // Is the criteria there?
244 if ($this->isKeySet($criteriaType, $criteriaKey)) {
246 $value = $this->getGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey);
254 * Get criteria element or FALSE if not found for 'choice' type
256 * @param $criteriaKey The requested criteria key
257 * @return $value Whether the value of the critera or FALSE
259 public function getCriteriaChoiceElemnent ($criteriaKey) {
261 return $this->getCriteriaElemnent($criteriaKey, 'choice');
265 * Get criteria element or FALSE if not found for 'exclude' type
267 * @param $criteriaKey The requested criteria key
268 * @return $value Whether the value of the critera or FALSE
270 public function getCriteriaExcludeElemnent ($criteriaKey) {
272 return $this->getCriteriaElemnent($criteriaKey, 'exclude');
276 * Checks whether given array entry matches
278 * @param $entryArray Array with the entries to find
279 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
280 * @return $matches Whether the entry matches or not
282 public function ifEntryMatches (array $entryArray, $criteriaType = 'default') {
283 // First nothing matches and nothing is counted
287 // Walk through all entries
288 foreach ($entryArray as $key => $entry) {
289 // Make sure no 'my-' or 'my_' passes this point
290 assert((strpos($key, 'my-') === FALSE) && (strpos($key, 'my_') === FALSE));
292 // Convert dashes to underscore
293 $key = $this->convertDashesToUnderscores($key);
295 // Then walk through all search criteria
296 foreach ($this->getGenericArrayKey('criteria', $criteriaType, 'entries') as $criteriaKey => $criteriaValue) {
297 // Make sure no 'my-' or 'my_' passes this point
298 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE) && (!is_bool($criteriaValue)));
300 // Convert dashes to underscore
301 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
303 // Is the element found and does it match?
304 if (($key == $criteriaKey) && ($criteriaValue == $entry)) {
305 // Then count this one up
311 // Now check if expected criteria counts match
312 $matches = ($counted == $this->countGenericArrayGroup('criteria', $criteriaType));
319 * Checks whether given array 'choice' entry matches
321 * @param $entryArray Array with the entries to find
322 * @return $matches Whether the entry matches or not
324 public function ifChoiceMatches (array $entryArray) {
326 return $this->ifEntryMatches($entryArray, 'choice');
330 * Checks whether given array 'exclude' entry matches
332 * @param $entryArray Array with the entries to find
333 * @return $matches Whether the entry matches or not
335 public function ifExcludeMatches (array $entryArray) {
337 return $this->ifEntryMatches($entryArray, 'exclude');
341 * "Getter" for a cache key
343 * @param $onlyKeys Only use these keys for a cache key
344 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
345 * @return $cacheKey The key suitable for the cache system
347 public function getCacheKey ($onlyKeys = array(), $criteriaType = 'default') {
349 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($this->__toString() . ': criteriaType=' . $criteriaType . ',count()=' . $this->countGenericArray('criteria')));
351 // Make sure the criteria is there
352 assert($this->isValidGenericArrayGroup('criteria', $criteriaType));
354 // Initialize the key
357 // Now walk through all criterias
358 foreach ($this->getGenericArrayKey('criteria', $criteriaType, 'entries') as $criteriaKey => $criteriaValue) {
359 // Make sure no 'my-' or 'my_' passes this point
360 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE) && (!is_bool($criteriaValue)));
362 // $criteriaValue cannot be an array
363 assert(!is_array($criteriaValue));
365 // Convert dashes to underscore
366 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
368 // Is the value in array or is $onlyKeys empty?
369 if ((isset($onlyKeys[$criteriaKey])) || (count($onlyKeys) == 0)) {
370 // Add the value URL encoded to avoid any trouble with special characters
371 $cacheKey .= sprintf('%s=%s;',
373 urlencode($criteriaValue)
378 // Remove last semicolon
379 $cacheKey = substr($cacheKey, 0, -1);
381 // Is the instance SearchCriteria?
382 if ($this instanceof SearchCriteria) {
383 // Check if 'limit' and 'skip' are in
384 if (((isset($onlyKeys['limit'])) && (isset($onlyKeys['skip']))) || (count($onlyKeys) == 0)) {
385 // Add limit and skip values
386 $cacheKey .= sprintf(';%%limit%%=%s;%%skip%%=%s',
393 // Return the cache key
398 * "Getter" for a cache key ('choice' type)
400 * @param $onlyKeys Only use these keys for a cache key
401 * @return $cacheKey The key suitable for the cache system
403 public function getCacheKeyChoice ($onlyKeys = array()) {
405 return $this->getCacheKey($onlyKeys, 'choice');
409 * "Getter" for a cache key ('exclude' type)
411 * @param $onlyKeys Only use these keys for a cache key
412 * @return $cacheKey The key suitable for the cache system
414 public function getCacheKeyExclude ($onlyKeys = array()) {
416 return $this->getCacheKey($onlyKeys, 'exclude');
420 * Count the criteria, e.g. useful to find out if a database query has no
421 * limitation (search criteria).
423 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
424 * @return $count Count of all criteria entries
426 public final function count ($criteriaType = 'default') {
428 return $this->countGenericArrayGroup('criteria', $criteriaType);
432 * Count 'choice' criteria, e.g. useful to find out if a database query
433 * has no limitation (search criteria).
435 * @return $count Count of all criteria entries
437 public final function countChoice () {
438 return $this->count('choice');
442 * Count 'exclude' criteria, e.g. useful to find out if a database query
443 * has no limitation (search criteria).
445 * @return $count Count of all criteria entries
447 public final function countExclude () {
448 return $this->count('exclude');