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