Continued:
[core.git] / inc / main / classes / criteria / search / class_SearchCriteria.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Criteria\Search;
4
5 // Import framework stuff
6 use CoreFramework\Criteria\BaseCriteria;
7
8 /**
9  * Search criteria for e.g. searching in databases. Do not use this class if
10  * you are looking for a ship or company, or what ever. Instead use this class
11  * for looking in storages like the database.
12  *
13  * @author              Roland Haeder <webmaster@shipsimu.org>
14  * @version             0.0.0
15  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16  * @license             GNU GPL 3.0 or any newer version
17  * @link                http://www.shipsimu.org
18  *
19  * This program is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  */
32 class SearchCriteria extends BaseCriteria implements LocalSearchCriteria {
33         /**
34          * Criteria to handle
35          */
36         private $criteria = array();
37
38         /**
39          * Limitation for the search
40          */
41         private $limit = 0;
42
43         /**
44          * Skip these entries before using them
45          */
46         private $skip = 0;
47
48         /**
49          * Protected constructor
50          *
51          * @return      void
52          */
53         protected function __construct () {
54                 // Call parent constructor
55                 parent::__construct(__CLASS__);
56         }
57
58         /**
59          * Create an instance of this class
60          *
61          * @return      $criteriaInstance       An instance of this criteria
62          */
63         public static final function createSearchCriteria () {
64                 // Get a new instance
65                 $criteriaInstance = new SearchCriteria();
66
67                 // Return this instance
68                 return $criteriaInstance;
69         }
70
71         /**
72          * Setter for limit
73          *
74          * @param       $limit  Search limit
75          * @return      void
76          * @todo        Find a nice casting here. (int) allows until and including 32766.
77          */
78         public final function setLimit ($limit) {
79                 $this->limit = $limit;
80         }
81
82         /**
83          * "Setter" for limit from a configuration entry
84          *
85          * @param       $configEntry    The configuration entry which hold a number as limit
86          * @return      void
87          */
88         public final function setConfiguredLimit ($configEntry) {
89                 // Get the limit from config entry and set it
90                 $limit = $this->getConfigInstance()->getConfigEntry($configEntry);
91                 $this->setLimit($limit);
92         }
93
94         /**
95          * Getter for limit
96          *
97          * @return      $limit  Search limit
98          */
99         public final function getLimit () {
100                 return $this->limit;
101         }
102
103         /**
104          * Setter for skip
105          *
106          * @param       $skip   Search skip
107          * @return      void
108          * @todo        Find a nice casting here. (int) allows until and including 32766.
109          */
110         public final function setSkip ($skip) {
111                 $this->skip = $skip;
112         }
113
114         /**
115          * Getter for skip
116          *
117          * @return      $skip   Search skip
118          */
119         public final function getSkip () {
120                 return $this->skip;
121         }
122
123         /**
124          * Checks whether the given key/value pair is matching with 'default' and one of 'choice' and
125          * never with in 'exclude'.
126          *
127          * @param       $key                    Key element to check
128          * @param       $value                  Value to check
129          * @param       $separator              Separator for "exploding" $value (default: ',')
130          * @return      $isMatching             Whether the key/value is matching or excluded
131          */
132         public function isCriteriaMatching ($key, $value, $separator = ',') {
133                 // $key/$value cannot be array/NULL/bool, value can be NULL but then NULL must be loocked for
134                 assert((!is_array($value)) && (!is_bool($value)) && (!is_array($key)) && (!is_null($key)) && (!is_bool($key)));
135
136                 // "Explode" value
137                 $valueArray = explode($separator, $value);
138
139                 // Debug message
140                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ' - CALLED!');
141
142                 // Get 'default' search value
143                 $searchDefault = $this->getCriteriaElemnent($key);
144
145                 // Debug message
146                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault);
147
148                 // 'default' check
149                 $isMatching = (((($searchDefault !== FALSE) && ($searchDefault == $value)) || ((is_null($searchDefault)) && (is_null($value)))) || ($searchDefault === FALSE));
150
151                 // Debug message
152                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault . ',isMatching=' . intval($isMatching));
153
154                 // Get 'choice' search value (can be NULL or $separator-separated string)
155                 $searchChoice = $this->getCriteriaChoiceElemnent($key);
156
157                 // May be FALSE or array
158                 assert(($searchChoice === FALSE) || (is_array($searchChoice)));
159
160                 // Debug message
161                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[' . gettype($searchChoice) . ']=' . print_r($searchChoice, TRUE));
162
163                 // 'choice' check
164                 if ((is_array($searchChoice)) && (count($valueArray) == 1)) {
165                         // $value is a single-search value, so use in_array()
166                         $isMatching = ((($isMatching === TRUE) || (($searchDefault === FALSE) && (!is_null($value)))) && (in_array($value, $searchChoice)));
167
168                         // Debug message
169                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - SINGLE-MATCH');
170                 } elseif ((is_array($searchChoice)) && (count($valueArray) > 1)) {
171                         // Debug message
172                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',valueArray()=' . count($valueArray) . ',isMatching=' . intval($isMatching));
173
174                         // $value is choice-search value, so check all entries
175                         $isMatching = (($isMatching === TRUE) || (($searchDefault === FALSE) && (!is_null($value))));
176                         $idx = 0;
177                         foreach ($valueArray as $idx => $match) {
178                                 // Debug message
179                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: match=' . $match . ',count(searchChoice)=' . count($searchChoice));
180
181                                 // Is it found? (one is okay)
182                                 $isMatching = (($isMatching === TRUE) && (in_array($match, $searchChoice)));
183
184                                 // Debug message
185                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: match=' . $match . ',isMatching=' . intval($isMatching));
186                         } // END - foreach
187
188                         // Debug message
189                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',valueArray()=' . count($valueArray) . ',idx=' . $idx . ',isMatching=' . intval($isMatching) . ' - CHOICE-MATCH');
190                 } else {
191                         // Choice-match is FALSE
192                         // Debug message
193                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - FALSE-MATCH');
194                 }
195
196                 // Debug message
197                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',isMatching=' . intval($isMatching));
198
199                 // Get 'exclude' search value
200                 $searchExclude = $this->getCriteriaExcludeElemnent($key);
201
202                 // Debug message
203                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaExcludeElement(' . $key . ')[' . gettype($searchExclude) . ']=' . $searchExclude);
204
205                 // 'exclude' check
206                 $isMatching = (
207                         (
208                                 (
209                                         $isMatching === TRUE
210                                 ) && (
211                                         $searchExclude === FALSE
212                                 )
213                         ) || (
214                                 (
215                                         (
216                                                 $isMatching === TRUE
217                                         ) && (
218                                                 $searchExclude !== FALSE
219                                         ) && (
220                                                 $searchExclude !== $value
221                                         )
222                                 )
223                         )
224                 );
225
226                 // Return result
227                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - EXIT!');
228                 return $isMatching;
229         }
230
231 }