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 $listGroups = array();
51 private $listEntries = array();
56 private $listIndex = array();
59 * Protected constructor
61 * @param $className Name of the class
64 protected function __construct ($className) {
65 // Call parent constructor
66 parent::__construct($className);
70 * Getter for iterator instance from this list
72 * @return $iteratorInstance An instance of a Iterator class
74 public function getIterator () {
75 // Get iterator from here
76 $iteratorInstance = $this->getIteratorInstance();
78 // Is the instance set?
79 if (is_null($iteratorInstance)) {
80 // Prepare a default iterator
81 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('default_iterator_class', array($this));
84 $this->setIteratorInstance($iteratorInstance);
88 return $iteratorInstance;
92 * Checks whether the given group is set
94 * @param $groupName Group to check if found in list
95 * @return $isset Whether the group is valid
97 public function isGroupSet ($groupName) {
98 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName);
99 return isset($this->listGroups[$groupName]);
103 * Adds the given group or if already added issues a ListGroupAlreadyAddedException
105 * @param $groupName Group to add
107 * @throws ListGroupAlreadyAddedException If the given group is already added
109 public function addGroup ($groupName) {
110 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!');
111 // Is the group already added?
112 if ($this->isGroupSet($groupName)) {
113 // Throw the exception here
114 throw new ListGroupAlreadyAddedException(array($this, $groupName), self::EXCEPTION_GROUP_ALREADY_ADDED);
117 // Add the group which is a simple array
118 $this->listGroups[$groupName] = ObjectFactory::createObjectByConfiguredName('list_group_class');
119 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - EXIT!');
123 * Adds the given instance to list group and sub group
125 * @param $groupName Group to add instance to
126 * @param $subGroup Sub group to add instance to
127 * @param $visitableInstance An instance of Visitable
129 * @throws NoListGroupException If the given group is not found
131 public function addInstance ($groupName, $subGroup, Visitable $visitableInstance) {
133 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',subGroup=' . $subGroup . ',visitableInstance=' . $visitableInstance->__toString() . ' - CALLED!');
135 // Is the group there?
136 if (!$this->isGroupSet($groupName)) {
137 // Throw the exception here
138 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
141 // Is the sub group there?
142 if (!$this->listGroups[$groupName]->isGroupSet($subGroup)) {
143 // Automatically add it
144 $this->listGroups[$groupName]->addGroup($subGroup);
148 $hash = $this->generateHash($groupName, $subGroup, $visitableInstance);
151 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',this->listGroups[' . $groupName . ']=' . $this->listGroups[$groupName]->__toString());
153 // Add the hash to the index
154 array_push($this->listIndex, $hash);
156 // Add the instance itself to the list
157 $this->listEntries[$hash] = $visitableInstance;
160 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',subGroup=' . $subGroup . ' - EXIT!');
164 * Gets an array from given list
166 * @param $list The requested list
167 * @return $array The requested array
168 * @throws NoListGroupException If the given group is not found
170 public final function getArrayFromList ($list) {
172 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',list[' . gettype($list) . ']=' . $list . ' - CALLED!');
174 // Is the group there?
175 if ((!is_null($list)) && (!$this->isGroupSet($list))) {
176 // Throw the exception here
177 throw new NoListGroupException(array($this, $list), self::EXCEPTION_GROUP_NOT_FOUND);
183 // Is there another list?
184 if (!is_null($list)) {
185 // Then get it as well
186 $array = $this->listGroups[$list]->getArrayFromList(NULL);
189 // Walk through all entries
190 foreach ($this->listIndex as $hash) {
192 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: hash=' . $hash);
194 // Is the list entry set?
195 if ($this->isHashValid($hash)) {
197 array_push($array, $this->listEntries[$hash]);
198 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: hash=' . $hash . ',array(' . count($array) . ')=' . print_r($array, true) . ' - ADDED!');
203 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',list[' . gettype($list) . ']=' . $list . ',array()=' . count($array) . ' - EXIT!');
210 * Adds the given entry to list group
212 * @param $groupName Group to add instance to
213 * @param $entry An entry of any type
215 * @throws NoListGroupException If the given group is not found
217 public function addEntry ($groupName, $entry) {
219 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!');
221 // Is the group already added?
222 if (!$this->isGroupSet($groupName)) {
223 // Throw the exception here
224 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
228 $hash = $this->generateHash($groupName, $groupName, $entry);
231 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',entry=' . print_r($entry, true) . ', hash=' . $hash);
233 // Add the hash to the index
234 array_push($this->listIndex, $hash);
237 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',listEntries()=' . count($this->listEntries));
239 // Now add the entry to the list
240 $this->listEntries[$hash] = $entry;
243 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',listEntries()=' . count($this->listEntries) . ' - EXIT!');
247 * Removes given entry from the list group
249 * @param $groupName Group where we should remove the entry from
250 * @param $entry The entry we should remove
252 * @throws NoListGroupException If the given group is not found
254 public function removeEntry ($groupName, $entry) {
256 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!');
258 // Is the group already added?
259 if (!$this->isGroupSet($groupName)) {
260 // Throw the exception here
261 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
265 $hash = $this->generateHash($groupName, $groupName, $entry);
268 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',entry=' . $entry . ', hash=' . $hash);
270 // Remove it from the list ...
271 unset($this->listEntries[$hash]);
273 // ... and hash list as well
274 unset($this->listIndex[array_search($hash, $this->listIndex)]);
277 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - EXIT!');
281 * Generates a hash from given group, sub group and entry
283 * @param $groupName Group to add instance to
284 * @param $subGroup Sub group to add instance to
285 * @param $entry An entry of any type
286 * @return $hash The generated
288 private function generateHash ($groupName, $subGroup, $entry) {
289 // Created entry, 'null' is default
292 // Determine type of entry
293 if (is_null($entry)) {
295 } elseif ($entry instanceof FrameworkInterface) {
296 // Own instance detected
297 $entry2 = $entry->hashCode();
298 } elseif ((is_int($entry)) || (is_float($entry)) || (is_resource($entry))) {
299 // Integer/float/resource detected
300 $entry2 = gettype($entry) . ':' . $entry;
301 } elseif (is_string($entry)) {
303 $entry2 = crc32($entry) . ':' . strlen($entry);
304 } elseif ((is_array($entry)) && (isset($entry['id']))) {
305 // Supported array found
306 $entry2 = crc32($entry['id']) . ':' . count($entry);
307 } elseif (($this->getCallbackInstance() instanceof FrameworkInterface) && (method_exists($this->getCallbackInstance(), 'generateListHashFromEntry'))) {
309 $entry2 = $this->getCallbackInstance()->generateListHashFromEntry($entry);
311 // Unsupported type detected
312 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST[' . __METHOD__ . ':' . __LINE__ . ']: Entry type ' . gettype($entry) . ' is unsupported (this->callbackInstance=' . $this->getCallbackInstance() . ').');
314 // At least take all keys from array
315 $entry2 = gettype($entry) . ':' . implode(':', array_keys($entry));
318 // Construct string which we shall hash
319 $hashString = $groupName . ':' . $subGroup . ':' . $entry2;
321 // Hash it with fastest hasher
322 $hash = crc32($hashString);
329 * Clears an array of groups, all are being checked for existence
331 * @param $groupNames An array with existing list groups
334 protected function clearGroups (array $groupNames) {
335 // Walk through all groups
336 foreach ($groupNames as $groupName) {
338 $this->clearGroup($groupName);
343 * Clears a single group by resetting it to its initial state (empty array)
345 * @param $groupName Name of an existing group to clear
348 protected function clearGroup ($groupName) {
349 // Does this group exist?
350 if (!$this->isGroupSet($groupName)) {
351 // Throw the exception here
352 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
355 // Then clear this group list
356 $this->listGroups[$groupName]->clearList();
359 $this->listIndex = array();
360 $this->listEntries = array();
364 * Counts all entries in this list
366 * @return $count All entries in this list
368 public final function count () {
369 return count($this->listIndex);
373 * Checks whether the given hash is valid
375 * @param $hash The hash we should validate
376 * @return $isValid Whether the given hash is valid
378 public final function isHashValid ($hash) {
380 $isValid = ((in_array($hash, $this->listIndex)) && (isset($this->listEntries[$hash])));
387 * Getter for hash from given hash index
389 * @param $hashIndex Index holding the hash
390 * @return $hash The hash
392 public final function getHash ($hashIndex) {
394 $hash = $this->listIndex[$hashIndex];
401 * Gets an entry from given hash index
403 * @param $hashIndex The hash index to resolve the mapped entry
404 * @return $entry Solved entry from list
405 * @throws InvalidListHashException If the solved hash index is invalid
407 public function getEntry ($hashIndex) {
408 // Get the hash value
409 $hash = $this->getHash($hashIndex);
411 // Is the hash valid?
412 if (!$this->isHashValid($hash)) {
413 // Throw an exception here
414 throw new InvalidListHashException(array($this, $hash, $hashIndex), self::EXCEPTION_INVALID_HASH);
417 // Now copy the entry
418 $entry = $this->listEntries[$hash];
425 * Gets a full array from given group name
427 * @param $groupName The group name to get a list for
428 * @return $entries The array with all entries
429 * @throws NoListGroupException If the specified group is invalid
431 public function getArrayFromProtocolInstance ($groupName) {
432 // Is the group valid?
433 if (!$this->isGroupSet($groupName)) {
434 // Throw the exception here
435 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
438 // Init the entries' array
442 $iteratorInstance = $this->listGroups[$groupName]->getIterator();
444 // Rewind the iterator for this round
445 $iteratorInstance->rewind();
447 // Go through all entries
448 while ($iteratorInstance->valid()) {
450 $entryIndex = $iteratorInstance->key();
452 // ... and the final entry which is the stored instance
453 $entry = $this->getEntry($entryIndex);
456 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('LIST: Adding entry ' . $entry . ' ...');
458 // Add it to the list
459 $entries[$iteratorInstance->current()] = $entry;
462 $iteratorInstance->next();
470 * Updates the given entry by hash with given array
472 * @param $hash Hash for this entry
473 * @param $entryArray Array with entry we should update
475 * @throws InvalidListHashException If the solved hash index is invalid
477 public function updateCurrentEntryByHash ($hash, array $entryArray) {
478 // Is the hash valid?
479 if (!$this->isHashValid($hash)) {
480 // Throw an exception here, hashIndex is unknown at this point
481 throw new InvalidListHashException(array($this, $hash, -999), self::EXCEPTION_INVALID_HASH);
485 $this->listEntries[$hash] = $entryArray;