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

Check   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 48
ccs 5
cts 10
cp 0.5
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 8 3
A __construct() 0 10 1
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