3 namespace CoreFramework\Criteria\Search;
5 // Import framework stuff
6 use CoreFramework\Criteria\BaseCriteria;
9 * Search criteria for e.g. searching in databases. Do not use this class if
10 * you are looking for a ship or company, or what ever. Instead use this class
11 * for looking in storages like the database.
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class SearchCriteria extends BaseCriteria implements LocalSearchCriteria {
36 private $criteria = array();
39 * Limitation for the search
44 * Skip these entries before using them
49 * Protected constructor
53 protected function __construct () {
54 // Call parent constructor
55 parent::__construct(__CLASS__);
59 * Create an instance of this class
61 * @return $criteriaInstance An instance of this criteria
63 public static final function createSearchCriteria () {
65 $criteriaInstance = new SearchCriteria();
67 // Return this instance
68 return $criteriaInstance;
74 * @param $limit Search limit
76 * @todo Find a nice casting here. (int) allows until and including 32766.
78 public final function setLimit ($limit) {
79 $this->limit = $limit;
83 * "Setter" for limit from a configuration entry
85 * @param $configEntry The configuration entry which hold a number as limit
88 public final function setConfiguredLimit ($configEntry) {
89 // Get the limit from config entry and set it
90 $limit = $this->getConfigInstance()->getConfigEntry($configEntry);
91 $this->setLimit($limit);
97 * @return $limit Search limit
99 public final function getLimit () {
106 * @param $skip Search skip
108 * @todo Find a nice casting here. (int) allows until and including 32766.
110 public final function setSkip ($skip) {
117 * @return $skip Search skip
119 public final function getSkip () {
124 * Checks whether the given key/value pair is matching with 'default' and one of 'choice' and
125 * never with in 'exclude'.
127 * @param $key Key element to check
128 * @param $value Value to check
129 * @param $separator Separator for "exploding" $value (default: ',')
130 * @return $isMatching Whether the key/value is matching or excluded
132 public function isCriteriaMatching ($key, $value, $separator = ',') {
133 // $key/$value cannot be array/NULL/bool, value can be NULL but then NULL must be loocked for
134 assert((!is_array($value)) && (!is_bool($value)) && (!is_array($key)) && (!is_null($key)) && (!is_bool($key)));
137 $valueArray = explode($separator, $value);
140 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ' - CALLED!');
142 // Get 'default' search value
143 $searchDefault = $this->getCriteriaElemnent($key);
146 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault);
149 $isMatching = (((($searchDefault !== FALSE) && ($searchDefault == $value)) || ((is_null($searchDefault)) && (is_null($value)))) || ($searchDefault === FALSE));
152 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault . ',isMatching=' . intval($isMatching));
154 // Get 'choice' search value (can be NULL or $separator-separated string)
155 $searchChoice = $this->getCriteriaChoiceElemnent($key);
157 // May be FALSE or array
158 assert(($searchChoice === FALSE) || (is_array($searchChoice)));
161 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[' . gettype($searchChoice) . ']=' . print_r($searchChoice, TRUE));
164 if ((is_array($searchChoice)) && (count($valueArray) == 1)) {
165 // $value is a single-search value, so use in_array()
166 $isMatching = ((($isMatching === TRUE) || (($searchDefault === FALSE) && (!is_null($value)))) && (in_array($value, $searchChoice)));
169 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - SINGLE-MATCH');
170 } elseif ((is_array($searchChoice)) && (count($valueArray) > 1)) {
172 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',valueArray()=' . count($valueArray) . ',isMatching=' . intval($isMatching));
174 // $value is choice-search value, so check all entries
175 $isMatching = (($isMatching === TRUE) || (($searchDefault === FALSE) && (!is_null($value))));
177 foreach ($valueArray as $idx => $match) {
179 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: match=' . $match . ',count(searchChoice)=' . count($searchChoice));
181 // Is it found? (one is okay)
182 $isMatching = (($isMatching === TRUE) && (in_array($match, $searchChoice)));
185 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: match=' . $match . ',isMatching=' . intval($isMatching));
189 //* 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');
191 // Choice-match is FALSE
193 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - FALSE-MATCH');
197 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',isMatching=' . intval($isMatching));
199 // Get 'exclude' search value
200 $searchExclude = $this->getCriteriaExcludeElemnent($key);
203 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaExcludeElement(' . $key . ')[' . gettype($searchExclude) . ']=' . $searchExclude);
211 $searchExclude === FALSE
218 $searchExclude !== FALSE
220 $searchExclude !== $value
227 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - EXIT!');