Passed
Push — master ( 6deecb...1d8eda )
by Smoren
02:15
created

Check::setInterrupting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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