|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gielfeldt\Iterators; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Split an iterator into multiple iterators. |
|
7
|
|
|
* |
|
8
|
|
|
* Usage: |
|
9
|
|
|
* foreach (new ChunkIterator($someIterator, 4) as $chunk) { |
|
10
|
|
|
* foreach ($chunk as $key => $value) { |
|
11
|
|
|
* ... do stuff |
|
12
|
|
|
* } |
|
13
|
|
|
* } |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
class ChunkIterator extends ValuesIterator |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Size of a chunk |
|
20
|
|
|
* |
|
21
|
|
|
* @var int |
|
22
|
|
|
*/ |
|
23
|
|
|
private $size; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The original iterator to split into chunks. |
|
27
|
|
|
* |
|
28
|
|
|
* @var \Iterator |
|
29
|
|
|
*/ |
|
30
|
|
|
private $innerIterator; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param \Traversable $iterator |
|
36
|
|
|
* The iterator to split into chunks. |
|
37
|
|
|
* @param int $size |
|
38
|
|
|
* The size of each chunk. |
|
39
|
|
|
*/ |
|
40
|
3 |
|
public function __construct(\Traversable $iterator, int $size) |
|
41
|
|
|
{ |
|
42
|
3 |
|
$this->innerIterator = $iterator instanceof \Iterator ? $iterator : new \IteratorIterator($iterator); |
|
43
|
3 |
|
$this->size = $size; |
|
44
|
|
|
|
|
45
|
|
|
// The outer iterator is a finite replaceable iterator with a condition |
|
46
|
|
|
// on the inner iterator not being empty. |
|
47
|
3 |
|
parent::__construct( |
|
48
|
3 |
|
new FiniteIterator( |
|
49
|
3 |
|
new InfiniteIterator(new ReplaceableIterator()), |
|
50
|
3 |
|
function ($iterator) { |
|
51
|
3 |
|
return !$iterator->current()->valid(); |
|
52
|
3 |
|
} |
|
53
|
|
|
) |
|
54
|
|
|
); |
|
55
|
3 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Rewind original inner iterator, and recreate inner and outer iterator. |
|
59
|
|
|
* |
|
60
|
|
|
* @see Iterator::rewind() |
|
61
|
|
|
*/ |
|
62
|
3 |
|
public function rewind() |
|
63
|
|
|
{ |
|
64
|
|
|
// Setup chunked inner iterator. |
|
65
|
3 |
|
$this->innerIterator->rewind(); |
|
66
|
3 |
|
$innerIterator = new \NoRewindIterator($this->innerIterator); |
|
67
|
3 |
|
$limitedInnerIterator = new \LimitIterator($innerIterator, 0, $this->size); |
|
68
|
3 |
|
$limitedInnerIterator->rewind(); |
|
69
|
|
|
|
|
70
|
|
|
// Setup outer iterator. |
|
71
|
3 |
|
$outerIterator = new \ArrayIterator([$limitedInnerIterator]); |
|
72
|
3 |
|
$this->setInnerIterator($outerIterator); |
|
|
|
|
|
|
73
|
3 |
|
parent::rewind(); |
|
74
|
3 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Make sure that the inner iterator is positioned correctly, before skipping |
|
78
|
|
|
* to next chunk. |
|
79
|
|
|
* |
|
80
|
|
|
* @see Iterator::next() |
|
81
|
|
|
*/ |
|
82
|
3 |
|
public function next() |
|
83
|
|
|
{ |
|
84
|
|
|
// Finish iteration on the current chunk, if necessary, so that our |
|
85
|
|
|
// inner iterator is positioned correctly. |
|
86
|
3 |
|
while ($this->current()->valid()) { |
|
87
|
1 |
|
$this->current()->next(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// Rewind norewind iterator to repeat iterator of chunk size. |
|
91
|
3 |
|
$this->current()->rewind(); |
|
92
|
3 |
|
return parent::next(); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.