Rewrote search criteria matching:
authorRoland Häder <roland@mxchange.org>
Wed, 13 Feb 2013 02:01:54 +0000 (02:01 +0000)
committerRoland Häder <roland@mxchange.org>
Wed, 13 Feb 2013 02:01:54 +0000 (02:01 +0000)
- Allows now 'exclude' rules (if found, result will be FALSE) and choice (if
  one is found, result will be TRUE)
- Rewrote LocalFileDatabase class to above added method
- Expanded interface LocalSearchCriteria will all public methods

inc/classes/interfaces/criteria/class_Criteria.php
inc/classes/interfaces/criteria/extended/class_LocalSearchCriteria.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 737d5bc7a4af231f2cc07f68e79c88a93a48a6c0..0a1264ccc9f8d33f2c39a52f3b95eeb6394980fd 100644 (file)
@@ -37,6 +37,31 @@ interface Criteria extends FrameworkInterface {
         */
        function getWrapperConfigEntry ();
 
+       /**
+        * Checks whether given key is set
+        *
+        * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
+        * @param       $criteriaKey    Criteria key
+        * @return      $isSet                  Whether key is set
+        */
+       function isKeySet ($criteriaType, $criteriaKey);
+
+       /**
+        * Checks whether given key is set for 'choice' type
+        *
+        * @param       $criteriaKey    Criteria key
+        * @return      $isSet                  Whether key is set
+        */
+       function isChoiceKeySet ($criteriaKey);
+
+       /**
+        * Checks whether given key is set for 'exclude' type
+        *
+        * @param       $criteriaKey    Criteria key
+        * @return      $isSet                  Whether key is set
+        */
+       function isExcludeKeySet ($criteriaKey);
+
        /**
         * Getter for criteria array
         *
index 7a768a1a8f6bb944c2e00e088a01144e9fd8ab57..32ccdcf71c91330b396a81357bbb2eeeaa1cd53e 100644 (file)
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 interface LocalSearchCriteria extends Criteria {
+       /**
+        * Setter for limit
+        *
+        * @param       $limit  Search limit
+        * @return      void
+        * @todo        Find a nice casting here. (int) allows until and including 32766.
+        */
+       function setLimit ($limit);
+
+       /**
+        * "Setter" for limit from a configuration entry
+        *
+        * @param       $configEntry    The configuration entry which hold a number as limit
+        * @return      void
+        */
+       function setConfiguredLimit ($configEntry);
+
+       /**
+        * Getter for limit
+        *
+        * @return      $limit  Search limit
+        */
+       function getLimit ();
+
+       /**
+        * Setter for skip
+        *
+        * @param       $skip   Search skip
+        * @return      void
+        * @todo        Find a nice casting here. (int) allows until and including 32766.
+        */
+       function setSkip ($skip);
+
+       /**
+        * Getter for skip
+        *
+        * @return      $skip   Search skip
+        */
+       function getSkip ();
+
+       /**
+        * 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
+        * @return      $isMatching             Whether the key/value is matching or excluded
+        */
+       function isCriteriaMatching ($key, $value);
 }
 
 // [EOF]
index 3b97b8b5f432d1b4fad74e23e9151d54671bf66b..b53ce6482845616efafdb17cc0fe32f9d7ff6580 100644 (file)
@@ -50,6 +50,43 @@ class BaseCriteria extends BaseFrameworkSystem implements Criteria {
                parent::__construct($className);
        }
 
+       /**
+        * Checks whether given key is set
+        *
+        * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
+        * @param       $criteriaKey    Criteria key
+        * @return      $isSet                  Whether key is set
+        */
+       public function isKeySet ($criteriaType, $criteriaKey) {
+               // Determine it
+               $isSet = isset($this->criteria[$criteriaType][$criteriaKey]);
+
+               // Return it
+               return $isSet;
+       }
+
+       /**
+        * Checks whether given key is set for 'choice' type
+        *
+        * @param       $criteriaKey    Criteria key
+        * @return      $isSet                  Whether key is set
+        */
+       public function isChoiceKeySet ($criteriaKey) {
+               // Call inner method
+               return $this->isKeySet('choice', $criteriaKey);
+       }
+
+       /**
+        * Checks whether given key is set for 'exclude' type
+        *
+        * @param       $criteriaKey    Criteria key
+        * @return      $isSet                  Whether key is set
+        */
+       public function isExcludeKeySet ($criteriaKey) {
+               // Call inner method
+               return $this->isKeySet('exclude', $criteriaKey);
+       }
+
        /**
         * Setter for wrapper class name
         *
@@ -115,7 +152,7 @@ class BaseCriteria extends BaseFrameworkSystem implements Criteria {
                $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
 
                // Is it already there?
-               if (isset($this->criteria[$criteriaType][$criteriaKey])) {
+               if ($this->isKeySet($criteriaType, $criteriaKey)) {
                        // Append it
                        $this->criteria[$criteriaType][$criteriaKey] .= ',' . $criteriaValue;
                } else {
@@ -186,7 +223,7 @@ class BaseCriteria extends BaseFrameworkSystem implements Criteria {
                $value = NULL;
 
                // Is the criteria there?
-               if (isset($this->criteria[$criteriaType][$criteriaKey])) {
+               if ($this->isKeySet($criteriaType, $criteriaKey)) {
                        // Then use it
                        $value = $this->criteria[$criteriaType][$criteriaKey];
                } // END - if
index 419427a22617d19053759d2544e6365e90f3bbb0..8d770f3ee4d0a2030df70602362467540b04546d 100644 (file)
@@ -113,6 +113,37 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria {
        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
+        * @return      $isMatching             Whether the key/value is matching or excluded
+        */
+       public function isCriteriaMatching ($key, $value) {
+               // Get 'default' search value
+               $search = $this->getCriteriaElemnent($key);
+
+               // 'default' check
+               $isMatching = ((!is_null($search)) && ($search == $value));
+
+               // Get 'choice' search value (can be NULL or comma-separated string)
+               $search = $this->getCriteriaChoiceElemnent($key);
+
+               // 'choice' check
+               $isMatching = (($isMatching === TRUE) && ((is_null($search)) || (in_array($value, explode(',', $search)))));
+
+               // Get 'exclude' search value
+               $search = $this->getCriteriaExcludeElemnent($key);
+
+               // 'exclude' check
+               $isMatching = (($isMatching === TRUE) && ((is_null($search)) || ($search != $value)));
+
+               // Return result
+               return $isMatching;
+       }
 }
 
 // [EOF]
