]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/lists/class_BaseList.php
Move to generic 'core' project, because getIndexKey() is generic code
[hub.git] / application / hub / main / lists / class_BaseList.php
index 99306aca654d5252e7019735de4a79a59ce426c9..979a45a42793b3ca6206628382d8a8058418545a 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -60,7 +60,7 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
         */
        public function getIterator () {
                // Prepare a default iterator
-               $iteratorInstance = ObjectFactory::createObjectByConfiguredName('default_iterator_class');
+               $iteratorInstance = ObjectFactory::createObjectByConfiguredName('default_iterator_class', array($this));
 
                // And return it
                return $iteratorInstance;
@@ -131,7 +131,7 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
 
                // Add the instance itself to the list
                $this->listEntries[$hash] = $instance;
-               //* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName  . '/' . $subGroup . ' - START');
+               //* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName  . '/' . $subGroup . ' - FINISHED');
        }
 
        /**
@@ -190,7 +190,7 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
 
                // Generate hash
                $hash = $this->generateHash($groupName, $groupName, $entry);
-               //* DEBUG: */ $this->debugOutput(__METHOD__.'('.$this->__toString().'): hash='.$hash.'');
+               //* DEBUG: */ $this->debugOutput(__METHOD__ . ': groupName=' . $groupName . ', entry=' . $entry . ', hash=' . $hash);
 
                // Add the hash to the index
                $this->listIndex[] = $hash;
@@ -202,6 +202,34 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
                //* DEBUG: */ $this->debugOutput(__METHOD__.'('.$this->__toString().'): '.$groupName . ' - FINISHED');
        }
 
+       /**
+        * Removes given entry from the list group
+        *
+        * @param       $groupName      Group where we should remove the entry from
+        * @param       $entry          The entry we should remove
+        * @return      void
+        * @throws      NoListGroupException    If the given group is not found
+        */
+       public function removeEntry ($groupName, $entry) {
+               //* DEBUG: */ $this->debugOutput(__METHOD__.'('.$this->__toString().'): '.$groupName . ' - START');
+               // Is the group already added?
+               if (!$this->isGroupSet($groupName)) {
+                       // Throw the exception here
+                       throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
+               } // END - if
+
+               // Generate hash
+               $hash = $this->generateHash($groupName, $groupName, $entry);
+               //* DEBUG: */ $this->debugOutput(__METHOD__ . ': groupName=' . $groupName . ', entry=' . $entry . ', hash=' . $hash);
+
+               // Remove it from the list ...
+               unset($this->listEntries[$hash]);
+
+               // ... and hash list as well
+               unset($this->listIndex[array_search($hash, $this->listIndex)]);
+               //* DEBUG: */ $this->debugOutput(__METHOD__.'('.$this->__toString().'): '.$groupName . ' - FINISHED');
+       }
+
        /**
         * Generates a hash from given group, sub group and entry
         *
@@ -226,6 +254,9 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
                } elseif (is_string($entry)) {
                        // String found
                        $entry2 = crc32($entry) . ':' . strlen($entry);
+               } elseif ((is_array($entry)) && (isset($entry['id']))) {
+                       // Supported array found
+                       $entry2 = crc32($entry['id']) . ':' . count($entry);
                } else {
                        // Unsupported type detected
                        $this->debugOutut(__METHOD__ . ': entry type ' . gettype($entry) . ' is unsupported.');
@@ -267,16 +298,30 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
                return $isValid;
        }
 
+       /**
+        * Getter for hash from given hash index
+        *
+        * @param       $hashIndex      Index holding the hash
+        * @return      $hash           The hash
+        */
+       public final function getHash ($hashIndex) {
+               // Get it ...
+               $hash = $this->listIndex[$hashIndex];
+
+               // ... and return it
+               return $hash;
+       }
+
        /**
         * Gets an entry from given hash index
         *
         * @param       $hashIndex      The hash index to resolve the mapped entry
         * @return      $entry          Solved entry from list
-        * @throws      InvalidListHashException If the solved hash index is invalid
+        * @throws      InvalidListHashException        If the solved hash index is invalid
         */
        public function getEntry ($hashIndex) {
                // Get the hash value
-               $hash = $this->listIndex[$hashIndex];
+               $hash = $this->getHash($hashIndex);
 
                // Is the hash valid?
                if (!$this->isHashValid($hash)) {
@@ -290,6 +335,64 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
                // Return it
                return $entry;
        }
+
+       /**
+        * Gets a full array from given group name
+        *
+        * @param       $groupName      The group name to get a list for
+        * @return      $entries        The array with all entries
+        * @throws      NoListGroupException    If the specified group is invalid
+        */
+       public function getArrayFromGroup ($groupName) {
+               // Is the group valid?
+               if (!$this->isGroupSet($groupName)) {
+                       // Throw the exception here
+                       throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
+               } // END - if
+
+               // Init the entries' array
+               $entries = array();
+
+               // Get an iterator
+               $iteratorInstance = $this->listGroups[$groupName]->getIterator();
+
+               // Go through all entries
+               while ($iteratorInstance->valid()) {
+                       // Get key
+                       $entryIndex = $iteratorInstance->key();
+
+                       // ... and the final entry which is the stored instance
+                       $entry = $this->getEntry($entryIndex);
+
+                       // Add it to the list
+                       $entries[$iteratorInstance->current()] = $entry;
+
+                       // Skip to next one
+                       $iteratorInstance->next();
+               } // END - while
+
+               // Return the list
+               return $entries;
+       }
+
+       /**
+        * Updates the given entry by hash with given array
+        *
+        * @param       $hash           Hash for this entry
+        * @param       $entryArray     Array with entry we should update
+        * @return      void
+        * @throws      InvalidListHashException        If the solved hash index is invalid
+        */
+       public function updateCurrentEntryByHash ($hash, array $entryArray) {
+               // Is the hash valid?
+               if (!$this->isHashValid($hash)) {
+                       // Throw an exception here, hashIndex is unknown at this point
+                       throw new InvalidListHashException(array($this, $hash, -999), self::EXCEPTION_INVALID_HASH);
+               } // END - if
+
+               // Set the entry
+               $this->listEntries[$hash] = $entryArray;
+       }
 }
 
 //