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