X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fcriteria%2Fsearch%2Fclass_SearchCriteria.php;h=ded04793312a5f9c37b4f40e8d06c10f0b88d5bd;hp=3a45b7488d41d986390fabc636cddb3998fb1bc9;hb=0221b36ba9297c14c860c5bf0e3824b38ab78f38;hpb=4b88c118b615335d06bd74e444173d21aef4406c diff --git a/inc/classes/main/criteria/search/class_SearchCriteria.php b/inc/classes/main/criteria/search/class_SearchCriteria.php index 3a45b748..ded04793 100644 --- a/inc/classes/main/criteria/search/class_SearchCriteria.php +++ b/inc/classes/main/criteria/search/class_SearchCriteria.php @@ -6,7 +6,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team + * @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 * @@ -27,7 +27,7 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria { /** * Criteria to handle */ - private $searchCriteria = array(); + private $criteria = array(); /** * Limitation for the search @@ -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()->getConfigEntry($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,89 @@ 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 = ',') { + // $value cannot be array + assert(!is_array($value)); - // 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[' . __LINE__ . ']: key=' . $key . ',value=' . $value . ' - ENTERED!'); - /** - * 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[' . __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 = ((!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 = (((is_null($searchDefault)) || ($isMatching === TRUE)) && ((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 - } // END - foreach - // Now check if expected criteria counts match - $matches = ($counted == count($this->searchCriteria)); + // 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($searchChoice))); + + // 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 the result - return $matches; + // Return result + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: key=' . $key . ',value=' . $value . ',isMatching=' . intval($isMatching) . ' - EXIT!'); + return $isMatching; } }