Passed
Push — main ( 7ad626...a806dc )
by Pol
11:45
created

PausableIteratorAggregate::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
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 Iterator;
14
use IteratorAggregate;
15
use loophp\iterators\Contract\PausableIteratorAggregateInterface;
16
use Traversable;
17
18
/**
19
 * @template TKey
20
 * @template T
21
 *
22
 * @implements IteratorAggregate<TKey, T>
23
 */
24
final class PausableIteratorAggregate implements PausableIteratorAggregateInterface
25
{
26
    /**
27
     * @var Iterator<TKey, T>
28
     */
29
    private Iterator $iterator;
30
31
    /**
32
     * @var IteratorAggregate<TKey, T>
33
     */
34
    private IteratorAggregate $iteratorAggregate;
35
36
    /**
37
     * @param Iterator<TKey, T> $iterator
38
     */
39
    public function __construct(Iterator $iterator)
40
    {
41
        $this->iteratorAggregate = new CachingIteratorAggregate($iterator);
42
        $this->iterator = new ArrayIterator();
43
    }
44
45
    /**
46
     * @return Traversable<TKey, T>
47
     */
48
    public function getIterator(): Traversable
49
    {
50
        $this->iterator = $this->iteratorAggregate->getIterator();
51
52
        yield from $this->iterator;
53
    }
54
55
    /**
56
     * @return Traversable<TKey, T>
57
     */
58
    public function rest(): Traversable
59
    {
60
        $this->iterator->next();
61
62
        for (; $this->iterator->valid(); $this->iterator->next()) {
63
            yield $this->iterator->key() => $this->iterator->current();
64
        }
65
    }
66
}
67