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

ValidAddingIterator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 9
c 2
b 0
f 2
dl 0
loc 69
rs 10
wmc 8

7 Methods

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