Test Failed
Push — feature/83 ( 94c196...f48b87 )
by Max
05:27 queued 02:23
created

ValidAddingIterator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 1
eloc 0
c 2
b 0
f 2
nc 1
nop 1
dl 0
loc 9
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 ($source->valid())
40
            ? new self($this->origin->from($source))
41
            : $this;
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    public function current(): mixed
48
    {
49
        return $this->origin->current();
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    public function key(): mixed
56
    {
57
        return $this->origin->key();
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function next(): void
64
    {
65
        $this->origin->next();
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    public function valid(): bool
72
    {
73
        return $this->origin->valid();
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79
    public function rewind(): void
80
    {
81
        $this->origin->rewind();
82
    }
83
}
84