* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria { /** * Criteria to handle */ private $criteria = []; /** * Limitation for the search */ private $limit = 0; /** * Skip these entries before using them */ private $skip = 0; /** * Protected constructor * * @return void */ private function __construct () { // Call parent constructor parent::__construct(__CLASS__); } /** * Create an instance of this class * * @return $criteriaInstance An instance of this criteria */ public static final function createSearchCriteria () { // Get a new instance $criteriaInstance = new SearchCriteria(); // Return this instance return $criteriaInstance; } /** * Setter for limit * * @param $limit Search limit * @return void * @todo Find a nice casting here. (int) allows until and including 32766. */ public final function setLimit (int $limit) { $this->limit = $limit; } /** * "Setter" for limit from a configuration entry * * @param $configEntry The configuration entry which hold a number as limit * @return void */ public final function setConfiguredLimit (string $configEntry) { // Get the limit from config entry and set it $limit = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configEntry); $this->setLimit($limit); } /** * Getter for limit * * @return $limit Search limit */ public final function getLimit () { return $this->limit; } /** * Setter for skip * * @param $skip Search skip * @return void * @todo Find a nice casting here. (int) allows until and including 32766. */ public final function setSkip (int $skip) { $this->skip = $skip; } /** * Getter for skip * * @return $skip Search skip */ public final function getSkip () { return $this->skip; } /** * Checks whether the given key/value pair is matching with 'default' and one of 'choice' and * never with in 'exclude'. * * @param $key Key element to check * @param $value Value to check * @param $separator Separator for "exploding" $value (default: ',') * @return $isMatching Whether the key/value is matching or excluded * @throws InvalidArgumentException If a parameter is invalid */ public function isCriteriaMatching (string $key, $value, string $separator = ',') { // $key/$value cannot be array/NULL/bool, value can be NULL but then NULL must be loocked for //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SEARCH-CRITERIA: key=%s,value[]=%s,separator=%s - CALLED!', $key, gettype($value), $separator)); if (empty($key)) { // Throw IAE throw new InvalidArgumentException('Parameter "key" cannot be empty'); } elseif (is_array($value) || is_bool($value) || is_bool($value) || is_object($value) || is_resource($value)) { // Throw it again throw new InvalidArgumentException(sprintf('value[]=%s is not supported/valid', gettype($value))); } elseif (empty($separator)) { // Throw IAE throw new InvalidArgumentException('Parameter "separator" cannot be empty'); } // "Explode" value $valueArray = explode($separator, $value); // Get 'default' search value $searchDefault = $this->getCriteriaElemnent($key); // 'default' check //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault); $isMatching = ( ( ( ($searchDefault !== false) && ($searchDefault == $value) ) || ( is_null($searchDefault) && is_null($value) ) ) || ( $searchDefault === false ) ); // Get 'choice' search value (can be NULL or $separator-separated string) //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault . ',isMatching=' . intval($isMatching)); $searchChoice = $this->getCriteriaChoiceElemnent($key); // May be false or array assert(($searchChoice === false) || (is_array($searchChoice))); // 'choice' check //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[' . gettype($searchChoice) . ']=' . print_r($searchChoice, true)); if ((is_array($searchChoice)) && (count($valueArray) == 1)) { // $value is a single-search value, so use in_[] $isMatching = ((($isMatching === true) || (($searchDefault === false) && (!is_null($value)))) && (in_array($value, $searchChoice))); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - SINGLE-MATCH'); } elseif ((is_array($searchChoice)) && (count($valueArray) > 1)) { // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[]=' . count($valueArray) . ',isMatching=' . intval($isMatching)); // $value is choice-search value, so check all entries $isMatching = (($isMatching === true) || (($searchDefault === false) && (!is_null($value)))); $idx = 0; foreach ($valueArray as $idx => $match) { // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: match=' . $match . ',count(searchChoice)=' . count($searchChoice)); // Is it found? (one is okay) $isMatching = (($isMatching === true) && (in_array($match, $searchChoice))); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: match=' . $match . ',isMatching=' . intval($isMatching)); } // END - foreach // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[]=' . count($valueArray) . ',idx=' . $idx . ',isMatching=' . intval($isMatching) . ' - CHOICE-MATCH'); } else { // Choice-match is false // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - false-MATCH'); } // Get 'exclude' search value //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',isMatching=' . intval($isMatching)); $searchExclude = $this->getCriteriaExcludeElemnent($key); // 'exclude' check //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: getCriteriaExcludeElement(' . $key . ')[' . gettype($searchExclude) . ']=' . $searchExclude); $isMatching = ( ( ( $isMatching === true ) && ( $searchExclude === false ) ) || ( ( ( $isMatching === true ) && ( $searchExclude !== false ) && ( $searchExclude !== $value ) ) ) ); // Return result //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SEARCH-CRITERIA: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - EXIT!'); return $isMatching; } }