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 * Checks whether given array entry matches
201 * @param $entryArray Array with the entries to find
202 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
203 * @return $matches Whether the entry matches or not
205 public function ifEntryMatches (array $entryArray, $criteriaType = 'default') {
206 // First nothing matches and nothing is counted
210 // Walk through all entries
211 foreach ($entryArray as $key => $entry) {
212 // Convert dashes to underscore
213 $key = $this->convertDashesToUnderscores($key);
215 // Then walk through all search criteria
216 foreach ($this->criteria[$criteriaType] as $criteriaKey => $criteriaValue) {
217 // Convert dashes to underscore
218 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
220 // Is the element found and does it match?
221 if (($key == $criteriaKey) && ($criteriaValue == $entry)) {
222 // Then count this one up
228 // Now check if expected criteria counts match
229 $matches = ($counted == count($this->criteria[$criteriaType]));
236 * Checks whether given array 'choice' entry matches
238 * @param $entryArray Array with the entries to find
239 * @return $matches Whether the entry matches or not
241 public function ifChoiceMatches (array $entryArray) {
243 return $this->ifEntryMatches($entryArray, 'choice');
247 * Checks whether given array 'exclude' entry matches
249 * @param $entryArray Array with the entries to find
250 * @return $matches Whether the entry matches or not
252 public function ifExcludeMatches (array $entryArray) {
254 return $this->ifEntryMatches($entryArray, 'exclude');
258 * "Getter" for a cache key
260 * @param $onlyKeys Only use these keys for a cache key
261 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
262 * @return $cacheKey The key suitable for the cache system
264 public function getCacheKey ($onlyKeys = array(), $criteriaType = 'default') {
266 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($this->__toString() . ': criteriaType=' . $criteriaType . ',count()=' . count($this->criteria));
268 // Make sure the criteria is there
269 assert((isset($this->criteria[$criteriaType])) && (is_array($this->criteria[$criteriaType])));
271 // Initialize the key
274 // Now walk through all criterias
275 foreach ($this->criteria[$criteriaType] as $criteriaKey => $criteriaValue) {
276 // Convert dashes to underscore
277 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
279 // Is the value in array or is $onlyKeys empty?
280 if ((isset($onlyKeys[$criteriaKey])) || (count($onlyKeys) == 0)) {
281 // Add the value URL encoded to avoid any trouble with special characters
282 $cacheKey .= sprintf("%s=%s;",
284 urlencode($criteriaValue)
289 // Remove last semicolon
290 $cacheKey = substr($cacheKey, 0, -1);
292 // Is the instance SearchCriteria?
293 if ($this instanceof SearchCriteria) {
294 // Check if 'limit' and 'skip' are in
295 if (((isset($onlyKeys['limit'])) && (isset($onlyKeys['skip']))) || (count($onlyKeys) == 0)) {
296 // Add limit and skip values
297 $cacheKey .= sprintf(";%%limit%%=%s;%%skip%%=%s",
304 // Return the cache key
309 * "Getter" for a cache key ('choice' type)
311 * @param $onlyKeys Only use these keys for a cache key
312 * @return $cacheKey The key suitable for the cache system
314 public function getCacheKeyChoice ($onlyKeys = array()) {
316 return $this->getCacheKey($onlyKeys, 'choice');
320 * "Getter" for a cache key ('exclude' type)
322 * @param $onlyKeys Only use these keys for a cache key
323 * @return $cacheKey The key suitable for the cache system
325 public function getCacheKeyExclude ($onlyKeys = array()) {
327 return $this->getCacheKey($onlyKeys, 'exclude');
331 * Count the criteria, e.g. useful to find out if a database query has no
332 * limitation (search criteria).
334 * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
335 * @return $count Count of all criteria entries
337 public final function count ($criteriaType = 'default') {
339 return count($this->criteria[$criteriaType]);
343 * Count 'choice' criteria, e.g. useful to find out if a database query
344 * has no limitation (search criteria).
346 * @return $count Count of all criteria entries
348 public final function countChoice () {
349 return $this->count('choice');
353 * Count 'exclude' criteria, e.g. useful to find out if a database query
354 * has no limitation (search criteria).
356 * @return $count Count of all criteria entries
358 public final function countExclude () {
359 return $this->count('exclude');