f48cc66b645a937e32be86d55f0604b49edba155
[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\Lists\Listable;
7
8 /**
9  * A Default iterator
10  *
11  * @author              Roland Haeder <webmaster@shipsimu.org>
12  * @version             0.0.0
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
16  *
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.
21  *
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.
26  *
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/>.
29  */
30 class DefaultIterator extends BaseIterator implements Iterator {
31         /**
32          * Key for the global list index
33          */
34         private $indexKey = 0;
35
36         /**
37          * Protected constructor
38          *
39          * @return      void
40          */
41         protected function __construct () {
42                 // Call parent constructor
43                 parent::__construct(__CLASS__);
44         }
45
46         /**
47          * Creates an instance of this class
48          *
49          * @param       $listInstance           A list of a Listable
50          * @return      $iteratorInstance       An instance a Iterator class
51          */
52         public static final function createDefaultIterator (Listable $listInstance) {
53                 // Get new instance
54                 $iteratorInstance = new DefaultIterator();
55
56                 // Set the list
57                 $iteratorInstance->setListInstance($listInstance);
58
59                 // Return the prepared instance
60                 return $iteratorInstance;
61         }
62
63         /**
64          * Getter for current value from group or generic
65          *
66          * @return      $current        Current value in iteration
67          * @throws      IndexOutOfBoundsException       If $indexKey is out of bounds
68          */
69         public function current () {
70                 // Default is null
71                 $current = NULL;
72
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);
77                 } // END - if
78
79                 // Now get the entry
80                 $current = $this->getListInstance()->getEntry($this->key());
81
82                 // Return it
83                 return $current;
84         }
85
86         /**
87          * Getter for key from group or generic
88          *
89          * @return      $indexKey       Current key in iteration
90          */
91         public function key () {
92                 return $this->indexKey;
93         }
94
95         /**
96          * Advances to the next entry
97          *
98          * @return      void
99          */
100         public function next () {
101                 $this->indexKey++;
102         }
103
104         /**
105          * Rewinds to the beginning of the iteration
106          *
107          * @return      void
108          */
109         public function rewind () {
110                 $this->indexKey = 0;
111         }
112
113         /**
114          * Checks whether the current entry is valid (not at the end of the list)
115          *
116          * @return      $isValid        Whether the current entry is there
117          */
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());
121
122                 // Return result
123                 return $isValid;
124         }
125
126 }