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