ValidAddingIterator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 3
Metric Value
eloc 11
c 4
b 0
f 3
dl 0
loc 71
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A key() 0 3 1
A next() 0 3 1
A rewind() 0 3 1
A current() 0 3 1
A from() 0 7 1
A valid() 0 3 1
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