3 namespace CoreFramework\Lists;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Generic\FrameworkInterface;
8 use CoreFramework\Object\BaseFrameworkSystem;
9 use CoreFramework\Visitor\Visitable;
12 use \IteratorAggregate;
16 * A general list class
18 * @author Roland Haeder <webmaster@shipsimu.org>
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.shipsimu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countable {
38 // Exception constants
39 const EXCEPTION_GROUP_ALREADY_ADDED = 0xf20;
40 const EXCEPTION_GROUP_NOT_FOUND = 0xf21;
41 const EXCEPTION_INVALID_HASH = 0xf22;
46 private $callbackInstance = NULL;
51 private $listGroups = array();
56 private $listEntries = array();
61 private $listIndex = array();
64 * Protected constructor
66 * @param $className Name of the class
69 protected function __construct ($className) {
70 // Call parent constructor
71 parent::__construct($className);
75 * Setter for call-back instance
77 * @param $callbackInstance An instance of a FrameworkInterface class
80 public final function setCallbackInstance (FrameworkInterface $callbackInstance) {
81 $this->callbackInstance = $callbackInstance;
85 * Getter for iterator instance from this list
87 * @return $iteratorInstance An instance of a Iterator class
89 public function getIterator () {
90 // Get iterator from here
91 $iteratorInstance = $this->getIteratorInstance();
93 // Is the instance set?
94 if (is_null($iteratorInstance)) {
95 // Prepare a default iterator
96 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('default_iterator_class', array($this));
99 $this->setIteratorInstance($iteratorInstance);
103 return $iteratorInstance;
107 * Checks whether the given group is set
109 * @param $groupName Group to check if found in list
110 * @return $isset Whether the group is valid
112 public function isGroupSet ($groupName) {
113 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName);
114 return isset($this->listGroups[$groupName]);
118 * Adds the given group or if already added issues a ListGroupAlreadyAddedException
120 * @param $groupName Group to add
122 * @throws ListGroupAlreadyAddedException If the given group is already added
124 public function addGroup ($groupName) {
125 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!');
126 // Is the group already added?
127 if ($this->isGroupSet($groupName)) {
128 // Throw the exception here
129 throw new ListGroupAlreadyAddedException(array($this, $groupName), self::EXCEPTION_GROUP_ALREADY_ADDED);
132 // Add the group which is a simple array
133 $this->listGroups[$groupName] = ObjectFactory::createObjectByConfiguredName('list_group_class');
134 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - EXIT!');
138 * Adds the given instance to list group and sub group
140 * @param $groupName Group to add instance to
141 * @param $subGroup Sub group to add instance to
142 * @param $visitableInstance An instance of Visitable
144 * @throws NoListGroupException If the given group is not found
146 public function addInstance ($groupName, $subGroup, Visitable $visitableInstance) {
148 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',subGroup=' . $subGroup . ',visitableInstance=' . $visitableInstance->__toString() . ' - CALLED!');
150 // Is the group there?
151 if (!$this->isGroupSet($groupName)) {
152 // Throw the exception here
153 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
156 // Is the sub group there?
157 if (!$this->listGroups[$groupName]->isGroupSet($subGroup)) {
158 // Automatically add it
159 $this->listGroups[$groupName]->addGroup($subGroup);
163 $hash = $this->generateHash($groupName, $subGroup, $visitableInstance);
166 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',subGroup=' . $subGroup . ',hash=' . $hash . ' - Calling addEntry() ...');
168 // Now add it to the group list and hash it
169 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',this->listGroups[' . $groupName . ']=' . $this->listGroups[$groupName]->__toString());
170 //$this->listGroups[$groupName]->addEntry($subGroup, $hash);
172 // Add the hash to the index
173 array_push($this->listIndex, $hash);
175 // Add the instance itself to the list
176 $this->listEntries[$hash] = $visitableInstance;
179 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',subGroup=' . $subGroup . ' - EXIT!');
183 * Gets an array from given list
185 * @param $list The requested list
186 * @return $array The requested array
187 * @throws NoListGroupException If the given group is not found
189 public final function getArrayFromList ($list) {
191 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',list[' . gettype($list) . ']=' . $list . ' - CALLED!');
193 // Is the group there?
194 if ((!is_null($list)) && (!$this->isGroupSet($list))) {
195 // Throw the exception here
196 throw new NoListGroupException(array($this, $list), self::EXCEPTION_GROUP_NOT_FOUND);
202 // Is there another list?
203 if (!is_null($list)) {
204 // Then get it as well
205 $array = $this->listGroups[$list]->getArrayFromList(NULL);
208 // Walk through all entries
209 foreach ($this->listIndex as $hash) {
211 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: hash=' . $hash);
213 // Is the list entry set?
214 if ($this->isHashValid($hash)) {
216 array_push($array, $this->listEntries[$hash]);
217 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: hash=' . $hash . ',array(' . count($array) . ')=' . print_r($array, true) . ' - ADDED!');
222 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',list[' . gettype($list) . ']=' . $list . ',array()=' . count($array) . ' - EXIT!');
229 * Adds the given entry to list group
231 * @param $groupName Group to add instance to
232 * @param $entry An entry of any type
234 * @throws NoListGroupException If the given group is not found
236 public function addEntry ($groupName, $entry) {
238 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!');
240 // Is the group already added?
241 if (!$this->isGroupSet($groupName)) {
242 // Throw the exception here
243 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
247 $hash = $this->generateHash($groupName, $groupName, $entry);
250 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',entry=' . print_r($entry, true) . ', hash=' . $hash);
252 // Add the hash to the index
253 array_push($this->listIndex, $hash);
256 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',listEntries()=' . count($this->listEntries));
258 // Now add the entry to the list
259 $this->listEntries[$hash] = $entry;
262 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',listEntries()=' . count($this->listEntries) . ' - EXIT!');
266 * Removes given entry from the list group
268 * @param $groupName Group where we should remove the entry from
269 * @param $entry The entry we should remove
271 * @throws NoListGroupException If the given group is not found
273 public function removeEntry ($groupName, $entry) {
275 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!');
277 // Is the group already added?
278 if (!$this->isGroupSet($groupName)) {
279 // Throw the exception here
280 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
284 $hash = $this->generateHash($groupName, $groupName, $entry);
287 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',entry=' . $entry . ', hash=' . $hash);
289 // Remove it from the list ...
290 unset($this->listEntries[$hash]);
292 // ... and hash list as well
293 unset($this->listIndex[array_search($hash, $this->listIndex)]);
296 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - EXIT!');
300 * Generates a hash from given group, sub group and entry
302 * @param $groupName Group to add instance to
303 * @param $subGroup Sub group to add instance to
304 * @param $entry An entry of any type
305 * @return $hash The generated
307 private function generateHash ($groupName, $subGroup, $entry) {
308 // Created entry, 'null' is default
311 // Determine type of entry
312 if (is_null($entry)) {
314 } elseif ($entry instanceof FrameworkInterface) {
315 // Own instance detected
316 $entry2 = $entry->hashCode();
317 } elseif ((is_int($entry)) || (is_float($entry)) || (is_resource($entry))) {
318 // Integer/float/resource detected
319 $entry2 = gettype($entry) . ':' . $entry;
320 } elseif (is_string($entry)) {
322 $entry2 = crc32($entry) . ':' . strlen($entry);
323 } elseif ((is_array($entry)) && (isset($entry['id']))) {
324 // Supported array found
325 $entry2 = crc32($entry['id']) . ':' . count($entry);
326 } elseif (($this->callbackInstance instanceof FrameworkInterface) && (is_callable(array($this->callbackInstance, 'generateListHashFromEntry')))) {
328 $entry2 = $this->callbackInstance->generateListHashFromEntry($entry);
330 // Unsupported type detected
331 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST[' . __METHOD__ . ':' . __LINE__ . ']: Entry type ' . gettype($entry) . ' is unsupported.');
333 // At least take all keys from array
334 $entry2 = gettype($entry) . ':' . implode(':', array_keys($entry));
337 // Construct string which we shall hash
338 $hashString = $groupName . ':' . $subGroup . ':' . $entry2;
340 // Hash it with fastest hasher
341 $hash = crc32($hashString);
348 * Clears an array of groups, all are being checked for existence
350 * @param $groupNames An array with existing list groups
353 protected function clearGroups (array $groupNames) {
354 // Walk through all groups
355 foreach ($groupNames as $groupName) {
357 $this->clearGroup($groupName);
362 * Clears a single group by resetting it to its initial state (empty array)
364 * @param $groupName Name of an existing group to clear
367 protected function clearGroup ($groupName) {
368 // Does this group exist?
369 if (!$this->isGroupSet($groupName)) {
370 // Throw the exception here
371 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
374 // Then clear this group list
375 $this->listGroups[$groupName]->clearList();
378 $this->listIndex = array();
379 $this->listEntries = array();
383 * Counts all entries in this list
385 * @return $count All entries in this list
387 public final function count () {
388 return count($this->listIndex);
392 * Checks whether the given hash is valid
394 * @param $hash The hash we should validate
395 * @return $isValid Whether the given hash is valid
397 public final function isHashValid ($hash) {
399 $isValid = ((in_array($hash, $this->listIndex)) && (isset($this->listEntries[$hash])));
406 * Getter for hash from given hash index
408 * @param $hashIndex Index holding the hash
409 * @return $hash The hash
411 public final function getHash ($hashIndex) {
413 $hash = $this->listIndex[$hashIndex];
420 * Gets an entry from given hash index
422 * @param $hashIndex The hash index to resolve the mapped entry
423 * @return $entry Solved entry from list
424 * @throws InvalidListHashException If the solved hash index is invalid
426 public function getEntry ($hashIndex) {
427 // Get the hash value
428 $hash = $this->getHash($hashIndex);
430 // Is the hash valid?
431 if (!$this->isHashValid($hash)) {
432 // Throw an exception here
433 throw new InvalidListHashException(array($this, $hash, $hashIndex), self::EXCEPTION_INVALID_HASH);
436 // Now copy the entry
437 $entry = $this->listEntries[$hash];
444 * Gets a full array from given group name
446 * @param $groupName The group name to get a list for
447 * @return $entries The array with all entries
448 * @throws NoListGroupException If the specified group is invalid
450 public function getArrayFromProtocolInstance ($groupName) {
451 // Is the group valid?
452 if (!$this->isGroupSet($groupName)) {
453 // Throw the exception here
454 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
457 // Init the entries' array
461 $iteratorInstance = $this->listGroups[$groupName]->getIterator();
463 // Rewind the iterator for this round
464 $iteratorInstance->rewind();
466 // Go through all entries
467 while ($iteratorInstance->valid()) {
469 $entryIndex = $iteratorInstance->key();
471 // ... and the final entry which is the stored instance
472 $entry = $this->getEntry($entryIndex);
475 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('LIST: Adding entry ' . $entry . ' ...');
477 // Add it to the list
478 $entries[$iteratorInstance->current()] = $entry;
481 $iteratorInstance->next();
489 * Updates the given entry by hash with given array
491 * @param $hash Hash for this entry
492 * @param $entryArray Array with entry we should update
494 * @throws InvalidListHashException If the solved hash index is invalid
496 public function updateCurrentEntryByHash ($hash, array $entryArray) {
497 // Is the hash valid?
498 if (!$this->isHashValid($hash)) {
499 // Throw an exception here, hashIndex is unknown at this point
500 throw new InvalidListHashException(array($this, $hash, -999), self::EXCEPTION_INVALID_HASH);
504 $this->listEntries[$hash] = $entryArray;