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 * Add criteria, this method converts dashes to underscores because dashes
142 * are not valid for criteria keys.
144 * @param $criteriaKey Criteria key
145 * @param $criteriaValue Criteria value
146 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
149 public final function addCriteria ($criteriaKey, $criteriaValue, $criteriaType = 'default') {
150 // Make sure no 'my-' or 'my_' passes this point
151 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE));
153 // Convert dashes to underscore
154 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
157 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
159 // Is it already there?
160 if ($this->isKeySet($criteriaType, $criteriaKey)) {
162 $this->criteria[$criteriaType][$criteriaKey] .= ',' . (string) $criteriaValue;
165 $this->criteria[$criteriaType][$criteriaKey] = (string) $criteriaValue;
170 * Add "choice" criteria, this method converts dashes to underscores because
171 * dashes are not valid for criteria keys.
173 * @param $criteriaKey Criteria key
174 * @param $criteriaValue Criteria value
177 public final function addChoiceCriteria ($criteriaKey, $criteriaValue) {
178 // Make sure no 'my-' or 'my_' passes this point
179 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE));
182 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
185 $this->criteria['choice'][$this->convertDashesToUnderscores($criteriaKey)][] = (string) $criteriaValue;
189 * Add "exclude" criteria, this method converts dashes to underscores because
190 * dashes are not valid for criteria keys.
192 * @param $criteriaKey Criteria key
193 * @param $criteriaValue Criteria value
196 public final function addExcludeCriteria ($criteriaKey, $criteriaValue) {
197 // Add it with generic method
198 $this->addCriteria($criteriaKey, $criteriaValue, 'exclude');
202 * Add configured criteria
204 * @param $criteriaKey Criteria key
205 * @param $configEntry Configuration entry
206 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
209 public final function addConfiguredCriteria ($criteriaKey, $configEntry, $criteriaType = 'default') {
210 // Add the configuration entry as a criteria
211 $value = $this->getConfigInstance()->getConfigEntry($configEntry);
212 $this->addCriteria($criteriaKey, $value, $criteriaType);
216 * Get criteria element or null if not found
218 * @param $criteriaKey The requested criteria key
219 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
220 * @return $value Whether the value of the critera or null
222 public function getCriteriaElemnent ($criteriaKey, $criteriaType = 'default') {
223 // Make sure no 'my-' or 'my_' passes this point
224 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE));
226 // Convert dashes to underscore
227 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
230 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteria()=' . count($this->criteria[$criteriaType]));
232 // Default is not found
235 // Is the criteria there?
236 if ($this->isKeySet($criteriaType, $criteriaKey)) {
238 $value = $this->criteria[$criteriaType][$criteriaKey];
246 * Get criteria element or null if not found for 'choice' type
248 * @param $criteriaKey The requested criteria key
249 * @return $value Whether the value of the critera or null
251 public function getCriteriaChoiceElemnent ($criteriaKey) {
253 return $this->getCriteriaElemnent($criteriaKey, 'choice');
257 * Get criteria element or null if not found for 'exclude' type
259 * @param $criteriaKey The requested criteria key
260 * @return $value Whether the value of the critera or null
262 public function getCriteriaExcludeElemnent ($criteriaKey) {
264 return $this->getCriteriaElemnent($criteriaKey, 'exclude');
268 * Checks whether given array entry matches
270 * @param $entryArray Array with the entries to find
271 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
272 * @return $matches Whether the entry matches or not
274 public function ifEntryMatches (array $entryArray, $criteriaType = 'default') {
275 // First nothing matches and nothing is counted
279 // Walk through all entries
280 foreach ($entryArray as $key => $entry) {
281 // Make sure no 'my-' or 'my_' passes this point
282 assert((strpos($key, 'my-') === FALSE) && (strpos($key, 'my_') === FALSE));
284 // Convert dashes to underscore
285 $key = $this->convertDashesToUnderscores($key);
287 // Then walk through all search criteria
288 foreach ($this->criteria[$criteriaType] as $criteriaKey => $criteriaValue) {
289 // Make sure no 'my-' or 'my_' passes this point
290 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE));
292 // Convert dashes to underscore
293 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
295 // Is the element found and does it match?
296 if (($key == $criteriaKey) && ($criteriaValue == $entry)) {
297 // Then count this one up
303 // Now check if expected criteria counts match
304 $matches = ($counted == count($this->criteria[$criteriaType]));
311 * Checks whether given array 'choice' entry matches
313 * @param $entryArray Array with the entries to find
314 * @return $matches Whether the entry matches or not
316 public function ifChoiceMatches (array $entryArray) {
318 return $this->ifEntryMatches($entryArray, 'choice');
322 * Checks whether given array 'exclude' entry matches
324 * @param $entryArray Array with the entries to find
325 * @return $matches Whether the entry matches or not
327 public function ifExcludeMatches (array $entryArray) {
329 return $this->ifEntryMatches($entryArray, 'exclude');
333 * "Getter" for a cache key
335 * @param $onlyKeys Only use these keys for a cache key
336 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
337 * @return $cacheKey The key suitable for the cache system
339 public function getCacheKey ($onlyKeys = array(), $criteriaType = 'default') {
341 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($this->__toString() . ': criteriaType=' . $criteriaType . ',count()=' . count($this->criteria));
343 // Make sure the criteria is there
344 assert((isset($this->criteria[$criteriaType])) && (is_array($this->criteria[$criteriaType])));
346 // Initialize the key
349 // Now walk through all criterias
350 foreach ($this->criteria[$criteriaType] as $criteriaKey => $criteriaValue) {
351 // Make sure no 'my-' or 'my_' passes this point
352 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE));
354 // Convert dashes to underscore
355 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
357 // Is the value in array or is $onlyKeys empty?
358 if ((isset($onlyKeys[$criteriaKey])) || (count($onlyKeys) == 0)) {
359 // Add the value URL encoded to avoid any trouble with special characters
360 $cacheKey .= sprintf("%s=%s;",
362 urlencode($criteriaValue)
367 // Remove last semicolon
368 $cacheKey = substr($cacheKey, 0, -1);
370 // Is the instance SearchCriteria?
371 if ($this instanceof SearchCriteria) {
372 // Check if 'limit' and 'skip' are in
373 if (((isset($onlyKeys['limit'])) && (isset($onlyKeys['skip']))) || (count($onlyKeys) == 0)) {
374 // Add limit and skip values
375 $cacheKey .= sprintf(";%%limit%%=%s;%%skip%%=%s",
382 // Return the cache key
387 * "Getter" for a cache key ('choice' type)
389 * @param $onlyKeys Only use these keys for a cache key
390 * @return $cacheKey The key suitable for the cache system
392 public function getCacheKeyChoice ($onlyKeys = array()) {
394 return $this->getCacheKey($onlyKeys, 'choice');
398 * "Getter" for a cache key ('exclude' 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 getCacheKeyExclude ($onlyKeys = array()) {
405 return $this->getCacheKey($onlyKeys, 'exclude');
409 * Count the criteria, e.g. useful to find out if a database query has no
410 * limitation (search criteria).
412 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
413 * @return $count Count of all criteria entries
415 public final function count ($criteriaType = 'default') {
417 return count($this->criteria[$criteriaType]);
421 * Count 'choice' criteria, e.g. useful to find out if a database query
422 * has no limitation (search criteria).
424 * @return $count Count of all criteria entries
426 public final function countChoice () {
427 return $this->count('choice');
431 * Count 'exclude' criteria, e.g. useful to find out if a database query
432 * has no limitation (search criteria).
434 * @return $count Count of all criteria entries
436 public final function countExclude () {
437 return $this->count('exclude');