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