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 * 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.
15 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
34 class SearchCriteria extends BaseCriteria implements LocalSearchCriteria {
38 private $criteria = [];
41 * Limitation for the search
46 * Skip these entries before using them
51 * Protected constructor
55 protected function __construct () {
56 // Call parent constructor
57 parent::__construct(__CLASS__);
61 * Create an instance of this class
63 * @return $criteriaInstance An instance of this criteria
65 public static final function createSearchCriteria () {
67 $criteriaInstance = new SearchCriteria();
69 // Return this instance
70 return $criteriaInstance;
76 * @param $limit Search limit
78 * @todo Find a nice casting here. (int) allows until and including 32766.
80 public final function setLimit ($limit) {
81 $this->limit = $limit;
85 * "Setter" for limit from a configuration entry
87 * @param $configEntry The configuration entry which hold a number as limit
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);
99 * @return $limit Search limit
101 public final function getLimit () {
108 * @param $skip Search skip
110 * @todo Find a nice casting here. (int) allows until and including 32766.
112 public final function setSkip ($skip) {
119 * @return $skip Search skip
121 public final function getSkip () {
126 * Checks whether the given key/value pair is matching with 'default' and one of 'choice' and
127 * never with in 'exclude'.
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
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)));
139 $valueArray = explode($separator, $value);
142 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ' - CALLED!');
144 // Get 'default' search value
145 $searchDefault = $this->getCriteriaElemnent($key);
148 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault);
151 $isMatching = (((($searchDefault !== false) && ($searchDefault == $value)) || ((is_null($searchDefault)) && (is_null($value)))) || ($searchDefault === false));
154 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault . ',isMatching=' . intval($isMatching));
156 // Get 'choice' search value (can be NULL or $separator-separated string)
157 $searchChoice = $this->getCriteriaChoiceElemnent($key);
159 // May be false or array
160 assert(($searchChoice === false) || (is_array($searchChoice)));
163 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[' . gettype($searchChoice) . ']=' . print_r($searchChoice, true));
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)));
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)) {
174 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[]=' . count($valueArray) . ',isMatching=' . intval($isMatching));
176 // $value is choice-search value, so check all entries
177 $isMatching = (($isMatching === true) || (($searchDefault === false) && (!is_null($value))));
179 foreach ($valueArray as $idx => $match) {
181 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: match=' . $match . ',count(searchChoice)=' . count($searchChoice));
183 // Is it found? (one is okay)
184 $isMatching = (($isMatching === true) && (in_array($match, $searchChoice)));
187 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: match=' . $match . ',isMatching=' . intval($isMatching));
191 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[]=' . count($valueArray) . ',idx=' . $idx . ',isMatching=' . intval($isMatching) . ' - CHOICE-MATCH');
193 // Choice-match is false
195 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - false-MATCH');
199 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',isMatching=' . intval($isMatching));
201 // Get 'exclude' search value
202 $searchExclude = $this->getCriteriaExcludeElemnent($key);
205 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaExcludeElement(' . $key . ')[' . gettype($searchExclude) . ']=' . $searchExclude);
213 $searchExclude === false
220 $searchExclude !== false
222 $searchExclude !== $value
229 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - EXIT!');