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 {
26 * Wrapper class name stored in config entry
28 private $wrapperConfigEntry = '';
33 private $criteria = array();
35 * Protected constructor
37 * @param $className Name of the class
40 protected function __construct ($className) {
41 // Call parent constructor
42 parent::__construct($className);
46 * Setter for wrapper class name
48 * @param $wrapperConfigEntry Configuration entry which hold the wrapper class' name
51 public final function setWrapperConfigEntry ($wrapperConfigEntry) {
52 $this->wrapperConfigEntry = (string) $wrapperConfigEntry;
56 * Getter for wrapper class name
58 * @return $wrapperConfigEntry Configuration entry which hold the wrapper class' name
60 public final function getWrapperConfigEntry () {
61 return $this->wrapperConfigEntry;
65 * Getter for criteria array
69 public final function getCriteriaArray () {
70 return $this->criteria;
74 * Add criteria, this method converts dashes to underscores because dashes
75 * are not valid for criteria keys.
77 * @param $criteriaKey Criteria key
78 * @param $criteriaValue Criteria value
81 public final function addCriteria ($criteriaKey, $criteriaValue) {
82 $this->criteria[$this->convertDashesToUnderscores($criteriaKey)] = (string)$criteriaValue;
86 * Add configured criteria
88 * @param $criteriaKey Criteria key
89 * @param $configEntry Configuration entry
92 public final function addConfiguredCriteria ($criteriaKey, $configEntry) {
93 // Add the configuration entry as a criteria
94 $value = $this->getConfigInstance()->getConfigEntry($configEntry);
95 $this->addCriteria($criteriaKey, $value);
99 * Get criteria element or null if not found
101 * @param $criteriaKey The requested criteria key
102 * @return $value Whether the value of the critera or null
104 public function getCriteriaElemnent ($criteriaKey) {
105 // Convert dashes to underscore
106 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
109 //* NOISY-DEBUG: */ $this->debugOutput('CRITERIA: criteriaKey=' . $criteriaKey . ',criteria()=' . count($this->criteria));
111 // Default is not found
114 // Is the criteria there?
115 if (isset($this->criteria[$criteriaKey])) {
117 $value = $this->criteria[$criteriaKey];
125 * Checks whether given array entry matches
127 * @param $entryArray Array with the entries to find
128 * @return $matches Whether the entry matches or not
130 public function ifEntryMatches (array $entryArray) {
131 // First nothing matches and nothing is counted
135 // Walk through all entries
136 foreach ($entryArray as $key => $entry) {
137 // Convert dashes to underscore
138 $key = $this->convertDashesToUnderscores($key);
140 // Then walk through all search criteria
141 foreach ($this->criteria as $criteriaKey => $criteriaValue) {
142 // Convert dashes to underscore
143 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
145 // Is the element found and does it match?
146 if (($key == $criteriaKey) && ($criteriaValue == $entry)) {
147 // Then count this one up
153 // Now check if expected criteria counts match
154 $matches = ($counted == count($this->criteria));
161 * "Getter" for a cache key
163 * @param $onlyKeys Only use these keys for a cache key
164 * @return $cacheKey The key suitable for the cache system
166 public function getCacheKey ($onlyKeys = array()) {
167 // Initialize the key
170 // Now walk through all criterias
171 foreach ($this->criteria as $criteriaKey => $criteriaValue) {
172 // Convert dashes to underscore
173 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
175 // Is the value in array or is $onlyKeys empty?
176 if ((isset($onlyKeys[$criteriaKey])) || (count($onlyKeys) == 0)) {
177 // Add the value URL encoded to avoid any trouble with special characters
178 $cacheKey .= sprintf("%s=%s;",
180 urlencode($criteriaValue)
185 // Remove last semicolon
186 $cacheKey = substr($cacheKey, 0, -1);
188 // Is the instance SearchCriteria?
189 if ($this instanceof SearchCriteria) {
190 // Check if 'limit' and 'skip' are in
191 if (((isset($onlyKeys['limit'])) && (isset($onlyKeys['skip']))) || (count($onlyKeys) == 0)) {
192 // Add limit and skip values
193 $cacheKey .= sprintf(";%%limit%%=%s;%%skip%%=%s",
200 // Return the cache key
205 * Count the criteria, e.g. useful to find out if a database query has no limitation (search criteria)
207 * @return $count Count of all criteria entries
209 public final function count () {
211 return count($this->criteria);