Passed
Push — master ( bc036e...f8a9bb )
by Smoren
02:19
created

Check   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 45.45%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 55
ccs 5
cts 11
cp 0.4545
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 12 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 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