*
* @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
*
* 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;
}
/**
- * 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);
// 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');
+ }
+
+ /**
+ * 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.chr(10);
+ // 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;
}
/**
* @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__ . ':' . __LINE__ . ']: groupName=' . $groupName . ', entry=' . $entry . ', hash=' . $hash);
// Add the hash to the index
$this->listIndex[] = $hash;
+ //* DEBUG: */ print $groupName.'/'.count($this->listIndex).chr(10);
// Now add the entry to the list
$this->listEntries[$hash] = $entry;
- //* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName . ' - FINISHED');
+ //* DEBUG: */ print $groupName.'/'.count($this->listEntries).chr(10);
+ //* 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__ . ':' . __LINE__ . ']: 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');
}
/**
} 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__ . ':' . __LINE__ . ']: entry type ' . gettype($entry) . ' is unsupported.');
// @TODO Extend this somehow?
$entry2 = gettype($entry);
// And return it
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
+ *
+ * @return $count All entries in this list
+ */
+ public final function count () {
+ return count($this->listIndex);
+ }
+
+ /**
+ * Checks whether the given hash is valid
+ *
+ * @param $hash The hash we should validate
+ * @return $isValid Whether 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;
+ }
+
+ /**
+ * 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;
+ }
}
//