-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCachingIteratorAggregate.php
More file actions
46 lines (39 loc) · 953 Bytes
/
CachingIteratorAggregate.php
File metadata and controls
46 lines (39 loc) · 953 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
declare(strict_types=1);
namespace loophp\iterators;
use Generator;
use Iterator;
use IteratorAggregate;
/**
* @template TKey
* @template T
*
* @implements IteratorAggregate<TKey, T>
*/
final class CachingIteratorAggregate implements IteratorAggregate
{
/**
* @var SimpleCachingIteratorAggregate<int, array{0: TKey, 1: T}>
*/
private SimpleCachingIteratorAggregate $iterator;
/**
* @param Iterator<TKey, T> $iterator
*/
public function __construct(Iterator $iterator)
{
$this->iterator = new SimpleCachingIteratorAggregate((new PackIterableAggregate($iterator))->getIterator());
}
/**
* @return Generator<TKey, T>
*/
public function getIterator(): Generator
{
foreach ($this->iterator as [$key, $current]) {
yield $key => $current;
}
}
public function hasNext(): bool
{
return $this->iterator->hasNext();
}
}