3 namespace CoreFramework\Criteria\Search;
6 * Search criteria for e.g. searching in databases. Do not use this class if
7 * you are looking for a ship or company, or what ever. Instead use this class
8 * for looking in storages like the database.
10 * @author Roland Haeder <webmaster@shipsimu.org>
12 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
13 * @license GNU GPL 3.0 or any newer version
14 * @link http://www.shipsimu.org
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 class SearchCriteria extends BaseCriteria implements LocalSearchCriteria {
33 private $criteria = array();
36 * Limitation for the search
41 * Skip these entries before using them
46 * Protected constructor
50 protected function __construct () {
51 // Call parent constructor
52 parent::__construct(__CLASS__);
56 * Create an instance of this class
58 * @return $criteriaInstance An instance of this criteria
60 public static final function createSearchCriteria () {
62 $criteriaInstance = new SearchCriteria();
64 // Return this instance
65 return $criteriaInstance;
71 * @param $limit Search limit
73 * @todo Find a nice casting here. (int) allows until and including 32766.
75 public final function setLimit ($limit) {
76 $this->limit = $limit;
80 * "Setter" for limit from a configuration entry
82 * @param $configEntry The configuration entry which hold a number as limit
85 public final function setConfiguredLimit ($configEntry) {
86 // Get the limit from config entry and set it
87 $limit = $this->getConfigInstance()->getConfigEntry($configEntry);
88 $this->setLimit($limit);
94 * @return $limit Search limit
96 public final function getLimit () {
103 * @param $skip Search skip
105 * @todo Find a nice casting here. (int) allows until and including 32766.
107 public final function setSkip ($skip) {
114 * @return $skip Search skip
116 public final function getSkip () {
121 * Checks whether the given key/value pair is matching with 'default' and one of 'choice' and
122 * never with in 'exclude'.
124 * @param $key Key element to check
125 * @param $value Value to check
126 * @param $separator Separator for "exploding" $value (default: ',')
127 * @return $isMatching Whether the key/value is matching or excluded
129 public function isCriteriaMatching ($key, $value, $separator = ',') {
130 // $key/$value cannot be array/NULL/bool, value can be NULL but then NULL must be loocked for
131 assert((!is_array($value)) && (!is_bool($value)) && (!is_array($key)) && (!is_null($key)) && (!is_bool($key)));
134 $valueArray = explode($separator, $value);
137 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ' - CALLED!');
139 // Get 'default' search value
140 $searchDefault = $this->getCriteriaElemnent($key);
143 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault);
146 $isMatching = (((($searchDefault !== FALSE) && ($searchDefault == $value)) || ((is_null($searchDefault)) && (is_null($value)))) || ($searchDefault === FALSE));
149 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault . ',isMatching=' . intval($isMatching));
151 // Get 'choice' search value (can be NULL or $separator-separated string)
152 $searchChoice = $this->getCriteriaChoiceElemnent($key);
154 // May be FALSE or array
155 assert(($searchChoice === FALSE) || (is_array($searchChoice)));
158 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[' . gettype($searchChoice) . ']=' . print_r($searchChoice, TRUE));
161 if ((is_array($searchChoice)) && (count($valueArray) == 1)) {
162 // $value is a single-search value, so use in_array()
163 $isMatching = ((($isMatching === TRUE) || (($searchDefault === FALSE) && (!is_null($value)))) && (in_array($value, $searchChoice)));
166 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - SINGLE-MATCH');
167 } elseif ((is_array($searchChoice)) && (count($valueArray) > 1)) {
169 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',valueArray()=' . count($valueArray) . ',isMatching=' . intval($isMatching));
171 // $value is choice-search value, so check all entries
172 $isMatching = (($isMatching === TRUE) || (($searchDefault === FALSE) && (!is_null($value))));
174 foreach ($valueArray as $idx => $match) {
176 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: match=' . $match . ',count(searchChoice)=' . count($searchChoice));
178 // Is it found? (one is okay)
179 $isMatching = (($isMatching === TRUE) && (in_array($match, $searchChoice)));
182 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: match=' . $match . ',isMatching=' . intval($isMatching));
186 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',valueArray()=' . count($valueArray) . ',idx=' . $idx . ',isMatching=' . intval($isMatching) . ' - CHOICE-MATCH');
188 // Choice-match is FALSE
190 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - FALSE-MATCH');
194 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',isMatching=' . intval($isMatching));
196 // Get 'exclude' search value
197 $searchExclude = $this->getCriteriaExcludeElemnent($key);
200 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaExcludeElement(' . $key . ')[' . gettype($searchExclude) . ']=' . $searchExclude);
208 $searchExclude === FALSE
215 $searchExclude !== FALSE
217 $searchExclude !== $value
224 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - EXIT!');