1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gielfeldt\Iterators; |
4
|
|
|
|
5
|
|
|
class CachingIterator extends \ArrayIterator |
6
|
|
|
{ |
7
|
|
|
const CLONE_KEY = 1; |
8
|
|
|
const CLONE_CURRENT = 2; |
9
|
|
|
|
10
|
|
|
private $uncachedIterator; |
11
|
|
|
private $uncachedIteratorCount; |
12
|
|
|
private $modified = false; |
13
|
|
|
private $finished = false; |
14
|
|
|
private $flags; |
15
|
|
|
private $index; |
16
|
|
|
|
17
|
|
|
public function __construct(\Traversable $iterator, int $flags = self::CLONE_KEY | self::CLONE_CURRENT) |
18
|
|
|
{ |
19
|
|
|
$this->flags = $flags; |
20
|
|
|
$this->uncachedIteratorCount = $iterator instanceof \Countable ? count($iterator) : null; |
21
|
|
|
$this->uncachedIterator = new TraversableIterator($iterator); |
22
|
|
|
parent::__construct(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
private function setupInnerIterator() |
26
|
|
|
{ |
27
|
|
|
if ($this->uncachedIterator->getIndex() === null) { |
28
|
|
|
$this->uncachedIterator->rewind(); |
29
|
|
|
parent::rewind(); |
|
|
|
|
30
|
|
|
$this->collect(); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function count() |
35
|
|
|
{ |
36
|
|
|
if (!$this->finished && !$this->modified && $this->uncachedIteratorCount !== null) { |
37
|
|
|
return $this->uncachedIteratorCount; |
38
|
|
|
} |
39
|
|
|
$this->collectRest(); |
40
|
|
|
return parent::count(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getIndex() |
44
|
|
|
{ |
45
|
|
|
return $this->index; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function rewind() |
49
|
|
|
{ |
50
|
|
|
$this->index = 0; |
51
|
|
|
$this->setupInnerIterator(); |
52
|
|
|
parent::rewind(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function next() |
56
|
|
|
{ |
57
|
|
|
$this->index++; |
58
|
|
|
parent::next(); |
59
|
|
|
if (!parent::valid()) { |
|
|
|
|
60
|
|
|
$this->collect(); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function offsetSet($offset, $value) |
65
|
|
|
{ |
66
|
|
|
$this->collectRest(); |
67
|
|
|
$this->modified = true; |
68
|
|
|
return parent::offsetSet($offset, $value); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function collectRest($until = null) |
72
|
|
|
{ |
73
|
|
|
if ($this->finished) { |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->setupInnerIterator(); |
78
|
|
|
|
79
|
|
|
while (!$this->finished && ($until == null || $until >= $this->uncachedIterator->getIndex())) { |
80
|
|
|
$this->collect(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function collect() |
85
|
|
|
{ |
86
|
|
|
if ($this->uncachedIterator->valid()) { |
87
|
|
|
$key = $this->uncachedIterator->key(); |
|
|
|
|
88
|
|
|
$key = ($this->flags & self::CLONE_KEY) && is_object($key) ? clone $key : $key; |
89
|
|
|
$current = $this->uncachedIterator->current(); |
90
|
|
|
$current = ($this->flags & self::CLONE_CURRENT) && is_object($current) ? clone $current : $current; |
91
|
|
|
parent::offsetSet($key, $current); |
|
|
|
|
92
|
|
|
$this->uncachedIterator->next(); |
93
|
|
|
} else { |
94
|
|
|
$this->finished = true; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function prev() |
99
|
|
|
{ |
100
|
|
|
$this->seek($this->index - 1); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// Ensure entire inner iterator is collected before applying the follwing. |
104
|
|
|
public function seek($pos) |
105
|
|
|
{ |
106
|
|
|
if ($pos >= $this->uncachedIterator->getIndex()) { |
107
|
|
|
$this->collectRest($pos); |
108
|
|
|
} |
109
|
|
|
$this->index = $pos; |
110
|
|
|
return parent::seek($pos); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function offsetGet($offset) |
114
|
|
|
{ |
115
|
|
|
$this->collectRest(); |
116
|
|
|
return parent::offsetGet($offset); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function offsetExists($offset) |
120
|
|
|
{ |
121
|
|
|
$this->collectRest(); |
122
|
|
|
return parent::offsetExists($offset); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function offsetUnset($offset) |
126
|
|
|
{ |
127
|
|
|
$this->collectRest(); |
128
|
|
|
$this->modified = true; |
129
|
|
|
return parent::offsetUnset($offset); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function getArrayCopy() |
133
|
|
|
{ |
134
|
|
|
$this->collectRest(); |
135
|
|
|
return iterator_to_array($this); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function ksort() |
139
|
|
|
{ |
140
|
|
|
$this->collectRest(); |
141
|
|
|
return parent::ksort(); |
142
|
|
|
} |
143
|
|
|
public function natcasesort() |
144
|
|
|
{ |
145
|
|
|
$this->collectRest(); |
146
|
|
|
return parent::natcasesort(); |
147
|
|
|
} |
148
|
|
|
public function natsort() |
149
|
|
|
{ |
150
|
|
|
$this->collectRest(); |
151
|
|
|
return parent::natsort(); |
152
|
|
|
} |
153
|
|
|
public function uasort($cmp) |
154
|
|
|
{ |
155
|
|
|
$this->collectRest(); |
156
|
|
|
return parent::uasort($cmp); |
157
|
|
|
} |
158
|
|
|
public function uksort($cmp) |
159
|
|
|
{ |
160
|
|
|
$this->collectRest(); |
161
|
|
|
return parent::uksort($cmp); |
162
|
|
|
} |
163
|
|
|
public function serialize() |
164
|
|
|
{ |
165
|
|
|
$this->collectRest(); |
166
|
|
|
$serialized = "x:i:0;"; |
167
|
|
|
$serialized .= serialize($this->getArrayCopy()) . ";"; |
168
|
|
|
$serialized .= "m:a:0:{}"; |
169
|
|
|
return $serialized; |
170
|
|
|
} |
171
|
|
|
public function unserialize($serialized) |
172
|
|
|
{ |
173
|
|
|
$this->finished = true; |
174
|
|
|
$this->modified = false; |
175
|
|
|
$this->uncachedIterator = new TraversableIterator(new \ArrayIterator()); |
176
|
|
|
$this->uncachedIteratorCount = null; |
177
|
|
|
return parent::unserialize($serialized); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.