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 * Setter for wrapper class name
56 * @param $wrapperConfigEntry Configuration entry which hold the wrapper class' name
59 public final function setWrapperConfigEntry ($wrapperConfigEntry) {
60 $this->wrapperConfigEntry = (string) $wrapperConfigEntry;
64 * Getter for wrapper class name
66 * @return $wrapperConfigEntry Configuration entry which hold the wrapper class' name
68 public final function getWrapperConfigEntry () {
69 return $this->wrapperConfigEntry;
73 * Getter for criteria array
75 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
78 public final function getCriteriaArray ($criteriaType = 'default') {
79 return $this->criteria[$criteriaType];
83 * Getter for criteria array 'choice' type
87 public final function getCriteriaChoiceArray () {
88 return $this->getCriteriaArray('choice');
92 * Getter for criteria array 'exclude' type
96 public final function getCriteriaExcludeArray () {
97 return $this->getCriteriaArray('exclude');
101 * Add criteria, this method converts dashes to underscores because dashes
102 * are not valid for criteria keys.
104 * @param $criteriaKey Criteria key
105 * @param $criteriaValue Criteria value
106 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
109 public final function addCriteria ($criteriaKey, $criteriaValue, $criteriaType = 'default') {
111 if (strpos($criteriaKey, 'my-') !== false) $this->debugBackTrace('criteriaKey=' . $criteriaKey);
112 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
114 // Convert dashes to underscore
115 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
117 // Is it already there?
118 if (isset($this->criteria[$criteriaType][$criteriaKey])) {
120 $this->criteria[$criteriaType][$criteriaKey] .= ',' . $criteriaValue;
123 $this->criteria[$criteriaType][$criteriaKey] = (string) $criteriaValue;
128 * Add "choice" criteria, this method converts dashes to underscores because
129 * dashes are not valid for criteria keys.
131 * @param $criteriaKey Criteria key
132 * @param $criteriaValue Criteria value
135 public final function addChoiceCriteria ($criteriaKey, $criteriaValue) {
137 if (strpos($criteriaKey, 'my-') !== false) $this->debugBackTrace('criteriaKey=' . $criteriaKey);
138 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHOICE-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
141 $this->criteria['choice'][$this->convertDashesToUnderscores($criteriaKey)][] = (string) $criteriaValue;
145 * Add "exclude" criteria, this method converts dashes to underscores because
146 * dashes are not valid for criteria keys.
148 * @param $criteriaKey Criteria key
149 * @param $criteriaValue Criteria value
152 public final function addExcludeCriteria ($criteriaKey, $criteriaValue) {
153 // Add it with generic method
154 $this->addCriteria($criteriaKey, $criteriaValue, 'exclude');
158 * Add configured criteria
160 * @param $criteriaKey Criteria key
161 * @param $configEntry Configuration entry
162 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
165 public final function addConfiguredCriteria ($criteriaKey, $configEntry, $criteriaType = 'default') {
166 // Add the configuration entry as a criteria
167 $value = $this->getConfigInstance()->getConfigEntry($configEntry);
168 $this->addCriteria($criteriaKey, $value, $criteriaType);
172 * Get criteria element or null if not found
174 * @param $criteriaKey The requested criteria key
175 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
176 * @return $value Whether the value of the critera or null
178 public function getCriteriaElemnent ($criteriaKey, $criteriaType = 'default') {
179 // Convert dashes to underscore
180 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
183 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CRITERIA: criteriaKey=' . $criteriaKey . ',criteria()=' . count($this->criteria[$criteriaType]));
185 // Default is not found
188 // Is the criteria there?
189 if (isset($this->criteria[$criteriaType][$criteriaKey])) {
191 $value = $this->criteria[$criteriaType][$criteriaKey];
199 * Get criteria element or null if not found for 'choice' type
201 * @param $criteriaKey The requested criteria key
202 * @return $value Whether the value of the critera or null
204 public function getCriteriaChoiceElemnent ($criteriaKey) {
206 return $this->getCriteriaElemnent($criteriaKey, 'choice');
210 * Get criteria element or null if not found for 'exclude' type
212 * @param $criteriaKey The requested criteria key
213 * @return $value Whether the value of the critera or null
215 public function getCriteriaExcludeElemnent ($criteriaKey) {
217 return $this->getCriteriaElemnent($criteriaKey, 'exclude');
221 * Checks whether given array entry matches
223 * @param $entryArray Array with the entries to find
224 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
225 * @return $matches Whether the entry matches or not
227 public function ifEntryMatches (array $entryArray, $criteriaType = 'default') {
228 // First nothing matches and nothing is counted
232 // Walk through all entries
233 foreach ($entryArray as $key => $entry) {
234 // Convert dashes to underscore
235 $key = $this->convertDashesToUnderscores($key);
237 // Then walk through all search criteria
238 foreach ($this->criteria[$criteriaType] as $criteriaKey => $criteriaValue) {
239 // Convert dashes to underscore
240 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
242 // Is the element found and does it match?
243 if (($key == $criteriaKey) && ($criteriaValue == $entry)) {
244 // Then count this one up
250 // Now check if expected criteria counts match
251 $matches = ($counted == count($this->criteria[$criteriaType]));
258 * Checks whether given array 'choice' entry matches
260 * @param $entryArray Array with the entries to find
261 * @return $matches Whether the entry matches or not
263 public function ifChoiceMatches (array $entryArray) {
265 return $this->ifEntryMatches($entryArray, 'choice');
269 * Checks whether given array 'exclude' entry matches
271 * @param $entryArray Array with the entries to find
272 * @return $matches Whether the entry matches or not
274 public function ifExcludeMatches (array $entryArray) {
276 return $this->ifEntryMatches($entryArray, 'exclude');
280 * "Getter" for a cache key
282 * @param $onlyKeys Only use these keys for a cache key
283 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
284 * @return $cacheKey The key suitable for the cache system
286 public function getCacheKey ($onlyKeys = array(), $criteriaType = 'default') {
288 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($this->__toString() . ': criteriaType=' . $criteriaType . ',count()=' . count($this->criteria));
290 // Make sure the criteria is there
291 assert((isset($this->criteria[$criteriaType])) && (is_array($this->criteria[$criteriaType])));
293 // Initialize the key
296 // Now walk through all criterias
297 foreach ($this->criteria[$criteriaType] as $criteriaKey => $criteriaValue) {
298 // Convert dashes to underscore
299 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
301 // Is the value in array or is $onlyKeys empty?
302 if ((isset($onlyKeys[$criteriaKey])) || (count($onlyKeys) == 0)) {
303 // Add the value URL encoded to avoid any trouble with special characters
304 $cacheKey .= sprintf("%s=%s;",
306 urlencode($criteriaValue)
311 // Remove last semicolon
312 $cacheKey = substr($cacheKey, 0, -1);
314 // Is the instance SearchCriteria?
315 if ($this instanceof SearchCriteria) {
316 // Check if 'limit' and 'skip' are in
317 if (((isset($onlyKeys['limit'])) && (isset($onlyKeys['skip']))) || (count($onlyKeys) == 0)) {
318 // Add limit and skip values
319 $cacheKey .= sprintf(";%%limit%%=%s;%%skip%%=%s",
326 // Return the cache key
331 * "Getter" for a cache key ('choice' type)
333 * @param $onlyKeys Only use these keys for a cache key
334 * @return $cacheKey The key suitable for the cache system
336 public function getCacheKeyChoice ($onlyKeys = array()) {
338 return $this->getCacheKey($onlyKeys, 'choice');
342 * "Getter" for a cache key ('exclude' type)
344 * @param $onlyKeys Only use these keys for a cache key
345 * @return $cacheKey The key suitable for the cache system
347 public function getCacheKeyExclude ($onlyKeys = array()) {
349 return $this->getCacheKey($onlyKeys, 'exclude');
353 * Count the criteria, e.g. useful to find out if a database query has no
354 * limitation (search criteria).
356 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
357 * @return $count Count of all criteria entries
359 public final function count ($criteriaType = 'default') {
361 return count($this->criteria[$criteriaType]);
365 * Count 'choice' criteria, e.g. useful to find out if a database query
366 * has no limitation (search criteria).
368 * @return $count Count of all criteria entries
370 public final function countChoice () {
371 return $this->count('choice');
375 * Count 'exclude' criteria, e.g. useful to find out if a database query
376 * has no limitation (search criteria).
378 * @return $count Count of all criteria entries
380 public final function countExclude () {
381 return $this->count('exclude');