]> git.mxchange.org Git - shipsimu.git/commitdiff
Setting index is now much nicer (more abstract!)
authorRoland Häder <roland@mxchange.org>
Fri, 14 Nov 2008 01:32:37 +0000 (01:32 +0000)
committerRoland Häder <roland@mxchange.org>
Fri, 14 Nov 2008 01:32:37 +0000 (01:32 +0000)
inc/classes/interfaces/result/class_SearchableResult.php
inc/classes/main/criteria/class_SearchCriteria.php
inc/classes/main/database/databases/class_LocalFileDatabase.php
inc/classes/main/database/wrapper/class_UserDatabaseWrapper.php
inc/classes/main/filter/verifier/class_UserGuestVerifierFilter.php
inc/classes/main/result/class_DatabaseResult.php
inc/classes/main/user/class_BaseUser.php
inc/classes/middleware/database/class_DatabaseConnection.php

index c2c78239e0c7d3c4d6b8ddf038694dc98b992f7c..c3bf26bf764ca256ef605401d06b4ce2617a4cc0 100644 (file)
@@ -29,6 +29,15 @@ interface SearchableResult extends FrameworkInterface {
         * @return      $result                         Found result entry
         */
        function searchEntry (LocalSearchCriteria $criteriaInstance);
+
+       /**
+        * Solver for result index value with call-back method
+        *
+        * @param       $databaseColumn         Database column where the index might be found
+        * @para        $callBack                       Call-back object for setting the index
+        * @return      void
+        */
+       function solveResultIndex ($databaseColumn, BaseDatabaseWrapper $wrapperInstance, array $callBack);
 }
 
 // [EOF]
index c95d853bfd07b67bbc7f518317300b1b3208cfe0..0e76ede72519815213c2d9375d16c15e8ab0a5b9 100644 (file)
@@ -73,8 +73,8 @@ class SearchCriteria extends BaseFrameworkSystem implements LocalSearchCriteria
         * @param       $criteriaValue  Criteria value
         * @return      void
         */
