* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 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 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('SEARCH-CRITERIA: CALLED!'); $criteriaInstance = new SearchCriteria(); // Return this instance //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SEARCH-CRITERIA: criteriaInstance=%s - EXIT!', $criteriaInstance->__toString())); 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 $configKey The configuration entry which hold a number as limit * @return void * @throws InvalidArgumentException If a paramter has an invalid value */ public final function setConfiguredLimit (string $configKey) { // Check parameter //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SEARCH-CRITERIA: configKey=%s - CALLED!', $configKey)); if (empty($configKey)) { // Throw IAE throw new InvalidArgumentException('Parameter "configKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } // Get the limit from config entry $limit = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configKey); // And set it //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SEARCH-CRITERIA: limit=%d', $limit)); $this->setLimit($limit); // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('SEARCH-CRITERIA: EXIT!'); } /** * 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 * @throws UnexpectedValueException If $searchChoice is not an array */ 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__)->traceMessage(sprintf('SEARCH-CRITERIA: key=%s,value[]=%s,separator=%s - CALLED!', $key, gettype($value), $separator)); if (empty($key)) { // Throw IAE throw new InvalidArgumentException('Parameter "key" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } elseif (is_array($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" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } // "Explode" value //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SEARCH-CRITERIA: Invoking explode("%s",value[]=%s) ...', $separator, gettype($value))); $valueArray = explode($separator, $value); //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: valueArray()=%d', count($valueArray))); // Get 'default' search value //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SEARCH-CRITERIA: Invoking this->getCriteriaElemnent(%s) ...', $key)); $searchDefault = $this->getCriteriaElemnent($key); // 'default' check //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: searchDefault[%s]=%s', 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__)->debugMessage(sprintf('SEARCH-CRITERIA: isMatching=%d', intval($isMatching))); $searchChoice = $this->getCriteriaChoiceElemnent($key); // Is an array returned? //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: searchChoice[]=%s', gettype($searchChoice))); if (!is_array($searchChoice) && !is_bool($searchChoice)) { // Should not happen throw new UnexpectedValueException(sprintf('searchChoice[]=%s is unexpected', gettype($searchChoice)), FrameworkInterface::EXCEPTION_UNEXPECTED_VALUE); } // 'choice' check //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: searchChoice[]=%s', gettype($searchChoice))); 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__)->debugMessage(sprintf('SEARCH-CRITERIA: isMatching=%d - SINGLE-MATCH', intval($isMatching))); } elseif ((is_array($searchChoice)) && (count($valueArray) > 1)) { // $value is choice-search value, so check all entries //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: searchChoice()=%d,valueArray()=%d - MULTI-MATCH', count($searchChoice), count($valueArray))); $isMatching = (($isMatching === true) || (($searchDefault === false) && (!is_null($value)))); // Loop through all values //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: isMatching=%d - BEFORE!', intval($isMatching))); foreach ($valueArray as $idx => $match) { // Is it found? (one is okay) //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: idx=%d,match=%s', $idx, $match)); $isMatching = (($isMatching === true) && (in_array($match, $searchChoice))); // No longer matching? //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: isMatching=%d - LOOP!', intval($isMatching))); if (!$isMatching) { // Skip further iterations //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('SEARCH-CRITERIA: BREAK!'); break; } } // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: isMatching=%d - AFTER!', intval($isMatching))); } else { // Choice-match is false //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: key=%s[],searchChoice[]=%s,value[%s]=%s,isMatching=%d - FALSE-MATCH', $key, gettype($searchChoice), gettype($value), $value, intval($isMatching))); } // Get 'exclude' search value //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: Invoking this->getCriteriaExcludeElemnent(%s) ...', $key)); $searchExclude = $this->getCriteriaExcludeElemnent($key); // 'exclude' check //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('SEARCH-CRITERIA: searchExclude[%s]=%s', gettype($searchExclude), $searchExclude)); $isMatching = ( ( ( $isMatching === true ) && ( $searchExclude === false ) ) || ( ( ( $isMatching === true ) && ( $searchExclude !== false ) && ( $searchExclude !== $value ) ) ) ); // Return result //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('SEARCH-CRITERIA: isMatching=%d - EXIT!', intval($isMatching))); return $isMatching; } }