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