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) {
62 $isSet = isset($this->criteria[$criteriaType][$criteriaKey]);
69 * Checks whether given key is set for 'choice' type
71 * @param $criteriaKey Criteria key
72 * @return $isSet Whether key is set
74 public function isChoiceKeySet ($criteriaKey) {
76 return $this->isKeySet('choice', $criteriaKey);
80 * Checks whether given key is set for 'exclude' type
82 * @param $criteriaKey Criteria key
83 * @return $isSet Whether key is set
85 public function isExcludeKeySet ($criteriaKey) {
87 return $this->isKeySet('exclude', $criteriaKey);
91 * Setter for wrapper class name
93 * @param $wrapperConfigEntry Configuration entry which hold the wrapper class' name
96 public final function setWrapperConfigEntry ($wrapperConfigEntry) {
97 $this->wrapperConfigEntry = (string) $wrapperConfigEntry;
101 * Getter for wrapper class name
103 * @return $wrapperConfigEntry Configuration entry which hold the wrapper class' name
105 public final function getWrapperConfigEntry () {
106 return $this->wrapperConfigEntry;
110 * Getter for criteria array
112 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
115 public final function getCriteriaArray ($criteriaType = 'default') {
116 return $this->criteria[$criteriaType];
120 * Getter for criteria array 'choice' type
124 public final function getCriteriaChoiceArray () {
125 return $this->getCriteriaArray('choice');
129 * Getter for criteria array 'exclude' type
133 public final function getCriteriaExcludeArray () {
134 return $this->getCriteriaArray('exclude');
138 * Add criteria, this method converts dashes to underscores because dashes
139 * are not valid for criteria keys.
141 * @param $criteriaKey Criteria key
142 * @param $criteriaValue Criteria value
143 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
146 public final function addCriteria ($criteriaKey, $criteriaValue, $criteriaType = 'default') {
148 if (strpos($criteriaKey, 'my-') !== false) $this->debugBackTrace('criteriaKey=' . $criteriaKey);
149 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
151 // Convert dashes to underscore
152 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
154 // Is it already there?
155 if ($this->isKeySet($criteriaType, $criteriaKey)) {
157 $this->criteria[$criteriaType][$criteriaKey] .= ',' . $criteriaValue;
160 $this->criteria[$criteriaType][$criteriaKey] = (string) $criteriaValue;
165 * Add "choice" criteria, this method converts dashes to underscores because
166 * dashes are not valid for criteria keys.
168 * @param $criteriaKey Criteria key
169 * @param $criteriaValue Criteria value
172 public final function addChoiceCriteria ($criteriaKey, $criteriaValue) {
174 if (strpos($criteriaKey, 'my-') !== false) $this->debugBackTrace('criteriaKey=' . $criteriaKey);
175 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHOICE-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
178 $this->criteria['choice'][$this->convertDashesToUnderscores($criteriaKey)][] = (string) $criteriaValue;
182 * Add "exclude" criteria, this method converts dashes to underscores because
183 * dashes are not valid for criteria keys.
185 * @param $criteriaKey Criteria key
186 * @param $criteriaValue Criteria value
189 public final function addExcludeCriteria ($criteriaKey, $criteriaValue) {
190 // Add it with generic method
191 $this->addCriteria($criteriaKey, $criteriaValue, 'exclude');
195 * Add configured criteria
197 * @param $criteriaKey Criteria key
198 * @param $configEntry Configuration entry
199 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
202 public final function addConfiguredCriteria ($criteriaKey, $configEntry, $criteriaType = 'default') {
203 // Add the configuration entry as a criteria
204 $value = $this->getConfigInstance()->getConfigEntry($configEntry);
205 $this->addCriteria($criteriaKey, $value, $criteriaType);
209 * Get criteria element or null if not found
211 * @param $criteriaKey The requested criteria key
212 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
213 * @return $value Whether the value of the critera or null
215 public function getCriteriaElemnent ($criteriaKey, $criteriaType = 'default') {
216 // Convert dashes to underscore
217 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
220 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteria()=' . count($this->criteria[$criteriaType]));
222 // Default is not found
225 // Is the criteria there?
226 if ($this->isKeySet($criteriaType, $criteriaKey)) {
228 $value = $this->criteria[$criteriaType][$criteriaKey];
236 * Get criteria element or null if not found for 'choice' type
238 * @param $criteriaKey The requested criteria key
239 * @return $value Whether the value of the critera or null
241 public function getCriteriaChoiceElemnent ($criteriaKey) {
243 return $this->getCriteriaElemnent($criteriaKey, 'choice');
247 * Get criteria element or null if not found for 'exclude' type
249 * @param $criteriaKey The requested criteria key
250 * @return $value Whether the value of the critera or null
252 public function getCriteriaExcludeElemnent ($criteriaKey) {
254 return $this->getCriteriaElemnent($criteriaKey, 'exclude');
258 * Checks whether given array entry matches
260 * @param $entryArray Array with the entries to find
261 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
262 * @return $matches Whether the entry matches or not
264 public function ifEntryMatches (array $entryArray, $criteriaType = 'default') {
265 // First nothing matches and nothing is counted
269 // Walk through all entries
270 foreach ($entryArray as $key => $entry) {
271 // Convert dashes to underscore
272 $key = $this->convertDashesToUnderscores($key);
274 // Then walk through all search criteria
275 foreach ($this->criteria[$criteriaType] as $criteriaKey => $criteriaValue) {
276 // Convert dashes to underscore
277 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
279 // Is the element found and does it match?
280 if (($key == $criteriaKey) && ($criteriaValue == $entry)) {
281 // Then count this one up
287 // Now check if expected criteria counts match
288 $matches = ($counted == count($this->criteria[$criteriaType]));
295 * Checks whether given array 'choice' entry matches
297 * @param $entryArray Array with the entries to find
298 * @return $matches Whether the entry matches or not
300 public function ifChoiceMatches (array $entryArray) {
302 return $this->ifEntryMatches($entryArray, 'choice');
306 * Checks whether given array 'exclude' entry matches
308 * @param $entryArray Array with the entries to find
309 * @return $matches Whether the entry matches or not
311 public function ifExcludeMatches (array $entryArray) {
313 return $this->ifEntryMatches($entryArray, 'exclude');
317 * "Getter" for a cache key
319 * @param $onlyKeys Only use these keys for a cache key
320 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
321 * @return $cacheKey The key suitable for the cache system
323 public function getCacheKey ($onlyKeys = array(), $criteriaType = 'default') {
325 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($this->__toString() . ': criteriaType=' . $criteriaType . ',count()=' . count($this->criteria));
327 // Make sure the criteria is there
328 assert((isset($this->criteria[$criteriaType])) && (is_array($this->criteria[$criteriaType])));
330 // Initialize the key
333 // Now walk through all criterias
334 foreach ($this->criteria[$criteriaType] as $criteriaKey => $criteriaValue) {
335 // Convert dashes to underscore
336 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
338 // Is the value in array or is $onlyKeys empty?
339 if ((isset($onlyKeys[$criteriaKey])) || (count($onlyKeys) == 0)) {
340 // Add the value URL encoded to avoid any trouble with special characters
341 $cacheKey .= sprintf("%s=%s;",
343 urlencode($criteriaValue)
348 // Remove last semicolon
349 $cacheKey = substr($cacheKey, 0, -1);
351 // Is the instance SearchCriteria?
352 if ($this instanceof SearchCriteria) {
353 // Check if 'limit' and 'skip' are in
354 if (((isset($onlyKeys['limit'])) && (isset($onlyKeys['skip']))) || (count($onlyKeys) == 0)) {
355 // Add limit and skip values
356 $cacheKey .= sprintf(";%%limit%%=%s;%%skip%%=%s",
363 // Return the cache key
368 * "Getter" for a cache key ('choice' type)
370 * @param $onlyKeys Only use these keys for a cache key
371 * @return $cacheKey The key suitable for the cache system
373 public function getCacheKeyChoice ($onlyKeys = array()) {
375 return $this->getCacheKey($onlyKeys, 'choice');
379 * "Getter" for a cache key ('exclude' type)
381 * @param $onlyKeys Only use these keys for a cache key
382 * @return $cacheKey The key suitable for the cache system
384 public function getCacheKeyExclude ($onlyKeys = array()) {
386 return $this->getCacheKey($onlyKeys, 'exclude');
390 * Count the criteria, e.g. useful to find out if a database query has no
391 * limitation (search criteria).
393 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
394 * @return $count Count of all criteria entries
396 public final function count ($criteriaType = 'default') {
398 return count($this->criteria[$criteriaType]);
402 * Count 'choice' criteria, e.g. useful to find out if a database query
403 * has no limitation (search criteria).
405 * @return $count Count of all criteria entries
407 public final function countChoice () {
408 return $this->count('choice');
412 * Count 'exclude' criteria, e.g. useful to find out if a database query
413 * has no limitation (search criteria).
415 * @return $count Count of all criteria entries
417 public final function countExclude () {
418 return $this->count('exclude');