* 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
protected function __construct ($className) {
// Call parent constructor
parent::__construct($className);
-
- // Remove some attributes
- $this->removeNumberFormaters();
- $this->removeSystemArray();
}
/**
*/
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;
//* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName . '/' . $subGroup . ' - START');
}
+ /**
+ * Gets an array from given list
+ *
+ * @param $list The requested list
+ * @return $array The requested array
+ * @throws NoListGroupException If the given group is not found
+ */
+ public final function getArrayFromList ($list) {
+ // Is the group there?
+ if ((!is_null($list)) && (!$this->isGroupSet($list))) {
+ // Throw the exception here
+ throw new NoListGroupException(array($this, $list), self::EXCEPTION_GROUP_NOT_FOUND);
+ } // END - if
+
+ // Init array
+ $array = array();
+
+ // Is there another list?
+ if (!is_null($list)) {
+ // Then get it as well
+ $array = $this->listGroups[$list]->getArrayFromList(null);
+ } // END - if
+
+ // Walk through all entries
+ foreach ($this->listIndex as $hash) {
+ //* DEBUG: */ print __METHOD__.':hash='.$hash."\n";
+ // Is the list entry set?
+ if ($this->isHashValid($hash)) {
+ // Add it
+ $array[] = $this->listEntries[$hash];
+ //* DEBUG: */ print __METHOD__.": ADDED!\n";
+ } // END - if
+ } // END - foreach
+
+ // Return it
+ return $array;
+ }
+
/**
* Adds the given entry to list group
*
* @throws NoListGroupException If the given group is not found
*/
public function addEntry ($groupName, $entry) {
- //* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName . ' - START');
+ //* DEBUG: */ $this->debugOutput(__METHOD__.'('.$this->__toString().'): '.$groupName . ' - START');
// Is the group already added?
if (!$this->isGroupSet($groupName)) {
// Throw the exception here
// Generate hash
$hash = $this->generateHash($groupName, $groupName, $entry);
+ //* DEBUG: */ $this->debugOutput(__METHOD__.'('.$this->__toString().'): hash='.$hash.'');
// Add the hash to the index
$this->listIndex[] = $hash;
+ //* DEBUG: */ print $groupName.'/'.count($this->listIndex)."\n";
// Now add the entry to the list
$this->listEntries[$hash] = $entry;
- //* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName . ' - FINISHED');
+ //* DEBUG: */ print $groupName.'/'.count($this->listEntries)."\n";
+ //* DEBUG: */ $this->debugOutput(__METHOD__.'('.$this->__toString().'): '.$groupName . ' - FINISHED');
}
/**
} 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);
+ } elseif ((is_array($entry)) && (isset($entry['id']))) {
+ // Supported array found
+ $entry2 = crc32($entry['id']) . ':' . count($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);
// 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;
+ }
+
+ /**
+ * 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
+ */
+ public function getEntry ($hashIndex) {
+ // Get the hash value
+ $hash = $this->getHash($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;
+ }
+
+ /**
+ * 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);
+ }
+
+ // Set the entry
+ $this->listEntries[$hash] = $entryArray;
+ }
}
//