MINOR: Missing empty line
[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 {
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
35         /**
36          * Protected constructor
37          *
38          * @param       $className      Name of the class
39          * @return      void
40          */
41         protected function __construct ($className) {
42                 // Call parent constructor
43                 parent::__construct($className);
44         }
45
46         /**
47          * Setter for wrapper class name
48          *
49          * @param       $wrapperConfigEntry             Configuration entry which hold the wrapper class' name
50          * @return      void
51          */
52         public final function setWrapperConfigEntry ($wrapperConfigEntry) {
53                 $this->wrapperConfigEntry = (string) $wrapperConfigEntry;
54         }
55
56         /**
57          * Getter for wrapper class name
58          *
59          * @return      $wrapperConfigEntry             Configuration entry which hold the wrapper class' name
60          */
61         public final function getWrapperConfigEntry () {
62                 return $this->wrapperConfigEntry;
63         }
64
65         /**
66          * Getter for criteria array
67          *
68          * @return      $criteria
69          */
70         public final function getCriteriaArray () {
71                 return $this->criteria;
72         }
73
74         /**
75          * Add criteria, this method converts dashes to underscores because dashes
76          * are not valid for criteria keys.
77          *
78          * @param       $criteriaKey    Criteria key
79          * @param       $criteriaValue  Criteria value
80          * @return      void
81          */
82         public final function addCriteria ($criteriaKey, $criteriaValue) {
83                 // Debug message
84                 if (strpos($criteriaKey, 'my-') !== false) $this->debugBackTrace('criteriaKey=' . $criteriaKey);
85                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
86
87                 // Add it
88                 $this->criteria[$this->convertDashesToUnderscores($criteriaKey)] = (string)$criteriaValue;
89         }
90
91         /**
92          * Add configured criteria
93          *
94          * @param       $criteriaKey    Criteria key
95          * @param       $configEntry    Configuration entry
96          * @return      void
97          */
98         public final function addConfiguredCriteria ($criteriaKey, $configEntry) {
99                 // Add the configuration entry as a criteria
100                 $value = $this->getConfigInstance()->getConfigEntry($configEntry);
101                 $this->addCriteria($criteriaKey, $value);
102         }
103
104         /**
105          * Get criteria element or null if not found
106          *
107          * @param       $criteriaKey    The requested criteria key
108          * @return      $value                  Whether the value of the critera or null
109          */
110         public function getCriteriaElemnent ($criteriaKey) {
111                 // Convert dashes to underscore
112                 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
113
114                 // Debug message
115                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CRITERIA: criteriaKey=' . $criteriaKey . ',criteria()=' . count($this->criteria));
116
117                 // Default is not found
118                 $value = NULL;
119
120                 // Is the criteria there?
121                 if (isset($this->criteria[$criteriaKey])) {
122                         // Then use it
123                         $value = $this->criteria[$criteriaKey];
124                 } // END - if
125
126                 // Return the value
127                 return $value;
128         }
129
130         /**
131          * Checks whether given array entry matches
132          *
133          * @param       $entryArray             Array with the entries to find
134          * @return      $matches                Whether the entry matches or not
135          */
136         public function ifEntryMatches (array $entryArray) {
137                 // First nothing matches and nothing is counted
138                 $matches = false;
139                 $counted = 0;
140
141                 // Walk through all entries
142                 foreach ($entryArray as $key => $entry) {
143                         // Convert dashes to underscore
144                         $key = $this->convertDashesToUnderscores($key);
145
146                         // Then walk through all search criteria
147                         foreach ($this->criteria as $criteriaKey => $criteriaValue) {
148                                 // Convert dashes to underscore
149                                 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
150
151                                 // Is the element found and does it match?
152                                 if (($key == $criteriaKey) && ($criteriaValue == $entry)) {
153                                         // Then count this one up
154                                         $counted++;
155                                 } // END - if
156                         } // END - foreach
157                 } // END - foreach
158
159                 // Now check if expected criteria counts match
160                 $matches = ($counted == count($this->criteria));
161
162                 // Return the result
163                 return $matches;
164         }
165
166         /**
167          * "Getter" for a cache key
168          *
169          * @param       $onlyKeys       Only use these keys for a cache key
170          * @return      $cacheKey       The key suitable for the cache system
171          */
172         public function getCacheKey ($onlyKeys = array()) {
173                 // Initialize the key
174                 $cacheKey = '';
175
176                 // Now walk through all criterias
177                 foreach ($this->criteria as $criteriaKey => $criteriaValue) {
178                         // Convert dashes to underscore
179                         $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
180
181                         // Is the value in array or is $onlyKeys empty?
182                         if ((isset($onlyKeys[$criteriaKey])) || (count($onlyKeys) == 0)) {
183                                 // Add the value URL encoded to avoid any trouble with special characters
184                                 $cacheKey .= sprintf("%s=%s;",
185                                         $criteriaKey,
186                                         urlencode($criteriaValue)
187                                 );
188                         } // END - if
189                 } // END - foreach
190
191                 // Remove last semicolon
192                 $cacheKey = substr($cacheKey, 0, -1);
193
194                 // Is the instance SearchCriteria?
195                 if ($this instanceof SearchCriteria) {
196                         // Check if 'limit' and 'skip' are in
197                         if (((isset($onlyKeys['limit'])) && (isset($onlyKeys['skip']))) || (count($onlyKeys) == 0)) {
198                                 // Add limit and skip values
199                                 $cacheKey .= sprintf(";%%limit%%=%s;%%skip%%=%s",
200                                         $this->getLimit(),
201                                         $this->getSkip()
202                                 );
203                         } // END - if
204                 } // END - if
205
206                 // Return the cache key
207                 return $cacheKey;
208         }
209
210         /**
211          * Count the criteria, e.g. useful to find out if a database query has no limitation (search criteria)
212          *
213          * @return      $count  Count of all criteria entries
214          */
215         public final function count () {
216                 // Return it
217                 return count($this->criteria);
218         }
219 }
220
221 // [EOF]
222 ?>