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