ValidAddingIterator::next()   A
last analyzed

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 only adds values if source is valid.
9
 * @template TKey
10
 * @template TValue
11
 * @implements AddingIterator<TKey, TValue>
12
 */
13
final class ValidAddingIterator implements AddingIterator
14
{
15
16
    /**
17
     * Ctor.
18
     * 
19
     * @phpstan-param AddingIterator<TKey, TValue> $origin
20
     * @param AddingIterator $origin original adding iterator.
21
     */
22
    public function __construct(
23
        /**
24
         * Original adding iterator.
25
         *
26
         * @phpstan-var AddingIterator<TKey, TValue>
27
         * @var AddingIterator
28
         */
29
        private AddingIterator $origin
30
    ) {
31
    }
32
33
    /**
34
     * {@inheritDoc}
35
     * Only adds values if source is valid.
36
     */
37
    public function from(Iterator $source): AddingIterator
38
    {
39
        return (new ValidTernary(
40
            $source,
41
            fn (Iterator $iter) => new self($this->origin->from($iter)),
42
            fn () => $this
43
        ))->value();
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49
    public function current(): mixed
50
    {
51
        return $this->origin->current();
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57
    public function key(): mixed
58
    {
59
        return $this->origin->key();
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65
    public function next(): void
66
    {
67
        $this->origin->next();
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73
    public function valid(): bool
74
    {
75
        return $this->origin->valid();
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81
    public function rewind(): void
82
    {
83
        $this->origin->rewind();
84
    }
85
}
86