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;
9 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
12 use \InvalidArgumentException;
15 * Search criteria for e.g. searching in databases. Do not use this class if
16 * you are looking for a ship or company, or what ever. Instead use this class
17 * for looking in storages like the database.
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 class SearchCriteria extends BaseCriteria implements LocalSearchCriteria {
42 private $criteria = [];
45 * Limitation for the search
50 * Skip these entries before using them
55 * Protected constructor
59 private function __construct () {
60 // Call parent constructor
61 parent::__construct(__CLASS__);
65 * Create an instance of this class
67 * @return $criteriaInstance An instance of this criteria
69 public static final function createSearchCriteria () {
71 $criteriaInstance = new SearchCriteria();
73 // Return this instance
74 return $criteriaInstance;
80 * @param $limit Search limit
82 * @todo Find a nice casting here. (int) allows until and including 32766.
84 public final function setLimit (int $limit) {
85 $this->limit = $limit;
89 * "Setter" for limit from a configuration entry
91 * @param $configEntry The configuration entry which hold a number as limit
94 public final function setConfiguredLimit (string $configEntry) {
95 // Get the limit from config entry and set it
96 $limit = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configEntry);
97 $this->setLimit($limit);
103 * @return $limit Search limit
105 public final function getLimit () {
112 * @param $skip Search skip
114 * @todo Find a nice casting here. (int) allows until and including 32766.
116 public final function setSkip (int $skip) {
123 * @return $skip Search skip
125 public final function getSkip () {
130 * Checks whether the given key/value pair is matching with 'default' and one of 'choice' and
131 * never with in 'exclude'.
133 * @param $key Key element to check
134 * @param $value Value to check
135 * @param $separator Separator for "exploding" $value (default: ',')
136 * @return $isMatching Whether the key/value is matching or excluded
137 * @throws InvalidArgumentException If a parameter is invalid
139 public function isCriteriaMatching (string $key, $value, string $separator = ',') {
140 // $key/$value cannot be array/NULL/bool, value can be NULL but then NULL must be loocked for
141 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SEARCH-CRITERIA: key=%s,value[]=%s,separator=%s - CALLED!', $key, gettype($value), $separator));
144 throw new InvalidArgumentException('Parameter "key" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
145 } elseif (is_array($value) || is_bool($value) || is_bool($value) || is_object($value) || is_resource($value)) {
147 throw new InvalidArgumentException(sprintf('value[]=%s is not supported/valid', gettype($value)));
148 } elseif (empty($separator)) {
150 throw new InvalidArgumentException('Parameter "separator" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
154 $valueArray = explode($separator, $value);
156 // Get 'default' search value
157 $searchDefault = $this->getCriteriaElemnent($key);
160 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault);
164 ($searchDefault !== false) &&
165 ($searchDefault == $value)
167 is_null($searchDefault) &&
171 $searchDefault === false
175 // Get 'choice' search value (can be NULL or $separator-separated string)
176 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault . ',isMatching=' . intval($isMatching));
177 $searchChoice = $this->getCriteriaChoiceElemnent($key);
179 // May be false or array
180 assert(($searchChoice === false) || (is_array($searchChoice)));
183 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[' . gettype($searchChoice) . ']=' . print_r($searchChoice, true));
184 if ((is_array($searchChoice)) && (count($valueArray) == 1)) {
185 // $value is a single-search value, so use in_[]
186 $isMatching = ((($isMatching === true) || (($searchDefault === false) && (!is_null($value)))) && (in_array($value, $searchChoice)));
189 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - SINGLE-MATCH');
190 } elseif ((is_array($searchChoice)) && (count($valueArray) > 1)) {
192 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[]=' . count($valueArray) . ',isMatching=' . intval($isMatching));
194 // $value is choice-search value, so check all entries
195 $isMatching = (($isMatching === true) || (($searchDefault === false) && (!is_null($value))));
197 foreach ($valueArray as $idx => $match) {
199 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: match=' . $match . ',count(searchChoice)=' . count($searchChoice));
201 // Is it found? (one is okay)
202 $isMatching = (($isMatching === true) && (in_array($match, $searchChoice)));
205 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: match=' . $match . ',isMatching=' . intval($isMatching));
209 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[]=' . count($valueArray) . ',idx=' . $idx . ',isMatching=' . intval($isMatching) . ' - CHOICE-MATCH');
211 // Choice-match is false
213 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - false-MATCH');
216 // Get 'exclude' search value
217 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',isMatching=' . intval($isMatching));
218 $searchExclude = $this->getCriteriaExcludeElemnent($key);
221 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaExcludeElement(' . $key . ')[' . gettype($searchExclude) . ']=' . $searchExclude);
227 $searchExclude === false
234 $searchExclude !== false
236 $searchExclude !== $value
243 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - EXIT!');