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