Test Failed
Push — feature/66 ( b40fab...9b60dc )
by Max
02:26
created

ArrayAddingIterator::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
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
 * Adding iterator which stores values in an array.
9
 * @template TKey
10
 * @template TValue
11
 * @implements AddingIterator<TKey, TValue>
12
 */
13
final class ArrayAddingIterator implements AddingIterator
14
{
15
16
    /**
17
     * Ctor.
18
     * 
19
     * @phpstan-param array<TKey, TValue> $added
20
     * @param array $added added values.
21
     */
22
    public function __construct(
23
        /**
24
         * Added values.
25
         *
26
         * @var array
27
         */
28
        private array $added = []
29
    ) {
30
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35
    public function from(Iterator $source): AddingIterator
36
    {
37
        $this->added[$source->key()] ??= $source->current();
38
        return new self($this->added);
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     */
44
    public function current(): mixed
45
    {
46
        return current($this->added);
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function key(): mixed
53
    {
54
        return key($this->added);
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    public function next(): void
61
    {
62
        next($this->added);
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68
    public function valid(): bool
69
    {
70
        return $this->key() !== null;
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    public function rewind(): void
77
    {
78
        reset($this->added);
79
    }
80
}
81