3 namespace Org\Mxchange\CoreFramework\Criteria\Search;
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;
11 use \InvalidArgumentException;
14 * Search criteria for e.g. searching in databases. Do not use this class if
15 * you are looking for a ship or company, or what ever. Instead use this class
16 * for looking in storages like the database.
18 * @author Roland Haeder <webmaster@shipsimu.org>
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.shipsimu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 class SearchCriteria extends BaseCriteria implements LocalSearchCriteria {
41 private $criteria = [];
44 * Limitation for the search
49 * Skip these entries before using them
54 * Protected constructor
58 private function __construct () {
59 // Call parent constructor
60 parent::__construct(__CLASS__);
64 * Create an instance of this class
66 * @return $criteriaInstance An instance of this criteria
68 public static final function createSearchCriteria () {
70 $criteriaInstance = new SearchCriteria();
72 // Return this instance
73 return $criteriaInstance;
79 * @param $limit Search limit
81 * @todo Find a nice casting here. (int) allows until and including 32766.
83 public final function setLimit (int $limit) {
84 $this->limit = $limit;
88 * "Setter" for limit from a configuration entry
90 * @param $configEntry The configuration entry which hold a number as limit
93 public final function setConfiguredLimit (string $configEntry) {
94 // Get the limit from config entry and set it
95 $limit = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configEntry);
96 $this->setLimit($limit);
102 * @return $limit Search limit
104 public final function getLimit () {
111 * @param $skip Search skip
113 * @todo Find a nice casting here. (int) allows until and including 32766.
115 public final function setSkip (int $skip) {
122 * @return $skip Search skip
124 public final function getSkip () {
129 * Checks whether the given key/value pair is matching with 'default' and one of 'choice' and
130 * never with in 'exclude'.
132 * @param $key Key element to check
133 * @param $value Value to check
134 * @param $separator Separator for "exploding" $value (default: ',')
135 * @return $isMatching Whether the key/value is matching or excluded
136 * @throws InvalidArgumentException If a parameter is invalid
138 public function isCriteriaMatching (string $key, $value, string $separator = ',') {
139 // $key/$value cannot be array/NULL/bool, value can be NULL but then NULL must be loocked for
140 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SEARCH-CRITERIA: key=%s,value[]=%s,separator=%s - CALLED!', $key, gettype($value), $separator));
143 throw new InvalidArgumentException('Parameter "key" is empty');
144 } elseif (is_array($value) || is_bool($value) || is_bool($value) || is_object($value) || is_resource($value)) {
146 throw new InvalidArgumentException(sprintf('value[]=%s is not supported/valid', gettype($value)));
147 } elseif (empty($separator)) {
149 throw new InvalidArgumentException('Parameter "separator" is empty');
153 $valueArray = explode($separator, $value);
155 // Get 'default' search value
156 $searchDefault = $this->getCriteriaElemnent($key);
159 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault);
163 ($searchDefault !== false) &&
164 ($searchDefault == $value)
166 is_null($searchDefault) &&
170 $searchDefault === false
174 // Get 'choice' search value (can be NULL or $separator-separated string)
175 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault . ',isMatching=' . intval($isMatching));
176 $searchChoice = $this->getCriteriaChoiceElemnent($key);
178 // May be false or array
179 assert(($searchChoice === false) || (is_array($searchChoice)));
182 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[' . gettype($searchChoice) . ']=' . print_r($searchChoice, true));
183 if ((is_array($searchChoice)) && (count($valueArray) == 1)) {
184 // $value is a single-search value, so use in_[]
185 $isMatching = ((($isMatching === true) || (($searchDefault === false) && (!is_null($value)))) && (in_array($value, $searchChoice)));
188 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - SINGLE-MATCH');
189 } elseif ((is_array($searchChoice)) && (count($valueArray) > 1)) {
191 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[]=' . count($valueArray) . ',isMatching=' . intval($isMatching));
193 // $value is choice-search value, so check all entries
194 $isMatching = (($isMatching === true) || (($searchDefault === false) && (!is_null($value))));
196 foreach ($valueArray as $idx => $match) {
198 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: match=' . $match . ',count(searchChoice)=' . count($searchChoice));
200 // Is it found? (one is okay)
201 $isMatching = (($isMatching === true) && (in_array($match, $searchChoice)));
204 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: match=' . $match . ',isMatching=' . intval($isMatching));
208 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[]=' . count($valueArray) . ',idx=' . $idx . ',isMatching=' . intval($isMatching) . ' - CHOICE-MATCH');
210 // Choice-match is false
212 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - false-MATCH');
215 // Get 'exclude' search value
216 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',isMatching=' . intval($isMatching));
217 $searchExclude = $this->getCriteriaExcludeElemnent($key);
220 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaExcludeElement(' . $key . ')[' . gettype($searchExclude) . ']=' . $searchExclude);
226 $searchExclude === false
233 $searchExclude !== false
235 $searchExclude !== $value
242 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - EXIT!');