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