Passed
Push — master ( 622029...98ce94 )
by Smoren
02:10
created

Check::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 10
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 callable
18
     */
19
    protected $predicate;
20
    /**
21
     * @var array<string, mixed>
22
     */
23
    protected array $params;
24
    /**
25
     * @var array<CheckInterface>
26
     */
27
    protected array $dependsOn;
28
29
    /**
30
     * @param string $name
31
     * @param callable $predicate
32
     * @param array<string, mixed> $params
33
     * @param array<CheckInterface> $dependsOn
34
     */
35
    public function __construct(
36
        string $name,
37
        callable $predicate,
38
        array $params = [],
39
        array $dependsOn = []
40
    ) {
41
        $this->name = $name;
42
        $this->predicate = $predicate;
43
        $this->params = $params;
44
        $this->dependsOn = $dependsOn;
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50 64
    public function execute($value, array $previousErrors): void
51
    {
52 64
        foreach ($this->dependsOn as $check) {
53 9
            $check->execute($value, $previousErrors);
54
        }
55
56 64
        if (($this->predicate)($value, ...array_values($this->params)) === false) {
57 39
            throw new CheckError($this->name, $value, $this->params);
58
        }
59
    }
60
}
61