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 = '';
33 private $criteria = array(
43 * Protected constructor
45 * @param $className Name of the class
48 protected function __construct ($className) {
49 // Call parent constructor
50 parent::__construct($className);
54 * Checks whether given key is set
56 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
57 * @param $criteriaKey Criteria key
58 * @return $isSet Whether key is set
60 public function isKeySet ($criteriaType, $criteriaKey) {
61 // Make sure no 'my-' or 'my_' passes this point
62 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE));
65 $isSet = isset($this->criteria[$criteriaType][$criteriaKey]);
72 * Checks whether given key is set for 'choice' type
74 * @param $criteriaKey Criteria key
75 * @return $isSet Whether key is set
77 public function isChoiceKeySet ($criteriaKey) {
79 return $this->isKeySet('choice', $criteriaKey);
83 * Checks whether given key is set for 'exclude' type
85 * @param $criteriaKey Criteria key
86 * @return $isSet Whether key is set
88 public function isExcludeKeySet ($criteriaKey) {
90 return $this->isKeySet('exclude', $criteriaKey);
94 * Setter for wrapper class name
96 * @param $wrapperConfigEntry Configuration entry which hold the wrapper class' name
99 public final function setWrapperConfigEntry ($wrapperConfigEntry) {
100 $this->wrapperConfigEntry = (string) $wrapperConfigEntry;
104 * Getter for wrapper class name
106 * @return $wrapperConfigEntry Configuration entry which hold the wrapper class' name
108 public final function getWrapperConfigEntry () {
109 return $this->wrapperConfigEntry;
113 * Getter for criteria array
115 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
118 public final function getCriteriaArray ($criteriaType = 'default') {
119 return $this->criteria[$criteriaType];
123 * Getter for criteria array 'choice' type
127 public final function getCriteriaChoiceArray () {
128 return $this->getCriteriaArray('choice');
132 * Getter for criteria array 'exclude' type
136 public final function getCriteriaExcludeArray () {
137 return $this->getCriteriaArray('exclude');
141 * Unsets a criteria key from all criteria types
143 * @param $criteriaKey Criteria key to unset
146 public final function unsetCriteria ($criteriaKey) {
147 // Make sure no 'my-' or 'my_' passes this point
148 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE));
150 // Convert dashes to underscore
151 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
153 // "Walk" through all criterias
154 foreach ($this->criteria as $criteriaType => $dummy) {
156 unset($this->criteria[$criteriaType][$criteriaKey]);
161 * Add criteria, this method converts dashes to underscores because dashes
162 * are not valid for criteria keys.
164 * @param $criteriaKey Criteria key
165 * @param $criteriaValue Criteria value
166 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
169 public final function addCriteria ($criteriaKey, $criteriaValue, $criteriaType = 'default') {
170 // Make sure no 'my-' or 'my_' passes this point
171 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE) && (!is_bool($criteriaValue)));
173 // Convert dashes to underscore
174 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
177 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
179 // Is it already there?
180 if ($this->isKeySet($criteriaType, $criteriaKey)) {
182 $this->criteria[$criteriaType][$criteriaKey] .= ',' . (string) $criteriaValue;
185 $this->criteria[$criteriaType][$criteriaKey] = (string) $criteriaValue;
190 * Add "choice" criteria, this method converts dashes to underscores because
191 * dashes are not valid for criteria keys.
193 * @param $criteriaKey Criteria key
194 * @param $criteriaValue Criteria value
197 public final function addChoiceCriteria ($criteriaKey, $criteriaValue) {
198 // Make sure no 'my-' or 'my_' passes this point
199 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE) && (!is_bool($criteriaValue)));
202 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
205 array_push($this->criteria['choice'][$this->convertDashesToUnderscores($criteriaKey)], (string) $criteriaValue);
209 * Add "exclude" criteria, this method converts dashes to underscores because
210 * dashes are not valid for criteria keys.
212 * @param $criteriaKey Criteria key
213 * @param $criteriaValue Criteria value
216 public final function addExcludeCriteria ($criteriaKey, $criteriaValue) {
217 // Add it with generic method
218 $this->addCriteria($criteriaKey, $criteriaValue, 'exclude');
222 * Add configured criteria
224 * @param $criteriaKey Criteria key
225 * @param $configEntry Configuration entry
226 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
229 public final function addConfiguredCriteria ($criteriaKey, $configEntry, $criteriaType = 'default') {
230 // Add the configuration entry as a criteria
231 $value = $this->getConfigInstance()->getConfigEntry($configEntry);
232 $this->addCriteria($criteriaKey, $value, $criteriaType);
236 * Get criteria element or FALSE if not found
238 * @param $criteriaKey The requested criteria key
239 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
240 * @return $value Whether the value of the critera or FALSE
242 public function getCriteriaElemnent ($criteriaKey, $criteriaType = 'default') {
243 // Make sure no 'my-' or 'my_' passes this point
244 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE));
246 // Convert dashes to underscore
247 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
250 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteria()=' . count($this->criteria[$criteriaType]));
252 // Default is not found
255 // Is the criteria there?
256 if ($this->isKeySet($criteriaType, $criteriaKey)) {
258 $value = $this->criteria[$criteriaType][$criteriaKey];
266 * Get criteria element or FALSE if not found for 'choice' type
268 * @param $criteriaKey The requested criteria key
269 * @return $value Whether the value of the critera or FALSE
271 public function getCriteriaChoiceElemnent ($criteriaKey) {
273 return $this->getCriteriaElemnent($criteriaKey, 'choice');
277 * Get criteria element or FALSE if not found for 'exclude' type
279 * @param $criteriaKey The requested criteria key
280 * @return $value Whether the value of the critera or FALSE
282 public function getCriteriaExcludeElemnent ($criteriaKey) {
284 return $this->getCriteriaElemnent($criteriaKey, 'exclude');
288 * Checks whether given array entry matches
290 * @param $entryArray Array with the entries to find
291 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
292 * @return $matches Whether the entry matches or not
294 public function ifEntryMatches (array $entryArray, $criteriaType = 'default') {
295 // First nothing matches and nothing is counted
299 // Walk through all entries
300 foreach ($entryArray as $key => $entry) {
301 // Make sure no 'my-' or 'my_' passes this point
302 assert((strpos($key, 'my-') === FALSE) && (strpos($key, 'my_') === FALSE));
304 // Convert dashes to underscore
305 $key = $this->convertDashesToUnderscores($key);
307 // Then walk through all search criteria
308 foreach ($this->criteria[$criteriaType] as $criteriaKey => $criteriaValue) {
309 // Make sure no 'my-' or 'my_' passes this point
310 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE) && (!is_bool($criteriaValue)));
312 // Convert dashes to underscore
313 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
315 // Is the element found and does it match?
316 if (($key == $criteriaKey) && ($criteriaValue == $entry)) {
317 // Then count this one up
323 // Now check if expected criteria counts match
324 $matches = ($counted == count($this->criteria[$criteriaType]));
331 * Checks whether given array 'choice' entry matches
333 * @param $entryArray Array with the entries to find
334 * @return $matches Whether the entry matches or not
336 public function ifChoiceMatches (array $entryArray) {
338 return $this->ifEntryMatches($entryArray, 'choice');
342 * Checks whether given array 'exclude' entry matches
344 * @param $entryArray Array with the entries to find
345 * @return $matches Whether the entry matches or not
347 public function ifExcludeMatches (array $entryArray) {
349 return $this->ifEntryMatches($entryArray, 'exclude');
353 * "Getter" for a cache key
355 * @param $onlyKeys Only use these keys for a cache key
356 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
357 * @return $cacheKey The key suitable for the cache system
359 public function getCacheKey ($onlyKeys = array(), $criteriaType = 'default') {
361 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($this->__toString() . ': criteriaType=' . $criteriaType . ',count()=' . count($this->criteria));
363 // Make sure the criteria is there
364 assert((isset($this->criteria[$criteriaType])) && (is_array($this->criteria[$criteriaType])));
366 // Initialize the key
369 // Now walk through all criterias
370 foreach ($this->criteria[$criteriaType] as $criteriaKey => $criteriaValue) {
371 // Make sure no 'my-' or 'my_' passes this point
372 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE) && (!is_bool($criteriaValue)));
374 // Convert dashes to underscore
375 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
377 // Is the value in array or is $onlyKeys empty?
378 if ((isset($onlyKeys[$criteriaKey])) || (count($onlyKeys) == 0)) {
379 // Add the value URL encoded to avoid any trouble with special characters
380 $cacheKey .= sprintf("%s=%s;",
382 urlencode($criteriaValue)
387 // Remove last semicolon
388 $cacheKey = substr($cacheKey, 0, -1);
390 // Is the instance SearchCriteria?
391 if ($this instanceof SearchCriteria) {
392 // Check if 'limit' and 'skip' are in
393 if (((isset($onlyKeys['limit'])) && (isset($onlyKeys['skip']))) || (count($onlyKeys) == 0)) {
394 // Add limit and skip values
395 $cacheKey .= sprintf(";%%limit%%=%s;%%skip%%=%s",
402 // Return the cache key
407 * "Getter" for a cache key ('choice' type)
409 * @param $onlyKeys Only use these keys for a cache key
410 * @return $cacheKey The key suitable for the cache system
412 public function getCacheKeyChoice ($onlyKeys = array()) {
414 return $this->getCacheKey($onlyKeys, 'choice');
418 * "Getter" for a cache key ('exclude' type)
420 * @param $onlyKeys Only use these keys for a cache key
421 * @return $cacheKey The key suitable for the cache system
423 public function getCacheKeyExclude ($onlyKeys = array()) {
425 return $this->getCacheKey($onlyKeys, 'exclude');
429 * Count the criteria, e.g. useful to find out if a database query has no
430 * limitation (search criteria).
432 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
433 * @return $count Count of all criteria entries
435 public final function count ($criteriaType = 'default') {
437 return count($this->criteria[$criteriaType]);
441 * Count 'choice' criteria, e.g. useful to find out if a database query
442 * has no limitation (search criteria).
444 * @return $count Count of all criteria entries
446 public final function countChoice () {
447 return $this->count('choice');
451 * Count 'exclude' criteria, e.g. useful to find out if a database query
452 * has no limitation (search criteria).
454 * @return $count Count of all criteria entries
456 public final function countExclude () {
457 return $this->count('exclude');