81050c5f5dedab8a9e8d6e15fd3c5c4267ec7ca9
[core.git] / inc / main / classes / iterator / default / class_DefaultIterator.php
1 <?php
2 /**
3  * A Default iterator
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 class DefaultIterator extends BaseIterator implements Iterator {
25         /**
26          * Key for the global list index
27          */
28         private $indexKey = 0;
29
30         /**
31          * Protected constructor
32          *
33          * @return      void
34          */
35         protected function __construct () {
36                 // Call parent constructor
37                 parent::__construct(__CLASS__);
38         }
39
40         /**
41          * Creates an instance of this class
42          *
43          * @param       $listInstance           A list of a Listable
44          * @return      $iteratorInstance       An instance a Iterator class
45          */
46         public static final function createDefaultIterator (Listable $listInstance) {
47                 // Get new instance
48                 $iteratorInstance = new DefaultIterator();
49
50                 // Set the list
51                 $iteratorInstance->setListInstance($listInstance);
52
53                 // Return the prepared instance
54                 return $iteratorInstance;
55         }
56
57         /**
58          * Getter for current value from group or generic
59          *
60          * @return      $current        Current value in iteration
61          * @throws      IndexOutOfBoundsException       If $indexKey is out of bounds
62          */
63         public function current () {
64                 // Default is null
65                 $current = NULL;
66
67                 // Is the entry valid?
68                 if (!$this->valid()) {
69                         // Throw an exception here
70                         throw new IndexOutOfBoundsException($this->key(), self::EXCEPTION_INDEX_OUT_OF_BOUNDS);
71                 } // END - if
72
73                 // Now get the entry
74                 $current = $this->getListInstance()->getEntry($this->key());
75
76                 // Return it
77                 return $current;
78         }
79
80         /**
81          * Getter for key from group or generic
82          *
83          * @return      $indexKey       Current key in iteration
84          */
85         public function key () {
86                 return $this->indexKey;
87         }
88
89         /**
90          * Advances to the next entry
91          *
92          * @return      void
93          */
94         public function next () {
95                 $this->indexKey++;
96         }
97
98         /**
99          * Rewinds to the beginning of the iteration
100          *
101          * @return      void
102          */
103         public function rewind () {
104                 $this->indexKey = 0;
105         }
106
107         /**
108          * Checks whether the current entry is valid (not at the end of the list)
109          *
110          * @return      $isValid        Whether the current entry is there
111          */
112         public function valid () {
113                 // Check for total active peers and if we are not at the end
114                 $isValid = ($this->key() < $this->getListInstance()->count());
115
116                 // Return result
117                 return $isValid;
118         }
119 }
120
121 // [EOF]
122 ?>