Made lower to upper case:
[core.git] / inc / classes / main / criteria / class_BaseCriteria.php
1 <?php
2 /**
3  * A general crtieria class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
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
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class BaseCriteria extends BaseFrameworkSystem implements Criteria {
25         /**
26          * Wrapper class name stored in config entry
27          */
28         private $wrapperConfigEntry = '';
29
30         /**
31          * Criteria to handle
32          */
33         private $criteria = array(
34                 // Default
35                 'default' => array(),
36                 // Choice
37                 'choice'  => array(),
38                 // .. and exclude
39                 'exclude' => array(),
40         );
41
42         /**
43          * Protected constructor
44          *
45          * @param       $className      Name of the class
46          * @return      void
47          */
48         protected function __construct ($className) {
49                 // Call parent constructor
50                 parent::__construct($className);
51         }
52
53         /**
54          * Checks whether given key is set
55          *
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
59          */
60         public function isKeySet ($criteriaType, $criteriaKey) {
61                 // Make sure no 'my-' or 'my_' passes this point
62                 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE));
63
64                 // Determine it
65                 $isSet = isset($this->criteria[$criteriaType][$criteriaKey]);
66
67                 // Return it
68                 return $isSet;
69         }
70
71         /**
72          * Checks whether given key is set for 'choice' type
73          *
74          * @param       $criteriaKey    Criteria key
75          * @return      $isSet                  Whether key is set
76          */
77         public function isChoiceKeySet ($criteriaKey) {
78                 // Call inner method
79                 return $this->isKeySet('choice', $criteriaKey);
80         }
81
82         /**
83          * Checks whether given key is set for 'exclude' type
84          *
85          * @param       $criteriaKey    Criteria key
86          * @return      $isSet                  Whether key is set
87          */
88         public function isExcludeKeySet ($criteriaKey) {
89                 // Call inner method
90                 return $this->isKeySet('exclude', $criteriaKey);
91         }
92
93         /**
94          * Setter for wrapper class name
95          *
96          * @param       $wrapperConfigEntry             Configuration entry which hold the wrapper class' name
97          * @return      void
98          */
99         public final function setWrapperConfigEntry ($wrapperConfigEntry) {
100                 $this->wrapperConfigEntry = (string) $wrapperConfigEntry;
101         }
102
103         /**
104          * Getter for wrapper class name
105          *
106          * @return      $wrapperConfigEntry             Configuration entry which hold the wrapper class' name
107          */
108         public final function getWrapperConfigEntry () {
109                 return $this->wrapperConfigEntry;
110         }
111
112         /**
113          * Getter for criteria array
114          *
115          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
116          * @return      $criteria
117          */
118         public final function getCriteriaArray ($criteriaType = 'default') {
119                 return $this->criteria[$criteriaType];
120         }
121
122         /**
123          * Getter for criteria array 'choice' type
124          *
125          * @return      $criteria
126          */
127         public final function getCriteriaChoiceArray () {
128                 return $this->getCriteriaArray('choice');
129         }
130
131         /**
132          * Getter for criteria array 'exclude' type
133          *
134          * @return      $criteria
135          */
136         public final function getCriteriaExcludeArray () {
137                 return $this->getCriteriaArray('exclude');
138         }
139
140         /**
141          * Unsets a criteria key from all criteria types
142          *
143          * @param       $criteriaKey    Criteria key to unset
144          * @return      void
145          */
146         public final function unsetCriteria ($criteriaKey) {
147                 // Make sure no 'my-' or 'my_' passes this point
148                 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE));
149
150                 // Convert dashes to underscore
151                 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
152
153                 // "Walk" through all criterias
154                 foreach ($this->criteria as $criteriaType => $dummy) {
155                         // Remove it
156                         unset($this->criteria[$criteriaType][$criteriaKey]);
157                 } // END - foreach
158         }
159
160         /**
161          * Add criteria, this method converts dashes to underscores because dashes
162          * are not valid for criteria keys.
163          *
164          * @param       $criteriaKey    Criteria key
165          * @param       $criteriaValue  Criteria value
166          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
167          * @return      void
168          */
169         public final function addCriteria ($criteriaKey, $criteriaValue, $criteriaType = 'default') {
170                 // Make sure no 'my-' or 'my_' passes this point
171                 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE) && (!is_bool($criteriaValue)));
172
173                 // Convert dashes to underscore
174                 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
175
176                 // Debug message
177                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
178
179                 // Is it already there?
180                 if ($this->isKeySet($criteriaType, $criteriaKey)) {
181                         // Append it
182                         $this->criteria[$criteriaType][$criteriaKey] .= ',' . (string) $criteriaValue;
183                 } else {
184                         // Add it
185                         $this->criteria[$criteriaType][$criteriaKey] = (string) $criteriaValue;
186                 }
187         }
188
189         /**
190          * Add "choice" criteria, this method converts dashes to underscores because
191          * dashes are not valid for criteria keys.
192          *
193          * @param       $criteriaKey    Criteria key
194          * @param       $criteriaValue  Criteria value
195          * @return      void
196          */
197         public final function addChoiceCriteria ($criteriaKey, $criteriaValue) {
198                 // Make sure no 'my-' or 'my_' passes this point
199                 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE) && (!is_bool($criteriaValue)));
200
201                 // Debug message
202                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
203
204                 // Add it
205                 array_push($this->criteria['choice'][$this->convertDashesToUnderscores($criteriaKey)], (string) $criteriaValue);
206         }
207
208         /**
209          * Add "exclude" criteria, this method converts dashes to underscores because
210          * dashes are not valid for criteria keys.
211          *
212          * @param       $criteriaKey    Criteria key
213          * @param       $criteriaValue  Criteria value
214          * @return      void
215          */
216         public final function addExcludeCriteria ($criteriaKey, $criteriaValue) {
217                 // Add it with generic method
218                 $this->addCriteria($criteriaKey, $criteriaValue, 'exclude');
219         }
220
221         /**
222          * Add configured criteria
223          *
224          * @param       $criteriaKey    Criteria key
225          * @param       $configEntry    Configuration entry
226          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
227          * @return      void
228          */
229         public final function addConfiguredCriteria ($criteriaKey, $configEntry, $criteriaType = 'default') {
230                 // Add the configuration entry as a criteria
231                 $value = $this->getConfigInstance()->getConfigEntry($configEntry);
232                 $this->addCriteria($criteriaKey, $value, $criteriaType);
233         }
234
235         /**
236          * Get criteria element or FALSE if not found
237          *
238          * @param       $criteriaKey    The requested criteria key
239          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
240          * @return      $value                  Whether the value of the critera or FALSE
241          */
242         public function getCriteriaElemnent ($criteriaKey, $criteriaType = 'default') {
243                 // Make sure no 'my-' or 'my_' passes this point
244                 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE));
245
246                 // Convert dashes to underscore
247                 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
248
249                 // Debug message
250                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteria()=' . count($this->criteria[$criteriaType]));
251
252                 // Default is not found
253                 $value = FALSE;
254
255                 // Is the criteria there?
256                 if ($this->isKeySet($criteriaType, $criteriaKey)) {
257                         // Then use it
258                         $value = $this->criteria[$criteriaType][$criteriaKey];
259                 } // END - if
260
261                 // Return the value
262                 return $value;
263         }
264
265         /**
266          * Get criteria element or FALSE if not found for 'choice' type
267          *
268          * @param       $criteriaKey    The requested criteria key
269          * @return      $value                  Whether the value of the critera or FALSE
270          */
271         public function getCriteriaChoiceElemnent ($criteriaKey) {
272                 // Call inner method
273                 return $this->getCriteriaElemnent($criteriaKey, 'choice');
274         }
275
276         /**
277          * Get criteria element or FALSE if not found for 'exclude' type
278          *
279          * @param       $criteriaKey    The requested criteria key
280          * @return      $value                  Whether the value of the critera or FALSE
281          */
282         public function getCriteriaExcludeElemnent ($criteriaKey) {
283                 // Call inner method
284                 return $this->getCriteriaElemnent($criteriaKey, 'exclude');
285         }
286
287         /**
288          * Checks whether given array entry matches
289          *
290          * @param       $entryArray             Array with the entries to find
291          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
292          * @return      $matches                Whether the entry matches or not
293          */
294         public function ifEntryMatches (array $entryArray, $criteriaType = 'default') {
295                 // First nothing matches and nothing is counted
296                 $matches = FALSE;
297                 $counted = 0;
298
299                 // Walk through all entries
300                 foreach ($entryArray as $key => $entry) {
301                         // Make sure no 'my-' or 'my_' passes this point
302                         assert((strpos($key, 'my-') === FALSE) && (strpos($key, 'my_') === FALSE));
303
304                         // Convert dashes to underscore
305                         $key = $this->convertDashesToUnderscores($key);
306
307                         // Then walk through all search criteria
308                         foreach ($this->criteria[$criteriaType] as $criteriaKey => $criteriaValue) {
309                                 // Make sure no 'my-' or 'my_' passes this point
310                                 assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE) && (!is_bool($criteriaValue)));
311
312                                 // Convert dashes to underscore
313                                 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
314
315                                 // Is the element found and does it match?
316                                 if (($key == $criteriaKey) && ($criteriaValue == $entry)) {
317                                         // Then count this one up
318                                         $counted++;
319                                 } // END - if
320                         } // END - foreach
321                 } // END - foreach
322
323                 // Now check if expected criteria counts match
324                 $matches = ($counted == count($this->criteria[$criteriaType]));
325
326                 // Return the result
327                 return $matches;
328         }
329
330         /**
331          * Checks whether given array 'choice' entry matches
332          *
333          * @param       $entryArray             Array with the entries to find
334          * @return      $matches                Whether the entry matches or not
335          */
336         public function ifChoiceMatches (array $entryArray) {
337                 // Call inner method
338                 return $this->ifEntryMatches($entryArray, 'choice');
339         }
340
341         /**
342          * Checks whether given array 'exclude' entry matches
343          *
344          * @param       $entryArray             Array with the entries to find
345          * @return      $matches                Whether the entry matches or not
346          */
347         public function ifExcludeMatches (array $entryArray) {
348                 // Call inner method
349                 return $this->ifEntryMatches($entryArray, 'exclude');
350         }
351
352         /**
353          * "Getter" for a cache key
354          *
355          * @param       $onlyKeys       Only use these keys for a cache key
356          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
357          * @return      $cacheKey       The key suitable for the cache system
358          */
359         public function getCacheKey ($onlyKeys = array(), $criteriaType = 'default') {
360                 // Debug message
361                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($this->__toString() . ': criteriaType=' . $criteriaType . ',count()=' . count($this->criteria));
362
363                 // Make sure the criteria is there
364                 assert((isset($this->criteria[$criteriaType])) && (is_array($this->criteria[$criteriaType])));
365
366                 // Initialize the key
367                 $cacheKey = '';
368
369                 // Now walk through all criterias
370                 foreach ($this->criteria[$criteriaType] as $criteriaKey => $criteriaValue) {
371                         // Make sure no 'my-' or 'my_' passes this point
372                         assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE) && (!is_bool($criteriaValue)));
373
374                         // Convert dashes to underscore
375                         $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
376
377                         // Is the value in array or is $onlyKeys empty?
378                         if ((isset($onlyKeys[$criteriaKey])) || (count($onlyKeys) == 0)) {
379                                 // Add the value URL encoded to avoid any trouble with special characters
380                                 $cacheKey .= sprintf("%s=%s;",
381                                         $criteriaKey,
382                                         urlencode($criteriaValue)
383                                 );
384                         } // END - if
385                 } // END - foreach
386
387                 // Remove last semicolon
388                 $cacheKey = substr($cacheKey, 0, -1);
389
390                 // Is the instance SearchCriteria?
391                 if ($this instanceof SearchCriteria) {
392                         // Check if 'limit' and 'skip' are in
393                         if (((isset($onlyKeys['limit'])) && (isset($onlyKeys['skip']))) || (count($onlyKeys) == 0)) {
394                                 // Add limit and skip values
395                                 $cacheKey .= sprintf(";%%limit%%=%s;%%skip%%=%s",
396                                         $this->getLimit(),
397                                         $this->getSkip()
398                                 );
399                         } // END - if
400                 } // END - if
401
402                 // Return the cache key
403                 return $cacheKey;
404         }
405
406         /**
407          * "Getter" for a cache key ('choice' type)
408          *
409          * @param       $onlyKeys       Only use these keys for a cache key
410          * @return      $cacheKey       The key suitable for the cache system
411          */
412         public function getCacheKeyChoice ($onlyKeys = array()) {
413                 // Call inner method
414                 return $this->getCacheKey($onlyKeys, 'choice');
415         }
416
417         /**
418          * "Getter" for a cache key ('exclude' type)
419          *
420          * @param       $onlyKeys       Only use these keys for a cache key
421          * @return      $cacheKey       The key suitable for the cache system
422          */
423         public function getCacheKeyExclude ($onlyKeys = array()) {
424                 // Call inner method
425                 return $this->getCacheKey($onlyKeys, 'exclude');
426         }
427
428         /**
429          * Count the criteria, e.g. useful to find out if a database query has no
430          * limitation (search criteria).
431          *
432          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
433          * @return      $count  Count of all criteria entries
434          */
435         public final function count ($criteriaType = 'default') {
436                 // Return it
437                 return count($this->criteria[$criteriaType]);
438         }
439
440         /**
441          * Count 'choice' criteria, e.g. useful to find out if a database query
442          * has no limitation (search criteria).
443          *
444          * @return      $count  Count of all criteria entries
445          */
446         public final function countChoice () {
447                 return $this->count('choice');
448         }
449
450         /**
451          * Count 'exclude' criteria, e.g. useful to find out if a database query
452          * has no limitation (search criteria).
453          *
454          * @return      $count  Count of all criteria entries
455          */
456         public final function countExclude () {
457                 return $this->count('exclude');
458         }
459 }
460
461 // [EOF]
462 ?>