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