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