From ccc1db45751c976264513b8c51884e39f8214b12 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Fri, 22 Feb 2013 06:32:16 +0000 Subject: [PATCH] Fixed searched criteria handling #3 --- .../interfaces/criteria/class_Criteria.php | 12 ++-- .../web/class_WebResendLinkCommand.php | 1 - .../main/criteria/class_BaseCriteria.php | 22 +++---- .../criteria/search/class_SearchCriteria.php | 64 +++++++++++++------ .../databases/class_LocalFileDatabase.php | 10 +++ 5 files changed, 70 insertions(+), 39 deletions(-) diff --git a/inc/classes/interfaces/criteria/class_Criteria.php b/inc/classes/interfaces/criteria/class_Criteria.php index 0a1264cc..d2cb532f 100644 --- a/inc/classes/interfaces/criteria/class_Criteria.php +++ b/inc/classes/interfaces/criteria/class_Criteria.php @@ -126,27 +126,27 @@ interface Criteria extends FrameworkInterface { function addConfiguredCriteria ($criteriaKey, $configEntry, $criteriaType = 'default'); /** - * Get criteria element or null if not found + * Get criteria element or FALSE if not found * * @param $criteriaKey The requested criteria key * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude' - * @return $value Whether the value of the critera or null + * @return $value Whether the value of the critera or FALSE */ function getCriteriaElemnent ($criteriaKey, $criteriaType = 'default'); /** - * Get criteria element or null if not found for 'choice' type + * Get criteria element or FALSE if not found for 'choice' type * * @param $criteriaKey The requested criteria key - * @return $value Whether the value of the critera or null + * @return $value Whether the value of the critera or FALSE */ function getCriteriaChoiceElemnent ($criteriaKey); /** - * Get criteria element or null if not found for 'exclude' type + * Get criteria element or FALSE if not found for 'exclude' type * * @param $criteriaKey The requested criteria key - * @return $value Whether the value of the critera or null + * @return $value Whether the value of the critera or FALSE */ function getCriteriaExcludeElemnent ($criteriaKey); diff --git a/inc/classes/main/commands/web/class_WebResendLinkCommand.php b/inc/classes/main/commands/web/class_WebResendLinkCommand.php index 3757d4b7..2cd6a82f 100644 --- a/inc/classes/main/commands/web/class_WebResendLinkCommand.php +++ b/inc/classes/main/commands/web/class_WebResendLinkCommand.php @@ -64,7 +64,6 @@ class WebResendLinkCommand extends BaseCommand implements Commandable { $applicationInstance = $this->getResolverInstance()->getApplicationInstance(); // Get a RNG instance (Random Number Generator) - // @TODO Rewrite this code to make use of generateRandomString() from 'hub' project $rngInstance = ObjectFactory::createObjectByConfiguredName('rng_class'); // Generate a pseudo-random string diff --git a/inc/classes/main/criteria/class_BaseCriteria.php b/inc/classes/main/criteria/class_BaseCriteria.php index ab91f93c..b0edb27d 100644 --- a/inc/classes/main/criteria/class_BaseCriteria.php +++ b/inc/classes/main/criteria/class_BaseCriteria.php @@ -148,7 +148,7 @@ class BaseCriteria extends BaseFrameworkSystem implements Criteria { */ public final function addCriteria ($criteriaKey, $criteriaValue, $criteriaType = 'default') { // Make sure no 'my-' or 'my_' passes this point - assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE)); + assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE) && (!is_bool($criteriaValue))); // Convert dashes to underscore $criteriaKey = $this->convertDashesToUnderscores($criteriaKey); @@ -176,7 +176,7 @@ class BaseCriteria extends BaseFrameworkSystem implements Criteria { */ public final function addChoiceCriteria ($criteriaKey, $criteriaValue) { // Make sure no 'my-' or 'my_' passes this point - assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE)); + assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE) && (!is_bool($criteriaValue))); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue); @@ -213,11 +213,11 @@ class BaseCriteria extends BaseFrameworkSystem implements Criteria { } /** - * Get criteria element or null if not found + * Get criteria element or FALSE if not found * * @param $criteriaKey The requested criteria key * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude' - * @return $value Whether the value of the critera or null + * @return $value Whether the value of the critera or FALSE */ public function getCriteriaElemnent ($criteriaKey, $criteriaType = 'default') { // Make sure no 'my-' or 'my_' passes this point @@ -230,7 +230,7 @@ class BaseCriteria extends BaseFrameworkSystem implements Criteria { //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteria()=' . count($this->criteria[$criteriaType])); // Default is not found - $value = NULL; + $value = FALSE; // Is the criteria there? if ($this->isKeySet($criteriaType, $criteriaKey)) { @@ -243,10 +243,10 @@ class BaseCriteria extends BaseFrameworkSystem implements Criteria { } /** - * Get criteria element or null if not found for 'choice' type + * Get criteria element or FALSE if not found for 'choice' type * * @param $criteriaKey The requested criteria key - * @return $value Whether the value of the critera or null + * @return $value Whether the value of the critera or FALSE */ public function getCriteriaChoiceElemnent ($criteriaKey) { // Call inner method @@ -254,10 +254,10 @@ class BaseCriteria extends BaseFrameworkSystem implements Criteria { } /** - * Get criteria element or null if not found for 'exclude' type + * Get criteria element or FALSE if not found for 'exclude' type * * @param $criteriaKey The requested criteria key - * @return $value Whether the value of the critera or null + * @return $value Whether the value of the critera or FALSE */ public function getCriteriaExcludeElemnent ($criteriaKey) { // Call inner method @@ -287,7 +287,7 @@ class BaseCriteria extends BaseFrameworkSystem implements Criteria { // Then walk through all search criteria foreach ($this->criteria[$criteriaType] as $criteriaKey => $criteriaValue) { // Make sure no 'my-' or 'my_' passes this point - assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE)); + assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE) && (!is_bool($criteriaValue))); // Convert dashes to underscore $criteriaKey = $this->convertDashesToUnderscores($criteriaKey); @@ -349,7 +349,7 @@ class BaseCriteria extends BaseFrameworkSystem implements Criteria { // Now walk through all criterias foreach ($this->criteria[$criteriaType] as $criteriaKey => $criteriaValue) { // Make sure no 'my-' or 'my_' passes this point - assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE)); + assert((strpos($criteriaKey, 'my-') === FALSE) && (strpos($criteriaKey, 'my_') === FALSE) && (!is_bool($criteriaValue))); // Convert dashes to underscore $criteriaKey = $this->convertDashesToUnderscores($criteriaKey); diff --git a/inc/classes/main/criteria/search/class_SearchCriteria.php b/inc/classes/main/criteria/search/class_SearchCriteria.php index f9926f12..02f3083d 100644 --- a/inc/classes/main/criteria/search/class_SearchCriteria.php +++ b/inc/classes/main/criteria/search/class_SearchCriteria.php @@ -124,79 +124,101 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria { * @return $isMatching Whether the key/value is matching or excluded */ public function isCriteriaMatching ($key, $value, $separator = ',') { - // $value cannot be array - assert(!is_array($value)); + // $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))); // "Explode" value $valueArray = explode($separator, $value); // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: key=' . $key . ',value=' . $value . ' - ENTERED!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: key=' . $key . ',value[' . gettype($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); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault); // 'default' check - $isMatching = ((!is_null($searchDefault)) && ($searchDefault == $value)); + $isMatching = (((($searchDefault !== FALSE) && ($searchDefault == $value)) || ((is_null($searchDefault)) && (is_null($value)))) || ($searchDefault === FALSE)); // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault . ',isMatching=' . intval($isMatching)); + /* 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); + // May be FALSE or array + assert(($searchChoice === FALSE) || (is_array($searchChoice))); + // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[' . gettype($searchChoice) . ']=' . print_r($searchChoice, TRUE)); + /* 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))))); + $isMatching = ((($isMatching === TRUE) || (($searchDefault === FALSE) && (!is_null($value)))) && (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'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __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[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',valueArray()=' . count($valueArray) . ',isMatching=' . intval($isMatching)); + // $value is choice-search value, so check all entries - $isMatching = ((is_null($searchDefault)) || ($isMatching === TRUE)); + $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[' . __LINE__ . ']: match=' . $match . ',count(searchChoice)=' . count($searchChoice)); + /* 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)); + /* 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'); + /* 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))); - + // Choice-match is FALSE // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value=' . $value . ',isMatching=' . intval($isMatching) . ' - NULL-MATCH'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - FALSE-MATCH'); } // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',isMatching=' . intval($isMatching)); + /* 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); + /* 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)); + $isMatching = ( + ( + ( + $isMatching === TRUE + ) && ( + $searchExclude === FALSE + ) + ) || ( + ( + ( + $isMatching === TRUE + ) && ( + $searchExclude !== FALSE + ) && ( + $searchExclude !== $value + ) + ) + ) + ); // Return result - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: key=' . $key . ',value=' . $value . ',isMatching=' . intval($isMatching) . ' - EXIT!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: key=' . $key . ',value[' . gettype($value) . ']=' . $value . ',isMatching=' . intval($isMatching) . ' - EXIT!'); return $isMatching; } } diff --git a/inc/classes/main/database/databases/class_LocalFileDatabase.php b/inc/classes/main/database/databases/class_LocalFileDatabase.php index e45b294a..7aa3fb35 100644 --- a/inc/classes/main/database/databases/class_LocalFileDatabase.php +++ b/inc/classes/main/database/databases/class_LocalFileDatabase.php @@ -357,6 +357,9 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackendIn // Search in the criteria with FMFW (First Matches, First Wins) foreach ($dataArray as $key => $value) { + // Make sure value is not bool + assert(!is_bool($value)); + // Found one entry? $isFound = (($isFound === TRUE) && ($searchInstance->isCriteriaMatching($key, $value))); /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: key=' . $key . ',value=' . $value . ',isFound=' . intval($isFound)); @@ -492,6 +495,9 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackendIn // Search in the criteria with FMFW (First Matches, First Wins) foreach ($dataArray as $key => $value) { + // Make sure value is not bool + assert(!is_bool($value)); + // Found one entry? $isFound = (($isFound === TRUE) && ($searchInstance->isCriteriaMatching($key, $value))); } // END - foreach @@ -510,6 +516,10 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackendIn // Entry found, so update it foreach ($searchArray as $searchKey => $searchValue) { + // Make sure the value is not bool again + assert(!is_bool($searchValue)); + + // Debug message + add/update it //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: criteriaKey=' . $searchKey . ',criteriaValue=' . $searchValue); $dataArray[$searchKey] = $searchValue; } // END - foreach -- 2.30.2