1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* For the full copyright and license information, please view |
5
|
|
|
* the LICENSE file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace loophp\iterators; |
11
|
|
|
|
12
|
|
|
use ArrayIterator; |
13
|
|
|
use Generator; |
14
|
|
|
use Iterator; |
15
|
|
|
use loophp\iterators\Contract\PausableIteratorAggregateInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @template TKey |
19
|
|
|
* @template T |
20
|
|
|
* |
21
|
|
|
* @implements PausableIteratorAggregateInterface<TKey, T> |
22
|
|
|
*/ |
23
|
|
|
final class PausableIteratorAggregate implements PausableIteratorAggregateInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var Iterator<int, array{0:TKey, 1:T}> |
27
|
|
|
*/ |
28
|
|
|
private Iterator $iterator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var PackIterableAggregate<TKey, T> |
32
|
|
|
*/ |
33
|
|
|
private PackIterableAggregate $iteratorAggregate; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Iterator<TKey, T> $iterator |
37
|
|
|
*/ |
38
|
2 |
|
public function __construct(Iterator $iterator) |
39
|
|
|
{ |
40
|
2 |
|
$this->iteratorAggregate = new PackIterableAggregate(new CachingIteratorAggregate($iterator)); |
41
|
2 |
|
$this->iterator = new ArrayIterator(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return Generator<TKey, T> |
46
|
|
|
*/ |
47
|
2 |
|
public function getIterator(): Generator |
48
|
|
|
{ |
49
|
2 |
|
$this->iterator = $this->iteratorAggregate->getIterator(); |
50
|
|
|
|
51
|
2 |
|
foreach ($this->iterator as [$key, $value]) { |
52
|
2 |
|
yield $key => $value; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return Generator<TKey, T> |
58
|
|
|
*/ |
59
|
1 |
|
public function rest(): Generator |
60
|
|
|
{ |
61
|
1 |
|
for ($this->iterator->next(); $this->iterator->valid(); $this->iterator->next()) { |
62
|
1 |
|
[$key, $current] = $this->iterator->current(); |
63
|
|
|
|
64
|
1 |
|
yield $key => $current; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|