3 namespace CoreFramework\Iterator;
8 * @author Roland Haeder <webmaster@shipsimu.org>
10 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
11 * @license GNU GPL 3.0 or any newer version
12 * @link http://www.shipsimu.org
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 class DefaultIterator extends BaseIterator implements Iterator {
29 * Key for the global list index
31 private $indexKey = 0;
34 * Protected constructor
38 protected function __construct () {
39 // Call parent constructor
40 parent::__construct(__CLASS__);
44 * Creates an instance of this class
46 * @param $listInstance A list of a Listable
47 * @return $iteratorInstance An instance a Iterator class
49 public static final function createDefaultIterator (Listable $listInstance) {
51 $iteratorInstance = new DefaultIterator();
54 $iteratorInstance->setListInstance($listInstance);
56 // Return the prepared instance
57 return $iteratorInstance;
61 * Getter for current value from group or generic
63 * @return $current Current value in iteration
64 * @throws IndexOutOfBoundsException If $indexKey is out of bounds
66 public function current () {
70 // Is the entry valid?
71 if (!$this->valid()) {
72 // Throw an exception here
73 throw new IndexOutOfBoundsException($this->key(), self::EXCEPTION_INDEX_OUT_OF_BOUNDS);
77 $current = $this->getListInstance()->getEntry($this->key());
84 * Getter for key from group or generic
86 * @return $indexKey Current key in iteration
88 public function key () {
89 return $this->indexKey;
93 * Advances to the next entry
97 public function next () {
102 * Rewinds to the beginning of the iteration
106 public function rewind () {
111 * Checks whether the current entry is valid (not at the end of the list)
113 * @return $isValid Whether the current entry is there
115 public function valid () {
116 // Check for total active peers and if we are not at the end
117 $isValid = ($this->key() < $this->getListInstance()->count());