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 <<<<<<< HEAD:framework/main/classes/criteria/search/class_SearchCriteria.php
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
20 >>>>>>> Some updates::inc/main/classes/criteria/search/class_SearchCriteria.php
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.shipsimu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 class SearchCriteria extends BaseCriteria implements LocalSearchCriteria {
41 private $criteria = array();
44 * Limitation for the search
49 * Skip these entries before using them
54 * Protected constructor
58 protected function __construct () {
59 // Call parent constructor
60 parent::__construct(__CLASS__);
64 * Create an instance of this class
66 * @return $criteriaInstance An instance of this criteria
68 public static final function createSearchCriteria () {
70 $criteriaInstance = new SearchCriteria();
72 // Return this instance
73 return $criteriaInstance;
79 * @param $limit Search limit
81 * @todo Find a nice casting here. (int) allows until and including 32766.
83 public final function setLimit ($limit) {
84 $this->limit = $limit;
88 * "Setter" for limit from a configuration entry
90 * @param $configEntry The configuration entry which hold a number as limit
93 public final function setConfiguredLimit ($configEntry) {
94 // Get the limit from config entry and set it
95 $limit = $this->getConfigInstance()->getConfigEntry($configEntry);
96 $this->setLimit($limit);
102 * @return $limit Search limit
104 public final function getLimit () {
111 * @param $skip Search skip
113 * @todo Find a nice casting here. (int) allows until and including 32766.
115 public final function setSkip ($skip) {
122 * @return $skip Search skip
124 public final function getSkip () {
129 * Checks whether the given key/value pair is matching with 'default' and one of 'choice' and
130 * never with in 'exclude'.
132 * @param $key Key element to check
133 * @param $value Value to check
134 * @param $separator Separator for "exploding" $value (default: ',')
135 * @return $isMatching Whether the key/value is matching or excluded
137 public function isCriteriaMatching ($key, $value, $separator = ',') {
138 // $key/$value cannot be array/NULL/bool, value can be NULL but then NULL must be loocked for
139 assert((!is_array($value)) && (!is_bool($value)) && (!is_array($key)) && (!is_null($key)) && (!is_bool($key)));
142 $valueArray = explode($separator, $value);
145 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ' - CALLED!');
147 // Get 'default' search value
148 $searchDefault = $this->getCriteriaElemnent($key);
151 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault);
154 $isMatching = (((($searchDefault !== false) && ($searchDefault == $value)) || ((is_null($searchDefault)) && (is_null($value)))) || ($searchDefault === false));
157 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault . ',isMatching=' . intval($isMatching));
159 // Get 'choice' search value (can be NULL or $separator-separated string)
160 $searchChoice = $this->getCriteriaChoiceElemnent($key);
162 // May be false or array
163 assert(($searchChoice === false) || (is_array($searchChoice)));
166 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[' . gettype($searchChoice) . ']=' . print_r($searchChoice, true));
169 if ((is_array($searchChoice)) && (count($valueArray) == 1)) {
170 // $value is a single-search value, so use in_array()
171 $isMatching = ((($isMatching === true) || (($searchDefault === false) && (!is_null($value)))) && (in_array($value, $searchChoice)));
174 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - SINGLE-MATCH');
175 } elseif ((is_array($searchChoice)) && (count($valueArray) > 1)) {
177 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',valueArray()=' . count($valueArray) . ',isMatching=' . intval($isMatching));
179 // $value is choice-search value, so check all entries
180 $isMatching = (($isMatching === true) || (($searchDefault === false) && (!is_null($value))));
182 foreach ($valueArray as $idx => $match) {
184 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: match=' . $match . ',count(searchChoice)=' . count($searchChoice));
186 // Is it found? (one is okay)
187 $isMatching = (($isMatching === true) && (in_array($match, $searchChoice)));
190 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: match=' . $match . ',isMatching=' . intval($isMatching));
194 //* 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');
196 // Choice-match is false
198 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - false-MATCH');
202 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',isMatching=' . intval($isMatching));
204 // Get 'exclude' search value
205 $searchExclude = $this->getCriteriaExcludeElemnent($key);
208 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaExcludeElement(' . $key . ')[' . gettype($searchExclude) . ']=' . $searchExclude);
216 $searchExclude === false
223 $searchExclude !== false
225 $searchExclude !== $value
232 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - EXIT!');