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