3 namespace Org\Mxchange\CoreFramework\Criteria\Search;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Criteria\BaseCriteria;
7 use Org\Mxchange\CoreFramework\Criteria\Local\LocalSearchCriteria;
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.
14 * @author Roland Haeder <webmaster@shipsimu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.shipsimu.org
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.
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.
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/>.
33 class SearchCriteria extends BaseCriteria implements LocalSearchCriteria {
37 private $criteria = array();
40 * Limitation for the search
45 * Skip these entries before using them
50 * Protected constructor
54 protected function __construct () {
55 // Call parent constructor
56 parent::__construct(__CLASS__);
60 * Create an instance of this class
62 * @return $criteriaInstance An instance of this criteria
64 public static final function createSearchCriteria () {
66 $criteriaInstance = new SearchCriteria();
68 // Return this instance
69 return $criteriaInstance;
75 * @param $limit Search limit
77 * @todo Find a nice casting here. (int) allows until and including 32766.
79 public final function setLimit ($limit) {
80 $this->limit = $limit;
84 * "Setter" for limit from a configuration entry
86 * @param $configEntry The configuration entry which hold a number as limit
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);
98 * @return $limit Search limit
100 public final function getLimit () {
107 * @param $skip Search skip
109 * @todo Find a nice casting here. (int) allows until and including 32766.
111 public final function setSkip ($skip) {
118 * @return $skip Search skip
120 public final function getSkip () {
125 * Checks whether the given key/value pair is matching with 'default' and one of 'choice' and
126 * never with in 'exclude'.
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
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)));
138 $valueArray = explode($separator, $value);
141 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ' - CALLED!');
143 // Get 'default' search value
144 $searchDefault = $this->getCriteriaElemnent($key);
147 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault);
150 $isMatching = (((($searchDefault !== false) && ($searchDefault == $value)) || ((is_null($searchDefault)) && (is_null($value)))) || ($searchDefault === false));
153 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault . ',isMatching=' . intval($isMatching));
155 // Get 'choice' search value (can be NULL or $separator-separated string)
156 $searchChoice = $this->getCriteriaChoiceElemnent($key);
158 // May be false or array
159 assert(($searchChoice === false) || (is_array($searchChoice)));
162 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[' . gettype($searchChoice) . ']=' . print_r($searchChoice, true));
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)));
170 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - SINGLE-MATCH');
171 } elseif ((is_array($searchChoice)) && (count($valueArray) > 1)) {
173 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',valueArray()=' . count($valueArray) . ',isMatching=' . intval($isMatching));
175 // $value is choice-search value, so check all entries
176 $isMatching = (($isMatching === true) || (($searchDefault === false) && (!is_null($value))));
178 foreach ($valueArray as $idx => $match) {
180 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: match=' . $match . ',count(searchChoice)=' . count($searchChoice));
182 // Is it found? (one is okay)
183 $isMatching = (($isMatching === true) && (in_array($match, $searchChoice)));
186 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: match=' . $match . ',isMatching=' . intval($isMatching));
190 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',valueArray()=' . count($valueArray) . ',idx=' . $idx . ',isMatching=' . intval($isMatching) . ' - CHOICE-MATCH');
192 // Choice-match is false
194 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - false-MATCH');
198 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',isMatching=' . intval($isMatching));
200 // Get 'exclude' search value
201 $searchExclude = $this->getCriteriaExcludeElemnent($key);
204 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaExcludeElement(' . $key . ')[' . gettype($searchExclude) . ']=' . $searchExclude);
212 $searchExclude === false
219 $searchExclude !== false
221 $searchExclude !== $value
228 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - EXIT!');