1 | <?php |
||
2 | /** |
||
3 | * |
||
4 | * This file is part of the Aura project for PHP. |
||
5 | * |
||
6 | * @package Aura.Input |
||
7 | * |
||
8 | * @license http://opensource.org/licenses/MIT-license.php MIT |
||
9 | * |
||
10 | */ |
||
11 | namespace Aura\Input; |
||
12 | |||
13 | use Iterator; |
||
14 | |||
15 | /** |
||
16 | * |
||
17 | * An object to allow iteration over fieldsets. |
||
18 | * |
||
19 | * @package Aura.Input |
||
20 | * |
||
21 | */ |
||
22 | class CollectionIterator implements Iterator |
||
23 | { |
||
24 | /** |
||
25 | * |
||
26 | * The fieldsets over which we are iterating. |
||
27 | * |
||
28 | * @var array |
||
29 | * |
||
30 | */ |
||
31 | protected $fieldsets; |
||
32 | |||
33 | /** |
||
34 | * |
||
35 | * The keys to iterate over in the fieldsets. |
||
36 | * |
||
37 | * @var array |
||
38 | * |
||
39 | */ |
||
40 | protected $keys; |
||
41 | |||
42 | /** |
||
43 | * |
||
44 | * Is the current iterator position valid? |
||
45 | * |
||
46 | * @var bool |
||
47 | * |
||
48 | */ |
||
49 | protected $valid; |
||
50 | |||
51 | /** |
||
52 | * |
||
53 | * Constructor. |
||
54 | * |
||
55 | * @param Collection $collection The fieldsets over which to iterate. |
||
56 | * |
||
57 | */ |
||
58 | public function __construct(Collection $collection) |
||
59 | { |
||
60 | $this->collection = $collection; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
61 | $this->keys = $this->collection->getKeys(); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * |
||
66 | * Returns the value at the current iterator position. |
||
67 | * |
||
68 | * @return mixed |
||
69 | * |
||
70 | */ |
||
71 | #[\ReturnTypeWillChange] |
||
72 | public function current() |
||
73 | { |
||
74 | return $this->collection->offsetGet($this->key()); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * |
||
79 | * Returns the current iterator position. |
||
80 | * |
||
81 | * @return mixed |
||
82 | * |
||
83 | */ |
||
84 | #[\ReturnTypeWillChange] |
||
85 | public function key() |
||
86 | { |
||
87 | return current($this->keys); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * |
||
92 | * Moves the iterator to the next position. |
||
93 | * |
||
94 | * @return void |
||
95 | * |
||
96 | */ |
||
97 | #[\ReturnTypeWillChange] |
||
98 | public function next() |
||
99 | { |
||
100 | $this->valid = (next($this->keys) !== false); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * |
||
105 | * Moves the iterator to the first position. |
||
106 | * |
||
107 | * @return void |
||
108 | * |
||
109 | */ |
||
110 | #[\ReturnTypeWillChange] |
||
111 | public function rewind() |
||
112 | { |
||
113 | $this->valid = (reset($this->keys) !== false); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * |
||
118 | * Is the current iterator position valid? |
||
119 | * |
||
120 | * @return boolean |
||
121 | * |
||
122 | */ |
||
123 | #[\ReturnTypeWillChange] |
||
124 | public function valid() |
||
125 | { |
||
126 | return $this->valid; |
||
127 | } |
||
128 | } |
||
129 |