]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/lists/class_BaseList.php
Fixed a typo, added an assert()
[hub.git] / application / hub / main / lists / class_BaseList.php
index 3755b2605d28b83deaa87499b014097aad1f4afc..5efe2baab27680566c38980eb4e7225aa675e9a0 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, 2010 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
  *
@@ -67,10 +67,10 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
        }
 
        /**
-        * Checks wether the given group is set
+        * Checks whether the given group is set
         *
         * @param       $groupName      Group to check if found in list
-        * @return      $isset          Wether the group is valid
+        * @return      $isset          Whether the group is valid
         */
        public function isGroupSet ($groupName) {
                //* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName);
@@ -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');
        }
 
        /**
@@ -154,12 +154,12 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
                // Is there another list?
                if (!is_null($list)) {
                        // Then get it as well
-                       $array = $this->listGroups[$list]->getArrayFromList(null);
+                       $array = $this->listGroups[$list]->getArrayFromList(NULL);
                } // END - if
 
                // Walk through all entries
                foreach ($this->listIndex as $hash) {
-                       //* DEBUG: */ print __METHOD__.':hash='.$hash."\n";
+                       //* DEBUG: */ print __METHOD__.':hash='.$hash.chr(10);
                        // Is the list entry set?
                        if ($this->isHashValid($hash)) {
                                // Add it
@@ -190,15 +190,15 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
 
                // Generate hash
                $hash = $this->generateHash($groupName, $groupName, $entry);
-               //* DEBUG: */ $this->debugOutput(__METHOD__ . ': groupName=' . $groupName . ', entry=' . $entry . ', hash=' . $hash);
+               //* DEBUG: */ $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']:  groupName=' . $groupName . ', entry=' . $entry . ', hash=' . $hash);
 
                // Add the hash to the index
                $this->listIndex[] = $hash;
-               //* DEBUG: */ print $groupName.'/'.count($this->listIndex)."\n";
+               //* DEBUG: */ print $groupName.'/'.count($this->listIndex).chr(10);
 
                // Now add the entry to the list
                $this->listEntries[$hash] = $entry;
-               //* DEBUG: */ print $groupName.'/'.count($this->listEntries)."\n";
+               //* DEBUG: */ print $groupName.'/'.count($this->listEntries).chr(10);
                //* DEBUG: */ $this->debugOutput(__METHOD__.'('.$this->__toString().'): '.$groupName . ' - FINISHED');
        }
 
@@ -220,7 +220,7 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
 
                // Generate hash
                $hash = $this->generateHash($groupName, $groupName, $entry);
-               //* DEBUG: */ $this->debugOutput(__METHOD__ . ': groupName=' . $groupName . ', entry=' . $entry . ', hash=' . $hash);
+               //* DEBUG: */ $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']:  groupName=' . $groupName . ', entry=' . $entry . ', hash=' . $hash);
 
                // Remove it from the list ...
                unset($this->listEntries[$hash]);
@@ -259,7 +259,7 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
                        $entry2 = crc32($entry['id']) . ':' . count($entry);
                } else {
                        // Unsupported type detected
-                       $this->debugOutut(__METHOD__ . ': entry type ' . gettype($entry) . ' is unsupported.');
+                       $this->debugOutut('[' . __METHOD__ . ':' . __LINE__ . ']:  entry type ' . gettype($entry) . ' is unsupported.');
 
                        // @TODO Extend this somehow?
                        $entry2 = gettype($entry);
@@ -275,6 +275,41 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
                return $hash;
        }
 
+       /**
+        * Clears an array of groups, all are being checked for existence
+        *
+        * @param       $groupNames             An array with existing list groups
+        * @return      void
+        */
+       protected function clearGroups (array $groupNames) {
+               // Walk through all groups
+               foreach ($groupNames as $groupName) {
+                       // Clear this group
+                       $this->clearGroup($groupName);
+               } // END - foreach
+       }
+
+       /**
+        * Clears a single group by resetting it to its initial state (empty array)
+        *
+        * @param       $groupName      Name of an existing group to clear
+        * @return      void
+        */
+       protected function clearGroup ($groupName) {
+               // Does this group exist?
+               if (!$this->isGroupSet($groupName)) {
+                       // Throw the exception here
+                       throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
+               } // END - if
+
+               // Then clear this group list
+               $this->listGroups[$groupName]->clearList();
+
+               // Clear this list
+               $this->listIndex = array();
+               $this->listEntries = array();
+       }
+       
        /**
         * Counts all entries in this list
         *
@@ -285,10 +320,10 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
        }
 
        /**
-        * Checks wether the given hash is valid
+        * Checks whether the given hash is valid
         *
         * @param       $hash           The hash we should validate
-        * @return      $isValid        Wether the given hash is valid
+        * @return      $isValid        Whether the given hash is valid
         */
        public final function isHashValid ($hash) {
                // Check it
@@ -336,6 +371,45 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
                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
         *
@@ -349,7 +423,7 @@ class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
                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;