3 namespace CoreFramework\Iterator;
5 // Import framework stuff
6 use CoreFramework\Iterator\BaseIterator;
7 use CoreFramework\Lists\Listable;
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 class DefaultIterator extends BaseIterator implements Iterator {
36 * Key for the global list index
38 private $indexKey = 0;
41 * Protected constructor
45 protected function __construct () {
46 // Call parent constructor
47 parent::__construct(__CLASS__);
51 * Creates an instance of this class
53 * @param $listInstance A list of a Listable
54 * @return $iteratorInstance An instance a Iterator class
56 public static final function createDefaultIterator (Listable $listInstance) {
58 $iteratorInstance = new DefaultIterator();
61 $iteratorInstance->setListInstance($listInstance);
63 // Return the prepared instance
64 return $iteratorInstance;
68 * Getter for current value from group or generic
70 * @return $current Current value in iteration
71 * @throws IndexOutOfBoundsException If $indexKey is out of bounds
73 public function current () {
77 // Is the entry valid?
78 if (!$this->valid()) {
79 // Throw an exception here
80 throw new IndexOutOfBoundsException($this->key(), self::EXCEPTION_INDEX_OUT_OF_BOUNDS);
84 $current = $this->getListInstance()->getEntry($this->key());
91 * Getter for key from group or generic
93 * @return $indexKey Current key in iteration
95 public function key () {
96 return $this->indexKey;
100 * Advances to the next entry
104 public function next () {
109 * Rewinds to the beginning of the iteration
113 public function rewind () {
118 * Checks whether the current entry is valid (not at the end of the list)
120 * @return $isValid Whether the current entry is there
122 public function valid () {
123 // Check for total active peers and if we are not at the end
124 $isValid = ($this->key() < $this->getListInstance()->count());