1 | <?php |
||
15 | class LookaheadIterator extends \IteratorIterator |
||
16 | { |
||
17 | /** |
||
18 | * Current key. |
||
19 | * |
||
20 | * @var mixed |
||
21 | */ |
||
22 | protected $key = 0; |
||
23 | |||
24 | /** |
||
25 | * Current value. |
||
26 | * |
||
27 | * @var mixed |
||
28 | */ |
||
29 | protected $current; |
||
30 | |||
31 | /** |
||
32 | * Whether the current element is valid or not. |
||
33 | * |
||
34 | * @var bool |
||
35 | */ |
||
36 | protected $valid = false; |
||
37 | |||
38 | /** |
||
39 | * LookaheadIterator constructor. |
||
40 | * @param iterable $iterator |
||
41 | */ |
||
42 | public function __construct(iterable $iterator) |
||
47 | |||
48 | /** |
||
49 | * @param iterable $iterable |
||
50 | * @return \Traversable |
||
51 | */ |
||
52 | private function toIterator(iterable $iterable): \Traversable |
||
56 | |||
57 | /** |
||
58 | * Return the current element. |
||
59 | * |
||
60 | * @return mixed |
||
61 | */ |
||
62 | public function current() |
||
66 | |||
67 | /** |
||
68 | * Return the key of the current element. |
||
69 | * |
||
70 | * @return mixed |
||
71 | */ |
||
72 | public function key() |
||
76 | |||
77 | /** |
||
78 | * Rewind the iterator to the first element. |
||
79 | * |
||
80 | * @return void |
||
81 | */ |
||
82 | public function rewind(): void |
||
88 | |||
89 | /** |
||
90 | * Move forward to next element. |
||
91 | * |
||
92 | * @return void |
||
93 | */ |
||
94 | public function next(): void |
||
108 | |||
109 | /** |
||
110 | * Check if current position is valid. |
||
111 | * |
||
112 | * @return bool |
||
113 | */ |
||
114 | public function valid(): bool |
||
118 | |||
119 | /** |
||
120 | * Check whether there is a next element. |
||
121 | * |
||
122 | * @return bool |
||
123 | */ |
||
124 | public function hasNext(): bool |
||
128 | |||
129 | /** |
||
130 | * Get next value. |
||
131 | * |
||
132 | * @return mixed |
||
133 | */ |
||
134 | public function getNext() |
||
138 | |||
139 | /** |
||
140 | * Get next key. |
||
141 | * |
||
142 | * @return mixed |
||
143 | */ |
||
144 | public function getNextKey() |
||
148 | } |
||
149 |