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

PausableIteratorAggregate   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 40
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 5 1
A __construct() 0 4 1
A rest() 0 6 2
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