Passed
Push — feature/84 ( 87966d )
by Max
03:22
created

ValidAddingIterator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

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

7 Methods

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