index 4d14624b90b5dec8da691dd0d594cbc95fb4e824..f4d855ff5e196faf08645e52e76e376eb1d4f505 100644 (file)
@@ -308,13 +308,13 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackendIn
         * Starts a SELECT query on the database by given return type, table name
         * and search criteria
         *
-        * @param       $tableName              Name of the database table
-        * @param       $criteria               Local search criteria class
-        * @return      $resultData             Result data of the query
+        * @param       $tableName                      Name of the database table
+        * @param       $searchInstance         Local search criteria class
+        * @return      $resultData                     Result data of the query
         * @throws      UnsupportedCriteriaException    If the criteria is unsupported
         * @throws      SqlException                                    If an 'SQL error' occurs
         */
-       public function querySelect ($tableName, LocalSearchCriteria $criteriaInstance) {
+       public function querySelect ($tableName, LocalSearchCriteria $searchInstance) {
                // The result is null by any errors
                $resultData = NULL;
 
@@ -339,7 +339,7 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackendIn
                        $idx = 1;
 
                        // Read the directory with some exceptions
-                       while (($dataFile = $directoryInstance->readDirectoryExcept(array('.', '..', '.htaccess', '.svn', 'info.' . $this->getFileExtension()))) && (($limitFound < $criteriaInstance->getLimit()) || ($criteriaInstance->getLimit() == 0))) {
+                       while (($dataFile = $directoryInstance->readDirectoryExcept(array('.', '..', '.htaccess', '.svn', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
                                // Does the extension match?
                                if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
                                        // Skip this file!
@@ -354,16 +354,12 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackendIn
                                if (is_array($dataArray)) {
                                        // Search in the criteria with FMFW (First Matches, First Wins)
                                        foreach ($dataArray as $key => $value) {
-                                               // Get criteria element
-                                               $criteria = $criteriaInstance->getCriteriaElemnent($key);
-
                                                // Is the criteria met or none set?
-                                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: criteria[' . gettype($criteria) . ']=' . $criteria . ';()=' . strlen($criteria) . ',criteriaInstance()=' . $criteriaInstance->count() . ',value(' . strlen($value) . ')=' . $value);
-                                               if (((!is_null($criteria)) && ($criteria == $value)) || ($criteriaInstance->count() == 0))  {
+                                               if ($searchInstance->isCriteriaMatching($key, $value)) {
                                                        // Shall we skip this entry?
-                                                       if ($criteriaInstance->getSkip() > 0) {
+                                                       if ($searchInstance->getSkip() > 0) {
                                                                // We shall skip some entries
-                                                               if ($skipFound < $criteriaInstance->getSkip()) {
+                                                               if ($skipFound < $searchInstance->getSkip()) {
                                                                        // Skip this entry
                                                                        $skipFound++;
                                                                        break;
@@ -463,7 +459,7 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackendIn
                        $skipFound = 0;
 
                        // Get the criteria array from the dataset
-                       $criteriaArray = $dataSetInstance->getCriteriaArray();
+                       $searchArray = $dataSetInstance->getCriteriaArray();
 
                        // Get search criteria
                        $searchInstance = $dataSetInstance->getSearchInstance();
@@ -486,12 +482,8 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackendIn
                                if (is_array($dataArray)) {
                                        // Search in the criteria with FMFW (First Matches, First Wins)
                                        foreach ($dataArray as $key => $value) {
-                                               // Get criteria element
-                                               $criteria = $searchInstance->getCriteriaElemnent($key);
-                                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',key=' . $key . ',criteria=' . $criteria);
-
                                                // Is the criteria met?
-                                               if (((!is_null($criteria)) && ($criteria == $value)) || ($searchInstance->count() == 0))  {
+                                               if ($searchInstance->isCriteriaMatching($key, $value))  {
                                                        // Shall we skip this entry?
                                                        if ($searchInstance->getSkip() > 0) {
                                                                // We shall skip some entries
@@ -503,9 +495,9 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackendIn
                                                        } // END - if
 
                                                        // Entry found, so update it
-                                                       foreach ($criteriaArray as $criteriaKey => $criteriaValue) {
-                                                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
-                                                               $dataArray[$criteriaKey] = $criteriaValue;
+                                                       foreach ($searchArray as $searchKey => $searchValue) {
+                                                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: criteriaKey=' . $searchKey . ',criteriaValue=' . $searchValue);
+                                                               $dataArray[$searchKey] = $searchValue;
                                                        } // END - foreach
 
                                                        // Write the data to a local file