Fixed searched criteria handling #3
authorRoland Häder <roland@mxchange.org>
Fri, 22 Feb 2013 06:32:16 +0000 (06:32 +0000)
committerRoland Häder <roland@mxchange.org>
Fri, 22 Feb 2013 06:32:16 +0000 (06:32 +0000)
inc/classes/interfaces/criteria/class_Criteria.php
inc/classes/main/commands/web/class_WebResendLinkCommand.php
inc/classes/main/criteria/class_BaseCriteria.php
inc/classes/main/criteria/search/class_SearchCriteria.php
inc/classes/main/database/databases/class_LocalFileDatabase.php

index 0a1264ccc9f8d33f2c39a52f3b95eeb6394980fd..d2cb532fe074fa4e939f0efbe2986627c5e0f9f6 100644 (file)
@@ -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);
 
index 3757d4b71a63599aa2b028e4ed000e9e5fc46d92..2cd6a82fffc118fe21d17e50d6d7c2d014fdc8e6 100644 (file)
@@ -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
index ab91f93c9ba9e6805345982307cffe9ae40c7496..b0edb27dc270a17c2be4bb7cf46b110a04a9dc5f 100644 (file)
@@ -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);
index f9926f1241d47e04cdd4df85069b89d308021aba..02f3083dc55d80b91d50eb7ae642d8b42a50c895 100644 (file)
@@ -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;
        }
 }
index e45b294a09e2bec3d1f6edd325508bcfefa1c3a8..7aa3fb359f983740957752b205021412b220bbd4 100644 (file)
@@ -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