Passed
Push — master ( bc036e...f8a9bb )
by Smoren
02:19
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
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\Validator\Checks;
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 string
18
     */
19
    protected string $errorName;
20
    /**
21
     * @var callable
22
     */
23
    protected $predicate;
24
    /**
25
     * @var array<string, mixed>
26
     */
27
    protected array $params;
28
    /**
29
     * @var array<CheckInterface>
30
     */
31
    protected array $dependsOnChecks;
32
33
    /**
34
     * @param string $name
35
     * @param string $errorName
36
     * @param callable $predicate
37
     * @param array<string, mixed> $params
38
     * @param array<CheckInterface> $dependsOnChecks
39
     */
40
    public function __construct(
41
        string $name,
42
        string $errorName,
43
        callable $predicate,
44
        array $params = [],
45
        array $dependsOnChecks = []
46
    ) {
47
        $this->name = $name;
48
        $this->errorName = $errorName;
49
        $this->predicate = $predicate;
50
        $this->params = $params;
51
        $this->dependsOnChecks = $dependsOnChecks;
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57 64
    public function execute($value, array $previousErrors): void
58
    {
59 64
        foreach ($this->dependsOnChecks as $check) {
60 10
            $check->execute($value, $previousErrors);
61
        }
62
63 64
        if (($this->predicate)($value, ...array_values($this->params)) === false) {
64 39
            throw new CheckError($this->errorName, $value, $this->params);
65
        }
66
    }
67
}
68