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();
36 * Protected constructor
38 * @param $className Name of the class
41 protected function __construct ($className) {
42 // Call parent constructor
43 parent::__construct($className);
47 * Setter for wrapper class name
49 * @param $wrapperConfigEntry Configuration entry which hold the wrapper class' name
52 public final function setWrapperConfigEntry ($wrapperConfigEntry) {
53 $this->wrapperConfigEntry = (string) $wrapperConfigEntry;
57 * Getter for wrapper class name
59 * @return $wrapperConfigEntry Configuration entry which hold the wrapper class' name
61 public final function getWrapperConfigEntry () {
62 return $this->wrapperConfigEntry;
66 * Getter for criteria array
68 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
71 public final function getCriteriaArray ($criteriaType = 'default') {
72 return $this->criteria[$criteriaType];
76 * Getter for criteria array 'choice' type
80 public final function getCriteriaChoiceArray () {
81 return $this->getCriteriaArray('choice');
85 * Getter for criteria array 'exclude' type
89 public final function getCriteriaExcludeArray () {
90 return $this->getCriteriaArray('exclude');
94 * Add criteria, this method converts dashes to underscores because dashes
95 * are not valid for criteria keys.
97 * @param $criteriaKey Criteria key
98 * @param $criteriaValue Criteria value
99 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
102 public final function addCriteria ($criteriaKey, $criteriaValue, $criteriaType = 'default') {
104 if (strpos($criteriaKey, 'my-') !== false) $this->debugBackTrace('criteriaKey=' . $criteriaKey);
105 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
107 // Convert dashes to underscore
108 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
110 // Is it already there?
111 if (isset($this->criteria[$criteriaType][$criteriaKey])) {
113 $this->criteria[$criteriaType][$criteriaKey] .= ',' . $criteriaValue;
116 $this->criteria[$criteriaType][$criteriaKey] = (string) $criteriaValue;
121 * Add "choice" criteria, this method converts dashes to underscores because
122 * dashes are not valid for criteria keys.
124 * @param $criteriaKey Criteria key
125 * @param $criteriaValue Criteria value
128 public final function addChoiceCriteria ($criteriaKey, $criteriaValue) {
130 if (strpos($criteriaKey, 'my-') !== false) $this->debugBackTrace('criteriaKey=' . $criteriaKey);
131 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHOICE-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
134 $this->criteria['choice'][$this->convertDashesToUnderscores($criteriaKey)][] = (string) $criteriaValue;
138 * Add "exclude" criteria, this method converts dashes to underscores because
139 * dashes are not valid for criteria keys.
141 * @param $criteriaKey Criteria key
142 * @param $criteriaValue Criteria value
145 public final function addExcludeCriteria ($criteriaKey, $criteriaValue) {
146 // Add it with generic method
147 $this->addCriteria($criteriaKey, $criteriaValue, 'exclude');
151 * Add configured criteria
153 * @param $criteriaKey Criteria key
154 * @param $configEntry Configuration entry
155 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
158 public final function addConfiguredCriteria ($criteriaKey, $configEntry, $criteriaType = 'default') {
159 // Add the configuration entry as a criteria
160 $value = $this->getConfigInstance()->getConfigEntry($configEntry);
161 $this->addCriteria($criteriaKey, $value, $criteriaType);
165 * Get criteria element or null if not found
167 * @param $criteriaKey The requested criteria key
168 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
169 * @return $value Whether the value of the critera or null
171 public function getCriteriaElemnent ($criteriaKey, $criteriaType = 'default') {
172 // Convert dashes to underscore
173 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
176 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CRITERIA: criteriaKey=' . $criteriaKey . ',criteria()=' . count($this->criteria[$criteriaType]));
178 // Default is not found
181 // Is the criteria there?
182 if (isset($this->criteria[$criteriaType][$criteriaKey])) {
184 $value = $this->criteria[$criteriaType][$criteriaKey];
192 * Checks whether given array entry matches
194 * @param $entryArray Array with the entries to find
195 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
196 * @return $matches Whether the entry matches or not
198 public function ifEntryMatches (array $entryArray, $criteriaType = 'default') {
199 // First nothing matches and nothing is counted
203 // Walk through all entries
204 foreach ($entryArray as $key => $entry) {
205 // Convert dashes to underscore
206 $key = $this->convertDashesToUnderscores($key);
208 // Then walk through all search criteria
209 foreach ($this->criteria[$criteriaType] as $criteriaKey => $criteriaValue) {
210 // Convert dashes to underscore
211 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
213 // Is the element found and does it match?
214 if (($key == $criteriaKey) && ($criteriaValue == $entry)) {
215 // Then count this one up
221 // Now check if expected criteria counts match
222 $matches = ($counted == count($this->criteria[$criteriaType]));
229 * Checks whether given array 'choice' entry matches
231 * @param $entryArray Array with the entries to find
232 * @return $matches Whether the entry matches or not
234 public function ifChoiceMatches (array $entryArray) {
236 return $this->ifEntryMatches($entryArray, 'choice');
240 * Checks whether given array 'exclude' entry matches
242 * @param $entryArray Array with the entries to find
243 * @return $matches Whether the entry matches or not
245 public function ifExcludeMatches (array $entryArray) {
247 return $this->ifEntryMatches($entryArray, 'exclude');
251 * "Getter" for a cache key
253 * @param $onlyKeys Only use these keys for a cache key
254 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
255 * @return $cacheKey The key suitable for the cache system
257 public function getCacheKey ($onlyKeys = array(), $criteriaType = 'default') {
258 // Initialize the key
261 // Now walk through all criterias
262 foreach ($this->criteria[$criteriaType] as $criteriaKey => $criteriaValue) {
263 // Convert dashes to underscore
264 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
266 // Is the value in array or is $onlyKeys empty?
267 if ((isset($onlyKeys[$criteriaKey])) || (count($onlyKeys) == 0)) {
268 // Add the value URL encoded to avoid any trouble with special characters
269 $cacheKey .= sprintf("%s=%s;",
271 urlencode($criteriaValue)
276 // Remove last semicolon
277 $cacheKey = substr($cacheKey, 0, -1);
279 // Is the instance SearchCriteria?
280 if ($this instanceof SearchCriteria) {
281 // Check if 'limit' and 'skip' are in
282 if (((isset($onlyKeys['limit'])) && (isset($onlyKeys['skip']))) || (count($onlyKeys) == 0)) {
283 // Add limit and skip values
284 $cacheKey .= sprintf(";%%limit%%=%s;%%skip%%=%s",
291 // Return the cache key
296 * "Getter" for a cache key ('choice' type)
298 * @param $onlyKeys Only use these keys for a cache key
299 * @return $cacheKey The key suitable for the cache system
301 public function getCacheKeyChoice ($onlyKeys = array()) {
303 return $this->getCacheKey($onlyKeys, 'choice');
307 * "Getter" for a cache key ('exclude' type)
309 * @param $onlyKeys Only use these keys for a cache key
310 * @return $cacheKey The key suitable for the cache system
312 public function getCacheKeyExclude ($onlyKeys = array()) {
314 return $this->getCacheKey($onlyKeys, 'exclude');
318 * "Getter" for a cache key ('choice' type)
320 * @param $onlyKeys Only use these keys for a cache key
321 * @return $cacheKey The key suitable for the cache system
323 public function getCacheKeyChoice ($onlyKeys = array()) {
325 return $this->getCacheKey($onlyKeys, 'choice');
329 * Count the criteria, e.g. useful to find out if a database query has no
330 * limitation (search criteria).
332 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
333 * @return $count Count of all criteria entries
335 public final function count ($criteriaType = 'default') {
337 return count($this->criteria[$criteriaType]);
341 * Count 'choice' criteria, e.g. useful to find out if a database query
342 * has no limitation (search criteria).
344 * @return $count Count of all criteria entries
346 public final function countChoice () {
347 return $this->count('choice');
351 * Count 'exclude' criteria, e.g. useful to find out if a database query
352 * has no limitation (search criteria).
354 * @return $count Count of all criteria entries
356 public final function countExclude () {
357 return $this->count('exclude');