Completed
Pull Request — master (#182)
by Max
02:17
created

ValidTernary::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 0
c 1
b 0
f 1
nc 1
nop 3
dl 0
loc 22
rs 10
1
<?php
2
3
namespace MaxGoryunov\SavingIterator\Src;
4
5
use Closure;
6
use Iterator;
7
8
/**
9
 * Class which checks whether the given iterator is valid and calls consequent
10
 * path if it is true and alternate path otherwise.
11
 */
12
final class ValidTernary implements Scalar
13
{
14
    /**
15
     * Ctor.
16
     *
17
     * @param Iterator $origin iterator which validity is checked.
18
     * @param Closure $cons    consequent path if iterator is valid.
19
     * @param Closure $alter   alternative path if iterator is not valid.
20
     */
21
    public function __construct(
22
        /**
23
         * Iterator which validity is checked.
24
         *
25
         * @var Iterator
26
         */
27
        private Iterator $origin,
28
29
        /**
30
         * Consequent path if iterator is valid.
31
         *
32
         * @var Closure
33
         */
34
        private Closure $cons,
35
36
        /**
37
         * Alternate path if iterator is not valid.
38
         *
39
         * @var Closure
40
         */
41
        private Closure $alter
42
    ) {
43
    }
44
45
    public function value(): mixed
46
    {
47
        if ($this->origin->valid()) {
48
            $path = $this->cons;
49
        } else {
50
            $path = $this->alter;
51
        }
52
        return $path($this->origin);
53
    }
54
}
55