3 * A general crtieria class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.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);
42 * Checks whether given key is set
44 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
45 * @param $criteriaKey Criteria key
46 * @return $isSet Whether key is set
48 public function isKeySet ($criteriaType, $criteriaKey) {
49 // Make sure no 'my-' or 'my_' passes this point
50 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE));
53 $isSet = $this->isGenericArrayKeySet('criteria', $criteriaType, $criteriaKey);
60 * Checks whether given key is set for 'choice' type
62 * @param $criteriaKey Criteria key
63 * @return $isSet Whether key is set
65 public function isChoiceKeySet ($criteriaKey) {
67 return $this->isKeySet('choice', $criteriaKey);
71 * Checks whether given key is set for 'exclude' type
73 * @param $criteriaKey Criteria key
74 * @return $isSet Whether key is set
76 public function isExcludeKeySet ($criteriaKey) {
78 return $this->isKeySet('exclude', $criteriaKey);
82 * Setter for wrapper class name
84 * @param $wrapperConfigEntry Configuration entry which hold the wrapper class' name
87 public final function setWrapperConfigEntry ($wrapperConfigEntry) {
88 $this->wrapperConfigEntry = (string) $wrapperConfigEntry;
92 * Getter for wrapper class name
94 * @return $wrapperConfigEntry Configuration entry which hold the wrapper class' name
96 public final function getWrapperConfigEntry () {
97 return $this->wrapperConfigEntry;
101 * Getter for criteria array
103 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
106 public final function getCriteriaArray ($criteriaType = 'default') {
107 return $this->getGenericSubArray('criteria', $criteriaType);
111 * Getter for criteria array 'choice' type
115 public final function getCriteriaChoiceArray () {
116 return $this->getCriteriaArray('choice');
120 * Getter for criteria array 'exclude' type
124 public final function getCriteriaExcludeArray () {
125 return $this->getCriteriaArray('exclude');
129 * Unsets a criteria key from all criteria types
131 * @param $criteriaKey Criteria key to unset
134 public final function unsetCriteria ($criteriaKey) {
135 // Make sure no 'my-' or 'my_' passes this point
136 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE));
138 // Convert dashes to underscore
139 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
141 // "Walk" through all criterias
142 foreach ($this->getGenericArray('criteria') as $criteriaType => $dummy) {
144 $this->unsetGenericArrayElement('criteria', $criteriaType, $criteriaKey);
149 * Add criteria, this method converts dashes to underscores because dashes
150 * are not valid for criteria keys.
152 * @param $criteriaKey Criteria key
153 * @param $criteriaValue Criteria value
154 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
157 public final function addCriteria ($criteriaKey, $criteriaValue, $criteriaType = 'default') {
158 // Make sure no 'my-' or 'my_' passes this point
159 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE) && (!is_bool($criteriaValue)));
161 // Convert dashes to underscore
162 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
165 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
168 $this->appendStringToGenericArrayElement('criteria', $criteriaType, $criteriaKey, $criteriaValue);
172 * Add "choice" criteria, this method converts dashes to underscores because
173 * dashes are not valid for criteria keys.
175 * @param $criteriaKey Criteria key
176 * @param $criteriaValue Criteria value
179 public final function addChoiceCriteria ($criteriaKey, $criteriaValue) {
180 // Make sure no 'my-' or 'my_' passes this point
181 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE) && (!is_bool($criteriaValue)));
184 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
187 $this->pushValueToGenericArrayElement('criteria', 'choice', $this->convertDashesToUnderscores($criteriaKey), (string) $criteriaValue);
191 * Add "exclude" criteria, this method converts dashes to underscores because
192 * dashes are not valid for criteria keys.
194 * @param $criteriaKey Criteria key
195 * @param $criteriaValue Criteria value
198 public final function addExcludeCriteria ($criteriaKey, $criteriaValue) {
199 // Add it with generic method
200 $this->addCriteria($criteriaKey, $criteriaValue, 'exclude');
204 * Add configured criteria
206 * @param $criteriaKey Criteria key
207 * @param $configEntry Configuration entry
208 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
211 public final function addConfiguredCriteria ($criteriaKey, $configEntry, $criteriaType = 'default') {
212 // Add the configuration entry as a criteria
213 $value = $this->getConfigInstance()->getConfigEntry($configEntry);
214 $this->addCriteria($criteriaKey, $value, $criteriaType);
218 * Get criteria element or FALSE if not found
220 * @param $criteriaKey The requested criteria key
221 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
222 * @return $value Whether the value of the critera or FALSE
224 public function getCriteriaElemnent ($criteriaKey, $criteriaType = 'default') {
225 // Make sure no 'my-' or 'my_' passes this point
226 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE));
228 // Convert dashes to underscore
229 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
232 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteria()=' . $this->countGenericArrayGroup('criteria', $criteriaType)));
234 // Default is not found
237 // Is the criteria there?
238 if ($this->isKeySet($criteriaType, $criteriaKey)) {
240 $value = $this->getGenericArrayKey('criteria', $criteriaType, $criteriaKey);
248 * Get criteria element or FALSE if not found for 'choice' type
250 * @param $criteriaKey The requested criteria key
251 * @return $value Whether the value of the critera or FALSE
253 public function getCriteriaChoiceElemnent ($criteriaKey) {
255 return $this->getCriteriaElemnent($criteriaKey, 'choice');
259 * Get criteria element or FALSE if not found for 'exclude' type
261 * @param $criteriaKey The requested criteria key
262 * @return $value Whether the value of the critera or FALSE
264 public function getCriteriaExcludeElemnent ($criteriaKey) {
266 return $this->getCriteriaElemnent($criteriaKey, 'exclude');
270 * Checks whether given array entry matches
272 * @param $entryArray Array with the entries to find
273 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
274 * @return $matches Whether the entry matches or not
276 public function ifEntryMatches (array $entryArray, $criteriaType = 'default') {
277 // First nothing matches and nothing is counted
281 // Walk through all entries
282 foreach ($entryArray as $key => $entry) {
283 // Make sure no 'my-' or 'my_' passes this point
284 assert((strpos($key, 'my-') === FALSE) && (strpos($key, 'my_') === FALSE));
286 // Convert dashes to underscore
287 $key = $this->convertDashesToUnderscores($key);
289 // Then walk through all search criteria
290 foreach ($this->getGenericSubArray('criteria', $criteriaType) as $criteriaKey => $criteriaValue) {
291 // Make sure no 'my-' or 'my_' passes this point
292 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE) && (!is_bool($criteriaValue)));
294 // Convert dashes to underscore
295 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
297 // Is the element found and does it match?
298 if (($key == $criteriaKey) && ($criteriaValue == $entry)) {
299 // Then count this one up
305 // Now check if expected criteria counts match
306 $matches = ($counted == $this->countGenericArrayGroup('criteria', $criteriaType));
313 * Checks whether given array 'choice' entry matches
315 * @param $entryArray Array with the entries to find
316 * @return $matches Whether the entry matches or not
318 public function ifChoiceMatches (array $entryArray) {
320 return $this->ifEntryMatches($entryArray, 'choice');
324 * Checks whether given array 'exclude' entry matches
326 * @param $entryArray Array with the entries to find
327 * @return $matches Whether the entry matches or not
329 public function ifExcludeMatches (array $entryArray) {
331 return $this->ifEntryMatches($entryArray, 'exclude');
335 * "Getter" for a cache key
337 * @param $onlyKeys Only use these keys for a cache key
338 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
339 * @return $cacheKey The key suitable for the cache system
341 public function getCacheKey ($onlyKeys = array(), $criteriaType = 'default') {
343 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($this->__toString() . ': criteriaType=' . $criteriaType . ',count()=' . $this->countGenericArray('criteria')));
345 // Make sure the criteria is there
346 assert($this->isValidGenericArrayGroup('criteria', $criteriaType));
348 // Initialize the key
351 // Now walk through all criterias
352 foreach ($this->getGenericSubArray('criteria', $criteriaType) 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 = $this->convertDashesToUnderscores($criteriaKey);
359 // Is the value in array or is $onlyKeys empty?
360 if ((isset($onlyKeys[$criteriaKey])) || (count($onlyKeys) == 0)) {
361 // Add the value URL encoded to avoid any trouble with special characters
362 $cacheKey .= sprintf("%s=%s;",
364 urlencode($criteriaValue)
369 // Remove last semicolon
370 $cacheKey = substr($cacheKey, 0, -1);
372 // Is the instance SearchCriteria?
373 if ($this instanceof SearchCriteria) {
374 // Check if 'limit' and 'skip' are in
375 if (((isset($onlyKeys['limit'])) && (isset($onlyKeys['skip']))) || (count($onlyKeys) == 0)) {
376 // Add limit and skip values
377 $cacheKey .= sprintf(";%%limit%%=%s;%%skip%%=%s",
384 // Return the cache key
389 * "Getter" for a cache key ('choice' type)
391 * @param $onlyKeys Only use these keys for a cache key
392 * @return $cacheKey The key suitable for the cache system
394 public function getCacheKeyChoice ($onlyKeys = array()) {
396 return $this->getCacheKey($onlyKeys, 'choice');
400 * "Getter" for a cache key ('exclude' type)
402 * @param $onlyKeys Only use these keys for a cache key
403 * @return $cacheKey The key suitable for the cache system
405 public function getCacheKeyExclude ($onlyKeys = array()) {
407 return $this->getCacheKey($onlyKeys, 'exclude');
411 * Count the criteria, e.g. useful to find out if a database query has no
412 * limitation (search criteria).
414 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
415 * @return $count Count of all criteria entries
417 public final function count ($criteriaType = 'default') {
419 return $this->countGenericArrayGroup('criteria', $criteriaType);
423 * Count 'choice' criteria, e.g. useful to find out if a database query
424 * has no limitation (search criteria).
426 * @return $count Count of all criteria entries
428 public final function countChoice () {
429 return $this->count('choice');
433 * Count 'exclude' criteria, e.g. useful to find out if a database query
434 * has no limitation (search criteria).
436 * @return $count Count of all criteria entries
438 public final function countExclude () {
439 return $this->count('exclude');