Passed
Branch main (85a504)
by Pol
13:36
created

SimpleCachingIteratorAggregate::getIterator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
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 CachingIterator;
13
use Iterator;
14
use IteratorAggregate;
15
use Traversable;
16
17
/**
18
 * @template TKey
19
 * @template T
20
 *
21
 * @implements IteratorAggregate<TKey, T>
22
 */
23
final class SimpleCachingIteratorAggregate implements IteratorAggregate
24
{
25
    /**
26
     * @var CachingIterator<TKey, T>
27
     */
28
    private CachingIterator $iterator;
29
30
    /**
31
     * @param Iterator<TKey, T> $iterator
32
     */
33
    public function __construct(Iterator $iterator)
34
    {
35
        $this->iterator = new CachingIterator(
36
            $iterator,
37
            CachingIterator::FULL_CACHE
38
        );
39
    }
40
41
    /**
42
     * @return Traversable<TKey, T>
43
     */
44
    public function getIterator(): Traversable
45
    {
46
        /** @var iterable<TKey, T> $traversable */
47
        $traversable = $this->iterator->getInnerIterator()->valid()
48
            ? $this->iterator
49
            : $this->iterator->getCache();
50
51
        yield from $traversable;
52
    }
53
}
54