1d0ec884d996b10e31781a9cc8ee1467bcb54f98
[core.git] / framework / main / classes / iterator / default / class_DefaultIterator.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Iterator;
4
5 // Import framework stuff
6 use CoreFramework\Iterator\BaseIterator;
7 use CoreFramework\Lists\Listable;
8
9 // Import SPL stuff
10 use \Iterator;
11
12 /**
13  * A Default iterator
14  *
15  * @author              Roland Haeder <webmaster@shipsimu.org>
16  * @version             0.0.0
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
20  *
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.
25  *
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.
30  *
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/>.
33  */
34 class DefaultIterator extends BaseIterator implements Iterator {
35         /**
36          * Key for the global list index
37          */
38         private $indexKey = 0;
39
40         /**
41          * Protected constructor
42          *
43          * @return      void
44          */
45         protected function __construct () {
46                 // Call parent constructor
47                 parent::__construct(__CLASS__);
48         }
49
50         /**
51          * Creates an instance of this class
52          *
53          * @param       $listInstance           A list of a Listable
54          * @return      $iteratorInstance       An instance a Iterator class
55          */
56         public static final function createDefaultIterator (Listable $listInstance) {
57                 // Get new instance
58                 $iteratorInstance = new DefaultIterator();
59
60                 // Set the list
61                 $iteratorInstance->setListInstance($listInstance);
62
63                 // Return the prepared instance
64                 return $iteratorInstance;
65         }
66
67         /**
68          * Getter for current value from group or generic
69          *
70          * @return      $current        Current value in iteration
71          * @throws      IndexOutOfBoundsException       If $indexKey is out of bounds
72          */
73         public function current () {
74                 // Default is null
75                 $current = NULL;
76
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);
81                 } // END - if
82
83                 // Now get the entry
84                 $current = $this->getListInstance()->getEntry($this->key());
85
86                 // Return it
87                 return $current;
88         }
89
90         /**
91          * Getter for key from group or generic
92          *
93          * @return      $indexKey       Current key in iteration
94          */
95         public function key () {
96                 return $this->indexKey;
97         }
98
99         /**
100          * Advances to the next entry
101          *
102          * @return      void
103          */
104         public function next () {
105                 $this->indexKey++;
106         }
107
108         /**
109          * Rewinds to the beginning of the iteration
110          *
111          * @return      void
112          */
113         public function rewind () {
114                 $this->indexKey = 0;
115         }
116
117         /**
118          * Checks whether the current entry is valid (not at the end of the list)
119          *
120          * @return      $isValid        Whether the current entry is there
121          */
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());
125
126                 // Return result
127                 return $isValid;
128         }
129
130 }