-       public function addCriteria ($criteriaKey, $criteriaValue) {
-               $this->searchCriteria[$criteriaKey] = $criteriaValue;
+       public final function addCriteria ($criteriaKey, $criteriaValue) {
+               $this->searchCriteria[(string)$criteriaKey] = (string)$criteriaValue;
        }
 
        /**
@@ -84,7 +84,7 @@ class SearchCriteria extends BaseFrameworkSystem implements LocalSearchCriteria
         * @param       $configEntry    Configuration entry
         * @return      void
         */
-       public function addConfiguredCriteria ($criteriaKey, $configEntry) {
+       public final function addConfiguredCriteria ($criteriaKey, $configEntry) {
                // Add the configuration entry as a criteria
                $value = $this->getConfigInstance()->readConfig($configEntry);
                $this->addCriteria($criteriaKey, $value);
@@ -95,6 +95,7 @@ class SearchCriteria extends BaseFrameworkSystem implements LocalSearchCriteria
         *
         * @param       $limit  Search limit
         * @return      void
+        * @todo        Find a nice casting here. (int) allows until and including 32766.
         */
        public final function setLimit ($limit) {
                $this->limit = $limit;
@@ -114,6 +115,7 @@ class SearchCriteria extends BaseFrameworkSystem implements LocalSearchCriteria
         *
         * @param       $skip   Search skip
         * @return      void
+        * @todo        Find a nice casting here. (int) allows until and including 32766.
         */
        public final function setSkip ($skip) {
                $this->skip = $skip;
index 96b5c2291eab729c88552c13a6db66c0ce6af999..29e2e5e0f397f3dab498d8dc80ce70565a1598ce 100644 (file)
@@ -70,6 +70,11 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
         */
        private $tableInfo = array();
 
+       /**
+        * Element for index
+        */
+       private $indexKey = "__idx";
+
        /**
         * The protected constructor. Do never instance from outside! You need to
         * set a local file path. The class will then validate it.
@@ -210,6 +215,15 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
                return $this->fileExtension;
        }
 
+       /**
+        * Getter for index key
+        *
+        * @return      $indexKey       Index key
+        */
+       public final function getIndexKey () {
+               return $this->indexKey;
+       }
+
        /**
         * Reads a local data file  and returns it's contents in an array
         *
@@ -402,7 +416,7 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend
                                                        } // END - if
 
                                                        // Set id number
-                                                       $dataArray['__idx'] = $idx;
+                                                       $dataArray[$this->getIndexKey()] = $idx;
 
                                                        // Entry found!
                                                        $resultData['rows'][] = $dataArray;
index 0406bb2172e25e57c11372be31696b55852c0713..39e128387bb4820aae24f52f7cbb0bdd3587ff31 100644 (file)
@@ -63,6 +63,15 @@ class UserDatabaseWrapper extends BaseDatabaseWrapper {
                return $wrapperInstance;
        }
 
+       /**
+        * Getter for index key
+        *
+        * @return      $indexKey       Index key
+        */
+       public final function getIndexKey () {
+               return $this->getDatabaseInstance()->getIndexKey();
+       }
+
        /**
         * Handles inserting the registration data from a registration instance into the database
         *
index 839b1eb96536330476500fdf9dedde17f0e5e056..75e00bd91c5a311cf80ecdb7662a7dd2a4ab3360 100644 (file)
@@ -65,7 +65,7 @@ class UserGuestVerifierFilter extends BaseFilter implements Filterable {
                        $requestInstance->requestIsValid(false);
 
                        // Add a message to the response
-                       $responseInstance->addFatalMessage('username_unset');
+                       $responseInstance->addFatalMessage('username_guest_unset');
 
                        // Abort here
                        return false;
@@ -74,7 +74,7 @@ class UserGuestVerifierFilter extends BaseFilter implements Filterable {
                        $requestInstance->requestIsValid(false);
 
                        // Add a message to the response
-                       $responseInstance->addFatalMessage('username_empty');
+                       $responseInstance->addFatalMessage('username_guest_empty');
 
                        // Abort here
                        return false;
@@ -83,7 +83,7 @@ class UserGuestVerifierFilter extends BaseFilter implements Filterable {
                        $requestInstance->requestIsValid(false);
 
                        // Add a message to the response
-                       $responseInstance->addFatalMessage('username_not_found');
+                       $responseInstance->addFatalMessage('username_guest_not_found');
 
                        // Abort here
                        return false;
index 3f8918b79da8c54dc29f1dfad2ddde98cb5857f5..603f8cb3f94be26356f199fd4bcbd8797a80dab6 100644 (file)
@@ -51,6 +51,11 @@ class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, Up
         */
        private $affectedRows = 0;
 
+       /**
+        * Found value
+        */
+       private $foundValue = "";
+
        /**
         * Protected constructor
         *
@@ -208,12 +213,13 @@ class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, Up
        }
 
        /**
-        * Rewind to the beginning
+        * Rewind to the beginning and clear array $currentRow
         *
         * @return      void
         */
        public function rewind () {
                $this->currentPos = -1;
+               $this->currentRow = array();
        }
 
        /**
@@ -291,6 +297,15 @@ class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, Up
                return $this->affectedRows;
        }
 
+       /**
+        * Getter for found value of previous found() call
+        *
+        * @return      $foundValue             Found value of previous found() call
+        */
+       public final function getFoundValue () {
+               return $this->foundValue;
+       }
+
        /**
         * Checks wether we have out-dated entries or not
         *
@@ -308,25 +323,80 @@ class DatabaseResult extends BaseFrameworkSystem implements SearchableResult, Up
         * @return      void
         */
        public function addElementsToDataSet (StoreableCriteria $criteriaInstance) {
+               // Walk only through out-dated columns
+               foreach ($this->outDated as $key => $dummy) {
+                       // Does this key exist?
+                       //* DEBUG: */ echo "outDated: {$key}<br />\n";
+                       if ($this->find($key)) {
+                               // Then update it
+                               $criteriaInstance->addCriteria($key, $this->getFoundValue());
+                       } // END - if
+               } // END - foreach
+       }
+
+       /**
+        * Find a key inside the result array
+        *
+        * @param       $key    The key we shall find
+        * @return      $found  Wether the key was found or not
+        */
+       public function find ($key) {
+               // By default nothing is found
+               $found = false;
+
                // Rewind the pointer
                $this->rewind();
 
                // Walk through all entries
                while ($this->valid()) {
-                       // Get next entry
+                       // Advance to next entry
                        $this->next();
+
+                       // Get the whole array
                        $currentEntry = $this->current();
 
-                       // Walk only through out-dated columns
-                       foreach ($this->outDated as $key => $dummy) {
-                               // Does this key exist?
-                               //* DEBUG: */ echo "outDated: {$key}<br />\n";
-                               if (isset($currentEntry[$key])) {
-                                       // Then update it
-                                       $criteriaInstance->addCriteria($key, $currentEntry[$key]);
-                               } // END - foreach
-                       } // END - foreach
+                       // Is the element there?
+                       if (isset($currentEntry[$key])) {
+                               // Okay, found!
+                               $found = true;
+
+                               // So "cache" it
+                               $this->foundValue = $currentEntry[$key];
+
+                               // And stop searching
+                               break;
+                       } // END - if
                } // END - while
+
+               // Return the result
+               return $found;
+       }
+
+       /**
+        * Solver for result index value with call-back method
+        *
+        * @param       $databaseColumn         Database column where the index might be found
+        * @param       $wrapperInstance        The wrapper instance to ask for array element
+        * @para        $callBack                       Call-back object for setting the index;
+        *                                                              0=object instance,1=method name
+        * @return      void
+4       * @todo        Find a caching way without modifying the result array
+        */
+       public function solveResultIndex ($databaseColumn, BaseDatabaseWrapper $wrapperInstance, array $callBack) {
+               // By default nothing is found
+               $indexValue = 0;
+
+               // Is the element in result itself found?
+               if ($this->find($databaseColumn)) {
+                       // Use this value
+                       $indexValue = $this->getFoundValue();
+               } elseif ($this->find($wrapperInstance->getIndexKey())) {
+                       // Use this value
+                       $indexValue = $this->getFoundValue();
+               }
+
+               // Set the index
+               call_user_func_array($callBack, array($indexValue));
        }
 }
 
index b3fc24d717409210fba85d3165f371ca220a658b..0e3dba9eebad03f3eda5152a232ec2278da47f18 100644 (file)
@@ -134,15 +134,20 @@ class BaseUser extends BaseFrameworkSystem {
 
                        // Add the username as a criteria and set limit to one entry
                        $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
-                       $criteriaInstance->setIndexSolution(UserDatabaseWrapper::DB_COLUMN_USERID, array($this, 'setUserId'));
                        $criteriaInstance->setLimit(1);
 
                        // Get a search result
-                       $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
-               } else {
-                       // Rewind it
-                       $this->getResultInstance()->rewind();
-               }
+                       $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
+
+                       // Set the index "solver"
+                       $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
+
+                       // And finally set it
+                       $this->setResultInstance($resultInstance);
+               } // END - if
+
+               // Rewind it
+               $this->getResultInstance()->rewind();
 
                // Search for it
                if ($this->getResultInstance()->next()) {
@@ -173,15 +178,20 @@ class BaseUser extends BaseFrameworkSystem {
 
                        // Add the username as a criteria and set limit to one entry
                        $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
-                       $criteriaInstance->setIndexSolution(UserDatabaseWrapper::DB_COLUMN_USERID, array($this, 'setUserId'));
                        $criteriaInstance->setLimit(1);
 
-                       // Get a search resultInstance
-                       $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
-               } else {
-                       // Rewind it
-                       $this->getResultInstance()->rewind();
-               }
+                       // Get a search result
+                       $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
+
+                       // Set the index "solver"
+                       $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
+
+                       // And finally set it
+                       $this->setResultInstance($resultInstance);
+               } // END - if
+
+               // Rewind it
+               $this->getResultInstance()->rewind();
 
                // Search for it
                if ($this->getResultInstance()->next()) {
@@ -213,28 +223,36 @@ class BaseUser extends BaseFrameworkSystem {
                // By default nothing matches... ;)
                $matches = false;
 
-               // Get a UserDatabaseWrapper instance
-               $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
+               // Is a previous result there?
+               if (is_null($this->getResultInstance())) {
+                       // Get a UserDatabaseWrapper instance
+                       $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
 
-               // Create a search criteria
-               $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+                       // Create a search criteria
+                       $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
 
-               // Add the username as a criteria and set limit to one entry
-               $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
-               $criteriaInstance->setIndexSolution(UserDatabaseWrapper::DB_COLUMN_USERID, array($this, 'setUserId'));
-               $criteriaInstance->setLimit(1);
+                       // Add the username as a criteria and set limit to one entry
+                       $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
+                       $criteriaInstance->setLimit(1);
 
-               // Get a search resultInstance
-               $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
+                       // Get a search result
+                       $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
 
-               // Search for it
-               if ($this->getResultInstance()->next()) {
-                       // Get the current entry (can only be one!)
-                       $entry = $this->getResultInstance()->current();
+                       // Set the index "solver"
+                       $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
+
+                       // And finally set it
+                       $this->setResultInstance($resultInstance);
+               } // END - if
+
+               // Rewind it
+               $this->getResultInstance()->rewind();
 
+               // Search for it
+               if ($this->getResultInstance()->find('pass_hash')) {
                        // So does the hashes match?
                        //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash')."/".$entry['pass_hash'];
-                       $matches = ($requestInstance->getRequestElement('pass_hash') === $entry['pass_hash']);
+                       $matches = ($requestInstance->getRequestElement('pass_hash') === $this->getResultInstance()->getFoundValue());
                } // END - if
 
                // Return the status
index 736ea4b501c14eb7261912180802c355d49049e1..6f0a4e1ef2660a5b0664689a5aaf11feb749b591 100644 (file)
@@ -98,6 +98,15 @@ class DatabaseConnection extends BaseMiddleware implements DatabaseConnector, Re
                $this->dbLayer = $dbLayer;
        }
 
+       /**
+        * Getter for index key
+        *
+        * @return      $indexKey       Index key
+        */
+       public final function getIndexKey () {
+               return $this->dbLayer->getIndexKey();
+       }
+
        /**
         * Runs a "select" statement on the database layer with given table name
         * and criteria. If this doesn't fail the result will be returned