Completed
Push — master ( ca7542...0e811b )
by
unknown
03:00
created

SavingIterator::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 3
Metric Value
cc 1
eloc 1
c 5
b 0
f 3
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace MaxGoryunov\SavingIterator\Src;
4
5
use Iterator;
6
7
/**
8
 * Iterator which stores iterated values.
9
 * @template TKey
10
 * @template TValue
11
 * @extends IteratorEnvelope<TKey, TValue>
12
 */
13
final class SavingIterator extends IteratorEnvelope
14
{
15
    /**
16
     * Ctor.
17
     * 
18
     * @phpstan-param Iterator<TKey, TValue>       $origin
19
     * @phpstan-param AddingIterator<TKey, TValue> $target
20
     * @param Iterator       $origin original iterator.
21
     * @param AddingIterator $target iterator to which the values are saved.
22
     */
23
    public function __construct(
24
        Iterator $origin,
25
        AddingIterator $target
26
    ) {
27
        parent::__construct(
28
            /** @phpstan-ignore-next-line */
29
            new ContextVeil(
30
                $target,
31
                new ClosureReaction(
32
                    function (AddingIterator $stored) use ($origin) {
33
                        $res = $stored;
34
                        if ($origin->valid()) {
35
                            $res = $stored->from($origin);
36
                            $origin->next();
37
                        }
38
                        return $res;
39
                    }
40
                )
41
            )
42
        );
43
    }
44
}
45