* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.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 = array(); /** * Limitation for the search */ private $limit = 0; /** * Skip these entries before using them */ private $skip = 0; /** * Protected constructor * * @return void */ protected 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 ($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 ($configEntry) { // Get the limit from config entry and set it $limit = $this->getConfigInstance()->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 ($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 */ public function isCriteriaMatching ($key, $value, $separator = ',') { // $value cannot be array assert(!is_array($value)); // "Explode" value $valueArray = explode($separator, $value); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: key=' . $key . ',value=' . $value . ' - ENTERED!'); // Get 'default' search value $searchDefault = $this->getCriteriaElemnent($key); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault); // 'default' check $isMatching = ((!is_null($searchDefault)) && ($searchDefault == $value)); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault . ',isMatching=' . intval($isMatching)); // Get 'choice' search value (can be NULL or $separator-separated string) $searchChoice = $this->getCriteriaChoiceElemnent($key); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[' . gettype($searchChoice) . ']=' . print_r($searchChoice, TRUE)); // 'choice' check if ((is_array($searchChoice)) && (count($valueArray) == 1)) { // $value is a single-search value, so use in_array() $isMatching = ((($isMatching === TRUE) || (is_null($searchDefault))) && ((is_null($searchChoice)) || ((is_array($searchChoice)) && (in_array($value, $searchChoice))))); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value=' . $value . ',isMatching=' . intval($isMatching) . ' - SINGLE-MATCH'); } elseif ((is_array($searchChoice)) && (count($valueArray) > 1)) { // $value is choice-search value, so check all entries $isMatching = ((is_null($searchDefault)) || ($isMatching === TRUE)); $idx = 0; foreach ($valueArray as $idx => $match) { // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: 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__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: match=' . $match . ',isMatching=' . intval($isMatching)); } // END - foreach // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',valueArray()=' . count($valueArray) . ',idx=' . $idx . ',isMatching=' . intval($isMatching) . ' - CHOICE-MATCH'); } else { // Choice-match is NULL? $isMatching = (($isMatching === TRUE) || (is_null($searchDefault))); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value=' . $value . ',isMatching=' . intval($isMatching) . ' - NULL-MATCH'); } // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',isMatching=' . intval($isMatching)); // Get 'exclude' search value $searchExclude = $this->getCriteriaExcludeElemnent($key); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaExcludeElement(' . $key . ')[' . gettype($searchExclude) . ']=' . $searchExclude); // 'exclude' check $isMatching = (((is_null($searchChoice)) || ($isMatching === TRUE)) && ((is_null($searchExclude)) || ($searchExclude != $value))); // Return result //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: key=' . $key . ',value=' . $value . ',isMatching=' . intval($isMatching) . ' - EXIT!'); return $isMatching; } } // [EOF] ?>