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