]> git.mxchange.org Git - core.git/blobdiff - inc/classes/main/criteria/search/class_SearchCriteria.php
Rewrote search criteria matching:
[core.git] / inc / classes / main / criteria / search / class_SearchCriteria.php
index e921ab069e7e1a53c3b3f3fb2edabeef44a663b5..8d770f3ee4d0a2030df70602362467540b04546d 100644 (file)
@@ -6,7 +6,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 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,34 @@ 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
+        * @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)
-                       );
-               }
-
-               // Add limit and skip values
-               $cacheKey .= sprintf("%%limit%%=%s;%%skip%%=%s",
-                       $this->limit,
-                       $this->skip
-               );
-
-               // Return the cache key
-               return $cacheKey;
-       }
+       public function isCriteriaMatching ($key, $value) {
+               // Get 'default' search value
+               $search = $this->getCriteriaElemnent($key);
 
-       /**
-        * 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];
-               }
-
-               // Return the value
-               return $value;
-       }
+               // 'default' check
+               $isMatching = ((!is_null($search)) && ($search == $value));
 
-       /**
-        * 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
-                       } // END - foreach
-               } // END - foreach
-
-               // Now check if expected criteria counts match
-               $matches = ($counted == count($this->searchCriteria));
-
-               // Return the result
-               return $matches;
+               // 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;
        }
 }