Passed
Push — master ( 17adcb...9acb8c )
by Smoren
12:18
created

Check::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 2
nc 2
nop 2
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
    /**
30
     * @param string $name
31
     * @param callable $predicate
32
     * @param array<string, mixed> $params
33
     * @param bool $isInterrupting
34
     */
35 2
    public function __construct(string $name, callable $predicate, array $params = [], bool $isInterrupting = false)
36
    {
37 2
        $this->name = $name;
38 2
        $this->predicate = $predicate;
39 2
        $this->params = $params;
40 2
        $this->isInterrupting = $isInterrupting;
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46 57
    public function execute($value, array $previousErrors): void
47
    {
48 57
        if (($this->predicate)($value, ...array_values($this->params)) === false) {
49 35
            throw new CheckError($this->name, $value, $this->params);
50
        }
51
    }
52
53
    /**
54
     * @return bool
55
     */
56 35
    public function isInterrupting(): bool
57
    {
58 35
        return $this->isInterrupting;
59
    }
60
61
    /**
62
     * @param bool $value
63
     *
64
     * @return static
65
     */
66
    public function setInterrupting(bool $value = true): CheckInterface
67
    {
68
        $this->isInterrupting = $value;
69
        return $this;
70
    }
71
}
72