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() . ',groupName=' . $groupName . ',subGroup=' . $subGroup . ',hash=' . $hash . ' - Calling addEntry() ...');
153 // Now add it to the group list and hash it
154 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',this->listGroups[' . $groupName . ']=' . $this->listGroups[$groupName]->__toString());
155 //$this->listGroups[$groupName]->addEntry($subGroup, $hash);
157 // Add the hash to the index
158 array_push($this->listIndex, $hash);
160 // Add the instance itself to the list
161 $this->listEntries[$hash] = $visitableInstance;
164 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',subGroup=' . $subGroup . ' - EXIT!');
168 * Gets an array from given list
170 * @param $list The requested list
171 * @return $array The requested array
172 * @throws NoListGroupException If the given group is not found
174 public final function getArrayFromList ($list) {
176 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',list[' . gettype($list) . ']=' . $list . ' - CALLED!');
178 // Is the group there?
179 if ((!is_null($list)) && (!$this->isGroupSet($list))) {
180 // Throw the exception here
181 throw new NoListGroupException(array($this, $list), self::EXCEPTION_GROUP_NOT_FOUND);
187 // Is there another list?
188 if (!is_null($list)) {
189 // Then get it as well
190 $array = $this->listGroups[$list]->getArrayFromList(NULL);
193 // Walk through all entries
194 foreach ($this->listIndex as $hash) {
196 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: hash=' . $hash);
198 // Is the list entry set?
199 if ($this->isHashValid($hash)) {
201 array_push($array, $this->listEntries[$hash]);
202 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: hash=' . $hash . ',array(' . count($array) . ')=' . print_r($array, true) . ' - ADDED!');
207 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',list[' . gettype($list) . ']=' . $list . ',array()=' . count($array) . ' - EXIT!');
214 * Adds the given entry to list group
216 * @param $groupName Group to add instance to
217 * @param $entry An entry of any type
219 * @throws NoListGroupException If the given group is not found
221 public function addEntry ($groupName, $entry) {
223 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!');
225 // Is the group already added?
226 if (!$this->isGroupSet($groupName)) {
227 // Throw the exception here
228 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
232 $hash = $this->generateHash($groupName, $groupName, $entry);
235 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',entry=' . print_r($entry, true) . ', hash=' . $hash);
237 // Add the hash to the index
238 array_push($this->listIndex, $hash);
241 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',listEntries()=' . count($this->listEntries));
243 // Now add the entry to the list
244 $this->listEntries[$hash] = $entry;
247 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',listEntries()=' . count($this->listEntries) . ' - EXIT!');
251 * Removes given entry from the list group
253 * @param $groupName Group where we should remove the entry from
254 * @param $entry The entry we should remove
256 * @throws NoListGroupException If the given group is not found
258 public function removeEntry ($groupName, $entry) {
260 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!');
262 // Is the group already added?
263 if (!$this->isGroupSet($groupName)) {
264 // Throw the exception here
265 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
269 $hash = $this->generateHash($groupName, $groupName, $entry);
272 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',entry=' . $entry . ', hash=' . $hash);
274 // Remove it from the list ...
275 unset($this->listEntries[$hash]);
277 // ... and hash list as well
278 unset($this->listIndex[array_search($hash, $this->listIndex)]);
281 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - EXIT!');
285 * Generates a hash from given group, sub group and entry
287 * @param $groupName Group to add instance to
288 * @param $subGroup Sub group to add instance to
289 * @param $entry An entry of any type
290 * @return $hash The generated
292 private function generateHash ($groupName, $subGroup, $entry) {
293 // Created entry, 'null' is default
296 // Determine type of entry
297 if (is_null($entry)) {
299 } elseif ($entry instanceof FrameworkInterface) {
300 // Own instance detected
301 $entry2 = $entry->hashCode();
302 } elseif ((is_int($entry)) || (is_float($entry)) || (is_resource($entry))) {
303 // Integer/float/resource detected
304 $entry2 = gettype($entry) . ':' . $entry;
305 } elseif (is_string($entry)) {
307 $entry2 = crc32($entry) . ':' . strlen($entry);
308 } elseif ((is_array($entry)) && (isset($entry['id']))) {
309 // Supported array found
310 $entry2 = crc32($entry['id']) . ':' . count($entry);
312 // Unsupported type detected
313 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST[' . __METHOD__ . ':' . __LINE__ . ']: Entry type ' . gettype($entry) . ' is unsupported.');
315 // @TODO Extend this somehow?
316 $entry2 = gettype($entry);
319 // Construct string which we shall hash
320 $hashString = $groupName . ':' . $subGroup . ':' . $entry2;
322 // Hash it with fastest hasher
323 $hash = crc32($hashString);
330 * Clears an array of groups, all are being checked for existence
332 * @param $groupNames An array with existing list groups
335 protected function clearGroups (array $groupNames) {
336 // Walk through all groups
337 foreach ($groupNames as $groupName) {
339 $this->clearGroup($groupName);
344 * Clears a single group by resetting it to its initial state (empty array)
346 * @param $groupName Name of an existing group to clear
349 protected function clearGroup ($groupName) {
350 // Does this group exist?
351 if (!$this->isGroupSet($groupName)) {
352 // Throw the exception here
353 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
356 // Then clear this group list
357 $this->listGroups[$groupName]->clearList();
360 $this->listIndex = array();
361 $this->listEntries = array();
365 * Counts all entries in this list
367 * @return $count All entries in this list
369 public final function count () {
370 return count($this->listIndex);
374 * Checks whether the given hash is valid
376 * @param $hash The hash we should validate
377 * @return $isValid Whether the given hash is valid
379 public final function isHashValid ($hash) {
381 $isValid = ((in_array($hash, $this->listIndex)) && (isset($this->listEntries[$hash])));
388 * Getter for hash from given hash index
390 * @param $hashIndex Index holding the hash
391 * @return $hash The hash
393 public final function getHash ($hashIndex) {
395 $hash = $this->listIndex[$hashIndex];
402 * Gets an entry from given hash index
404 * @param $hashIndex The hash index to resolve the mapped entry
405 * @return $entry Solved entry from list
406 * @throws InvalidListHashException If the solved hash index is invalid
408 public function getEntry ($hashIndex) {
409 // Get the hash value
410 $hash = $this->getHash($hashIndex);
412 // Is the hash valid?
413 if (!$this->isHashValid($hash)) {
414 // Throw an exception here
415 throw new InvalidListHashException(array($this, $hash, $hashIndex), self::EXCEPTION_INVALID_HASH);
418 // Now copy the entry
419 $entry = $this->listEntries[$hash];
426 * Gets a full array from given group name
428 * @param $groupName The group name to get a list for
429 * @return $entries The array with all entries
430 * @throws NoListGroupException If the specified group is invalid
432 public function getArrayFromProtocolInstance ($groupName) {
433 // Is the group valid?
434 if (!$this->isGroupSet($groupName)) {
435 // Throw the exception here
436 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
439 // Init the entries' array
443 $iteratorInstance = $this->listGroups[$groupName]->getIterator();
445 // Rewind the iterator for this round
446 $iteratorInstance->rewind();
448 // Go through all entries
449 while ($iteratorInstance->valid()) {
451 $entryIndex = $iteratorInstance->key();
453 // ... and the final entry which is the stored instance
454 $entry = $this->getEntry($entryIndex);
457 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('LIST: Adding entry ' . $entry . ' ...');
459 // Add it to the list
460 $entries[$iteratorInstance->current()] = $entry;
463 $iteratorInstance->next();
471 * Updates the given entry by hash with given array
473 * @param $hash Hash for this entry
474 * @param $entryArray Array with entry we should update
476 * @throws InvalidListHashException If the solved hash index is invalid
478 public function updateCurrentEntryByHash ($hash, array $entryArray) {
479 // Is the hash valid?
480 if (!$this->isHashValid($hash)) {
481 // Throw an exception here, hashIndex is unknown at this point
482 throw new InvalidListHashException(array($this, $hash, -999), self::EXCEPTION_INVALID_HASH);
486 $this->listEntries[$hash] = $entryArray;