]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/lists/class_BaseList.php
Iterator continued (not fully implemented), iteration on all clients and hubs should...
[hub.git] / application / hub / main / lists / class_BaseList.php
index c81063933225d54c66bcce029fff4aa6e07c39f4..fc6969ae6943d100a368f9ddb57a6c9d81705c74 100644 (file)
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-class BaseList extends BaseHubSystem implements IteratorAggregate {
+class BaseList extends BaseHubSystem implements IteratorAggregate, Countable {
        // Exception constants
        const EXCEPTION_GROUP_ALREADY_ADDED = 0xf20;
        const EXCEPTION_GROUP_NOT_FOUND     = 0xf21;
+       const EXCEPTION_INVALID_HASH        = 0xf22;
 
        /**
         * List groups array
@@ -182,12 +183,15 @@ class BaseList extends BaseHubSystem implements IteratorAggregate {
                } elseif ($entry instanceof FrameworkInterface) {
                        // Own instance detected
                        $entry2 = $entry->hashCode();
-               } elseif (!is_array($entry)) {
-                       // Non-array found, use it directly with type
-                       $entry2 = gettype($entry) . ':' . $entry2;
+               } elseif ((is_int($entry)) || (is_float($entry)) || (is_resource($entry))) {
+                       // Integer/float/resource detected
+                       $entry2 = gettype($entry) . ':' . $entry;
+               } elseif (is_string($entry)) {
+                       // String found
+                       $entry2 = crc32($entry) . ':' . strlen($entry);
                } else {
-                       // Arrays are unsupported at the momement
-                       $this->debugOutut(__METHOD__ . ': entry is an array. UNSUPPORTED!');
+                       // Unsupported type detected
+                       $this->debugOutut(__METHOD__ . ': entry type ' . gettype($entry) . ' is unsupported.');
 
                        // @TODO Extend this somehow?
                        $entry2 = gettype($entry);
@@ -202,6 +206,53 @@ class BaseList extends BaseHubSystem implements IteratorAggregate {
                // And return it
                return $hash;
        }
+
+       /**
+        * Counts all entries in this list
+        *
+        * @return      $count  All entries in this list
+        */
+       public final function count () {
+               return count($this->listIndex);
+       }
+
+       /**
+        * Checks wether the given hash is valid
+        *
+        * @param       $hash           The hash we should validate
+        * @return      $isValid        Wether the given hash is valid
+        */
+       public final function isHashValid ($hash) {
+               // Check it
+               $isValid = ((in_array($hash, $this->listIndex)) && (isset($this->listEntries[$hash])));
+
+               // Return the result
+               return $isValid;
+       }
+
+       /**
+        * 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
+        */
+       public function getEntry ($hashIndex) {
+               // Get the hash value
+               $hash = $this->listIndex[$hashIndex];
+
+               // Is the hash valid?
+               if (!$this->isHashValid($hash)) {
+                       // Throw an exception here
+                       throw new InvalidListHashException(array($this, $hash, $hashIndex), self::EXCEPTION_INVALID_HASH);
+               } // END - if
+
+               // Now copy the entry
+               $entry = $this->listEntries[$hash];
+
+               // Return it
+               return $entry;
+       }
 }
 
 //