Passed
Push — main ( b736ca...c356e6 )
by Pol
01:55
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 Generator;
13
use IteratorAggregate;
14
15
/**
16
 * @template TKey
17
 * @template T
18
 *
19
 * @implements IteratorAggregate<TKey, T>
20
 */
21
final class InterruptableIterableIteratorAggregate implements IteratorAggregate
22
{
23
    public const BREAK = 'break';
24
25
    /**
26
     * @var iterable<TKey, T>
27
     */
28
    private iterable $iterable;
29
30
    /**
31
     * @param iterable<TKey, T> $iterable
32
     */
33 2
    public function __construct(iterable $iterable)
34
    {
35 2
        $this->iterable = $iterable;
36
    }
37
38
    /**
39
     * @return Generator<Generator<TKey, T>, array{0: TKey, 1: T}>
40
     */
41 2
    public function getIterator(): Generator
42
    {
43 2
        $generator = $this->getGenerator();
44
45 2
        foreach ($generator as $key => $value) {
46 2
            yield $generator => [$key, $value];
47
        }
48
    }
49
50
    /**
51
     * @return Generator<TKey, T>
52
     */
53 2
    private function getGenerator(): Generator
54
    {
55 2
        foreach ($this->iterable as $key => $value) {
56
            /** @var mixed $return */
57 2
            $return = yield $key => $value;
58
59 2
            if (self::BREAK === $return) {
60 2
                break;
61
            }
62
        }
63
    }
64
}
65