X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fcriteria%2Fsearch%2Fclass_SearchCriteria.php;h=3bc2ef145d7bed7b99e6f64dc861d9a81985fb8a;hp=936200a0607b76b044dca672cc1fe3d7579564d5;hb=5203f9bd014ad46fbc7ee54e7223dcd46e14e3b4;hpb=0cd57c3885f00ad77fc599e53ed2f2d5e7ac267f diff --git a/inc/classes/main/criteria/search/class_SearchCriteria.php b/inc/classes/main/criteria/search/class_SearchCriteria.php index 936200a0..3bc2ef14 100644 --- a/inc/classes/main/criteria/search/class_SearchCriteria.php +++ b/inc/classes/main/criteria/search/class_SearchCriteria.php @@ -4,11 +4,11 @@ * you are looking for a ship or company, or what ever. Instead use this class * for looking in storages like the database. * - * @author Roland Haeder + * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team * @license GNU GPL 3.0 or any newer version - * @link http://www.ship-simu.org + * @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 @@ -27,7 +27,7 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria { /** * Criteria to handle */ - private $searchCriteria = array(); + private $criteria = array(); /** * Limitation for the search @@ -54,7 +54,7 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria { * * @return $criteriaInstance An instance of this criteria */ - public final static function createSearchCriteria () { + public static final function createSearchCriteria () { // Get a new instance $criteriaInstance = new SearchCriteria(); @@ -63,38 +63,26 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria { } /** - * Add criteria - * - * @param $criteriaKey Criteria key - * @param $criteriaValue Criteria value - * @return void - */ - public final function addCriteria ($criteriaKey, $criteriaValue) { - $this->searchCriteria[(string)$criteriaKey] = (string)$criteriaValue; - } - - /** - * Add configured criteria + * Setter for limit * - * @param $criteriaKey Criteria key - * @param $configEntry Configuration entry + * @param $limit Search limit * @return void + * @todo Find a nice casting here. (int) allows until and including 32766. */ - public final function addConfiguredCriteria ($criteriaKey, $configEntry) { - // Add the configuration entry as a criteria - $value = $this->getConfigInstance()->readConfig($configEntry); - $this->addCriteria($criteriaKey, $value); + public final function setLimit ($limit) { + $this->limit = $limit; } /** - * Setter for limit + * "Setter" for limit from a configuration entry * - * @param $limit Search limit + * @param $configEntry The configuration entry which hold a number as limit * @return void - * @todo Find a nice casting here. (int) allows until and including 32766. */ - public final function setLimit ($limit) { - $this->limit = $limit; + public final function setConfiguredLimit ($configEntry) { + // Get the limit from config entry and set it + $limit = $this->getConfigInstance()->getConfigEntry($configEntry); + $this->setLimit($limit); } /** @@ -127,81 +115,111 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria { } /** - * "Getter" for a cache key + * Checks whether the given key/value pair is matching with 'default' and one of 'choice' and + * never with in 'exclude'. * - * @return $cacheKey The key suitable for the cache system + * @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 getCacheKey () { - // Initialize the key - $cacheKey = ''; - - // Now walk through all criterias - foreach ($this->searchCriteria as $criteriaKey => $criteriaValue) { - // Add the value URL encoded to avoid any trouble with special characters - $cacheKey .= sprintf("%s=%s;", - $criteriaKey, - urlencode($criteriaValue) - ); - } + public function isCriteriaMatching ($key, $value, $separator = ',') { + // $key/$value cannot be array/NULL/bool, value can be NULL but then NULL must be loocked for + assert((!is_array($value)) && (!is_bool($value)) && (!is_array($key)) && (!is_null($key)) && (!is_bool($key))); - // Add limit and skip values - $cacheKey .= sprintf("%%limit%%=%s;%%skip%%=%s", - $this->limit, - $this->skip - ); + // "Explode" value + $valueArray = explode($separator, $value); - // Return the cache key - return $cacheKey; - } + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ' - CALLED!'); - /** - * Get criteria element or null if not found - * - * @param $criteria The criteria we want to have - * @return $value Wether the value of the critera or null - */ - public function getCriteriaElemnent ($criteria) { - // Default is not found - $value = null; - - // Is the criteria there? - if (isset($this->searchCriteria[$criteria])) { - // Then use it - $value = $this->searchCriteria[$criteria]; - } + // Get 'default' search value + $searchDefault = $this->getCriteriaElemnent($key); - // Return the value - return $value; - } + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault); - /** - * Checks wether given array entry matches - * - * @param $entryArray Array with the entries to find - * @return $matches Wether the entry matches or not - */ - public function ifEntryMatches (array $entryArray) { - // First nothing matches and nothing is counted - $matches = false; - $counted = 0; - - // Walk through all entries - foreach ($entryArray as $key => $entry) { - // Then walk through all search criteria - foreach ($this->searchCriteria as $criteriaKey => $criteriaValue) { - // Is the element found and does it match? - if (($key == $criteriaKey) && ($criteriaValue == $entry)) { - // Then count this one up - $counted++; - } // END - if + // 'default' check + $isMatching = (((($searchDefault !== FALSE) && ($searchDefault == $value)) || ((is_null($searchDefault)) && (is_null($value)))) || ($searchDefault === FALSE)); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault . ',isMatching=' . intval($isMatching)); + + // Get 'choice' search value (can be NULL or $separator-separated string) + $searchChoice = $this->getCriteriaChoiceElemnent($key); + + // May be FALSE or array + assert(($searchChoice === FALSE) || (is_array($searchChoice))); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __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) || (($searchDefault === FALSE) && (!is_null($value)))) && (in_array($value, $searchChoice))); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: 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__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',valueArray()=' . 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__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __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[' . __METHOD__ . ':' . __LINE__ . ']: match=' . $match . ',isMatching=' . intval($isMatching)); } // END - foreach - } // END - foreach - // Now check if expected criteria counts match - $matches = ($counted == count($this->searchCriteria)); + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',valueArray()=' . count($valueArray) . ',idx=' . $idx . ',isMatching=' . intval($isMatching) . ' - CHOICE-MATCH'); + } else { + // Choice-match is FALSE + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - FALSE-MATCH'); + } + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __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[' . __METHOD__ . ':' . __LINE__ . ']: getCriteriaExcludeElement(' . $key . ')[' . gettype($searchExclude) . ']=' . $searchExclude); + + // 'exclude' check + $isMatching = ( + ( + ( + $isMatching === TRUE + ) && ( + $searchExclude === FALSE + ) + ) || ( + ( + ( + $isMatching === TRUE + ) && ( + $searchExclude !== FALSE + ) && ( + $searchExclude !== $value + ) + ) + ) + ); - // Return the result - return $matches; + // Return result + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __METHOD__ . ':' . __LINE__ . ']: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - EXIT!'); + return $isMatching; } }