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() . ',this->listGroups[' . $groupName . ']=' . $this->listGroups[$groupName]->__toString());
168 // Add the hash to the index
169 array_push($this->listIndex, $hash);
171 // Add the instance itself to the list
172 $this->listEntries[$hash] = $visitableInstance;
175 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',subGroup=' . $subGroup . ' - EXIT!');
179 * Gets an array from given list
181 * @param $list The requested list
182 * @return $array The requested array
183 * @throws NoListGroupException If the given group is not found
185 public final function getArrayFromList ($list) {
187 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',list[' . gettype($list) . ']=' . $list . ' - CALLED!');
189 // Is the group there?
190 if ((!is_null($list)) && (!$this->isGroupSet($list))) {
191 // Throw the exception here
192 throw new NoListGroupException(array($this, $list), self::EXCEPTION_GROUP_NOT_FOUND);
198 // Is there another list?
199 if (!is_null($list)) {
200 // Then get it as well
201 $array = $this->listGroups[$list]->getArrayFromList(NULL);
204 // Walk through all entries
205 foreach ($this->listIndex as $hash) {
207 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: hash=' . $hash);
209 // Is the list entry set?
210 if ($this->isHashValid($hash)) {
212 array_push($array, $this->listEntries[$hash]);
213 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: hash=' . $hash . ',array(' . count($array) . ')=' . print_r($array, true) . ' - ADDED!');
218 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',list[' . gettype($list) . ']=' . $list . ',array()=' . count($array) . ' - EXIT!');
225 * Adds the given entry to list group
227 * @param $groupName Group to add instance to
228 * @param $entry An entry of any type
230 * @throws NoListGroupException If the given group is not found
232 public function addEntry ($groupName, $entry) {
234 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!');
236 // Is the group already added?
237 if (!$this->isGroupSet($groupName)) {
238 // Throw the exception here
239 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
243 $hash = $this->generateHash($groupName, $groupName, $entry);
246 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',entry=' . print_r($entry, true) . ', hash=' . $hash);
248 // Add the hash to the index
249 array_push($this->listIndex, $hash);
252 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',listEntries()=' . count($this->listEntries));
254 // Now add the entry to the list
255 $this->listEntries[$hash] = $entry;
258 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',listEntries()=' . count($this->listEntries) . ' - EXIT!');
262 * Removes given entry from the list group
264 * @param $groupName Group where we should remove the entry from
265 * @param $entry The entry we should remove
267 * @throws NoListGroupException If the given group is not found
269 public function removeEntry ($groupName, $entry) {
271 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!');
273 // Is the group already added?
274 if (!$this->isGroupSet($groupName)) {
275 // Throw the exception here
276 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
280 $hash = $this->generateHash($groupName, $groupName, $entry);
283 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',entry=' . $entry . ', hash=' . $hash);
285 // Remove it from the list ...
286 unset($this->listEntries[$hash]);
288 // ... and hash list as well
289 unset($this->listIndex[array_search($hash, $this->listIndex)]);
292 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - EXIT!');
296 * Generates a hash from given group, sub group and entry
298 * @param $groupName Group to add instance to
299 * @param $subGroup Sub group to add instance to
300 * @param $entry An entry of any type
301 * @return $hash The generated
303 private function generateHash ($groupName, $subGroup, $entry) {
304 // Created entry, 'null' is default
307 // Determine type of entry
308 if (is_null($entry)) {
310 } elseif ($entry instanceof FrameworkInterface) {
311 // Own instance detected
312 $entry2 = $entry->hashCode();
313 } elseif ((is_int($entry)) || (is_float($entry)) || (is_resource($entry))) {
314 // Integer/float/resource detected
315 $entry2 = gettype($entry) . ':' . $entry;
316 } elseif (is_string($entry)) {
318 $entry2 = crc32($entry) . ':' . strlen($entry);
319 } elseif ((is_array($entry)) && (isset($entry['id']))) {
320 // Supported array found
321 $entry2 = crc32($entry['id']) . ':' . count($entry);
322 } elseif (($this->callbackInstance instanceof FrameworkInterface) && (method_exists($this->callbackInstance, 'generateListHashFromEntry'))) {
324 $entry2 = $this->callbackInstance->generateListHashFromEntry($entry);
326 // Unsupported type detected
327 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST[' . __METHOD__ . ':' . __LINE__ . ']: Entry type ' . gettype($entry) . ' is unsupported (this->callbackInstance=' . $this->callbackInstance . ').');
329 // At least take all keys from array
330 $entry2 = gettype($entry) . ':' . implode(':', array_keys($entry));
333 // Construct string which we shall hash
334 $hashString = $groupName . ':' . $subGroup . ':' . $entry2;
336 // Hash it with fastest hasher
337 $hash = crc32($hashString);
344 * Clears an array of groups, all are being checked for existence
346 * @param $groupNames An array with existing list groups
349 protected function clearGroups (array $groupNames) {
350 // Walk through all groups
351 foreach ($groupNames as $groupName) {
353 $this->clearGroup($groupName);
358 * Clears a single group by resetting it to its initial state (empty array)
360 * @param $groupName Name of an existing group to clear
363 protected function clearGroup ($groupName) {
364 // Does this group exist?
365 if (!$this->isGroupSet($groupName)) {
366 // Throw the exception here
367 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
370 // Then clear this group list
371 $this->listGroups[$groupName]->clearList();
374 $this->listIndex = array();
375 $this->listEntries = array();
379 * Counts all entries in this list
381 * @return $count All entries in this list
383 public final function count () {
384 return count($this->listIndex);
388 * Checks whether the given hash is valid
390 * @param $hash The hash we should validate
391 * @return $isValid Whether the given hash is valid
393 public final function isHashValid ($hash) {
395 $isValid = ((in_array($hash, $this->listIndex)) && (isset($this->listEntries[$hash])));
402 * Getter for hash from given hash index
404 * @param $hashIndex Index holding the hash
405 * @return $hash The hash
407 public final function getHash ($hashIndex) {
409 $hash = $this->listIndex[$hashIndex];
416 * Gets an entry from given hash index
418 * @param $hashIndex The hash index to resolve the mapped entry
419 * @return $entry Solved entry from list
420 * @throws InvalidListHashException If the solved hash index is invalid
422 public function getEntry ($hashIndex) {
423 // Get the hash value
424 $hash = $this->getHash($hashIndex);
426 // Is the hash valid?
427 if (!$this->isHashValid($hash)) {
428 // Throw an exception here
429 throw new InvalidListHashException(array($this, $hash, $hashIndex), self::EXCEPTION_INVALID_HASH);
432 // Now copy the entry
433 $entry = $this->listEntries[$hash];
440 * Gets a full array from given group name
442 * @param $groupName The group name to get a list for
443 * @return $entries The array with all entries
444 * @throws NoListGroupException If the specified group is invalid
446 public function getArrayFromProtocolInstance ($groupName) {
447 // Is the group valid?
448 if (!$this->isGroupSet($groupName)) {
449 // Throw the exception here
450 throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
453 // Init the entries' array
457 $iteratorInstance = $this->listGroups[$groupName]->getIterator();
459 // Rewind the iterator for this round
460 $iteratorInstance->rewind();
462 // Go through all entries
463 while ($iteratorInstance->valid()) {
465 $entryIndex = $iteratorInstance->key();
467 // ... and the final entry which is the stored instance
468 $entry = $this->getEntry($entryIndex);
471 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('LIST: Adding entry ' . $entry . ' ...');
473 // Add it to the list
474 $entries[$iteratorInstance->current()] = $entry;
477 $iteratorInstance->next();
485 * Updates the given entry by hash with given array
487 * @param $hash Hash for this entry
488 * @param $entryArray Array with entry we should update
490 * @throws InvalidListHashException If the solved hash index is invalid
492 public function updateCurrentEntryByHash ($hash, array $entryArray) {
493 // Is the hash valid?
494 if (!$this->isHashValid($hash)) {
495 // Throw an exception here, hashIndex is unknown at this point
496 throw new InvalidListHashException(array($this, $hash, -999), self::EXCEPTION_INVALID_HASH);
500 $this->listEntries[$hash] = $entryArray;