Passed
Push — master ( b66066...5b471b )
by Smoren
02:41
created

Check::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\Validator\Structs;
6
7
use Smoren\Validator\Exceptions\CheckError;
8
use Smoren\Validator\Interfaces\CheckInterface;
9
10
class Check implements CheckInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    protected string $name;
16
    /**
17
     * @var callable
18
     */
19
    protected $predicate;
20
    /**
21
     * @var array<string, mixed>
22
     */
23
    protected array $params;
24
    /**
25
     * @var bool
26
     */
27
    protected bool $isInterrupting;
28
    /**
29
     * @var array<CheckInterface>
30
     */
31
    protected array $dependsOn;
32
33
    /**
34
     * @param string $name
35
     * @param callable $predicate
36
     * @param array<string, mixed> $params
37
     * @param bool $isInterrupting
38
     * @param array<CheckInterface> $dependsOn
39
     */
40
    public function __construct(
41
        string $name,
42
        callable $predicate,
43
        array $params = [],
44
        bool $isInterrupting = false,
45
        array $dependsOn = []
46
    )
47
    {
48
        $this->name = $name;
49
        $this->predicate = $predicate;
50
        $this->params = $params;
51
        $this->isInterrupting = $isInterrupting;
52
        $this->dependsOn = $dependsOn;
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58 62
    public function execute($value, array $previousErrors): void
59
    {
60 62
        foreach ($this->dependsOn as $check) {
61 4
            $check->execute($value, $previousErrors);
62
        }
63
64 62
        if (($this->predicate)($value, ...array_values($this->params)) === false) {
65 38
            throw new CheckError($this->name, $value, $this->params);
66
        }
67
    }
68
69
    /**
70
     * @return bool
71
     */
72 38
    public function isInterrupting(): bool
73
    {
74 38
        return $this->isInterrupting;
75
    }
76
77
    /**
78
     * @param bool $value
79
     *
80
     * @return static
81
     */
82
    public function setInterrupting(bool $value = true): CheckInterface
83
    {
84
        $this->isInterrupting = $value;
85
        return $this;
86
    }
87
}
88