1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\Compiler\Iterator; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class LookaheadIterator |
14
|
|
|
*/ |
15
|
|
|
class LookaheadIterator implements \Iterator |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var \Traversable |
19
|
|
|
*/ |
20
|
|
|
protected $iterator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
protected $key = 0; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var mixed |
29
|
|
|
*/ |
30
|
|
|
protected $current; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
protected $valid; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* LookaheadIterator constructor. |
39
|
|
|
* @param iterable $iterator |
40
|
|
|
* @throws \InvalidArgumentException |
41
|
|
|
*/ |
42
|
|
|
public function __construct(iterable $iterator) |
43
|
|
|
{ |
44
|
|
|
$this->iterator = $this->toIterator($iterator); |
45
|
|
|
$this->current = $this->iterator->current(); |
46
|
|
|
$this->key = $this->iterator->key(); |
47
|
|
|
$this->valid = $this->iterator->valid(); |
48
|
|
|
|
49
|
|
|
$this->rewind(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param iterable $iterator |
54
|
|
|
* @return \Iterator |
55
|
|
|
* @throws \InvalidArgumentException |
56
|
|
|
*/ |
57
|
|
|
private function toIterator(iterable $iterator): \Iterator |
58
|
|
|
{ |
59
|
|
|
switch (true) { |
60
|
|
|
case $iterator instanceof \Iterator: |
61
|
|
|
return $iterator; |
62
|
|
|
case $iterator instanceof \Traversable: |
63
|
|
|
return new \IteratorIterator($iterator); |
64
|
|
|
case \is_array($iterator): |
65
|
|
|
return new \ArrayIterator($iterator); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
throw new \InvalidArgumentException('Unsupported iterator type'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
public function current() |
75
|
|
|
{ |
76
|
|
|
return $this->current; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return int|mixed |
81
|
|
|
*/ |
82
|
|
|
public function key() |
83
|
|
|
{ |
84
|
|
|
return $this->key; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Rewind the iterator to the first element. |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
public function rewind(): void |
92
|
|
|
{ |
93
|
|
|
$this->iterator->rewind(); |
|
|
|
|
94
|
|
|
$this->next(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Move forward to next element. |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
public function next(): void |
103
|
|
|
{ |
104
|
|
|
$this->valid = $this->iterator->valid(); |
|
|
|
|
105
|
|
|
|
106
|
|
|
if ($this->valid === false) { |
107
|
|
|
return; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->key = $this->iterator->key(); |
|
|
|
|
111
|
|
|
$this->current = $this->iterator->current(); |
|
|
|
|
112
|
|
|
|
113
|
|
|
$this->iterator->next(); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Check if current position is valid. |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function valid(): bool |
122
|
|
|
{ |
123
|
|
|
return $this->valid; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Check whether there is a next element. |
128
|
|
|
* |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function hasNext(): bool |
132
|
|
|
{ |
133
|
|
|
return $this->iterator->valid(); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get next value. |
138
|
|
|
* |
139
|
|
|
* @return mixed |
140
|
|
|
*/ |
141
|
|
|
public function getNext() |
142
|
|
|
{ |
143
|
|
|
return $this->iterator->current(); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get next key. |
148
|
|
|
* |
149
|
|
|
* @return mixed |
150
|
|
|
*/ |
151
|
|
|
public function getNextKey() |
152
|
|
|
{ |
153
|
|
|
return $this->iterator->key(); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: