Passed
Push — master ( 437e87...30bcee )
by Smoren
02:21
created

Check::execute()   B

Complexity

Conditions 8
Paths 25

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 19
c 0
b 0
f 0
nc 25
nop 3
dl 0
loc 31
ccs 20
cts 20
cp 1
crap 8
rs 8.4444
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\Exceptions\ValidationError;
9
use Smoren\Validator\Interfaces\CheckInterface;
10
use Smoren\Validator\Structs\Param;
11
12
class Check implements CheckInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    protected string $name;
18
    /**
19
     * @var callable
20
     */
21
    protected $predicate;
22
    /**
23
     * @var array<string, mixed>
24
     */
25
    protected array $params;
26
    /**
27
     * @var array<string, callable>
28
     */
29
    protected array $calculatedParams;
30
    /**
31
     * @var array<CheckInterface>
32
     */
33
    protected array $dependsOnChecks;
34
    /**
35
     * @var bool
36
     */
37
    protected bool $preventDuplicate;
38
39
    /**
40
     * @param string $name
41
     * @param callable $predicate
42
     * @param array<string, mixed> $params
43
     * @param array<string, callable> $calculatedParams
44
     * @param array<CheckInterface> $dependsOnChecks
45
     * @param bool $preventDuplicate
46
     */
47 238
    public function __construct(
48
        string $name,
49
        callable $predicate,
50
        array $params = [],
51
        array $calculatedParams = [],
52
        array $dependsOnChecks = [],
53
        bool $preventDuplicate = false
54
    ) {
55 238
        $this->name = $name;
56 238
        $this->predicate = $predicate;
57 238
        $this->params = $params;
58 238
        $this->calculatedParams = $calculatedParams;
59 238
        $this->dependsOnChecks = $dependsOnChecks;
60 238
        $this->preventDuplicate = $preventDuplicate;
61
    }
62
63
    /**
64
     * {@inheritDoc}
65
     */
66 232
    public function execute($value, array $previousErrors, bool $preventDuplicate = false): void
67
    {
68 232
        if ($preventDuplicate) {
69 33
            foreach ($previousErrors as $error) {
70 4
                if ($error->getName() === $this->name) {
71 1
                    return;
72
                }
73
            }
74
        }
75
76 232
        foreach ($this->dependsOnChecks as $check) {
77 33
            $check->execute(
78 33
                $value,
79 33
                $previousErrors,
80 33
                true
81 33
            );
82
        }
83
84 232
        $params = $this->params;
85 232
        foreach ($this->calculatedParams as $key => $paramGetter) {
86 3
            $params[$key] = $paramGetter($value);
87
        }
88
89
        try {
90 232
            if (($this->predicate)($value, ...array_values($params)) === false) {
91 232
                throw new CheckError($this->name, $value, $params);
92
            }
93 125
        } catch (ValidationError $e) {
94 10
            $params[Param::RULE] = $e->getName();
95 10
            $params[Param::VIOLATED_RESTRICTIONS] = $e->getViolatedRestrictions();
96 10
            throw new CheckError($this->name, $value, $params);
97
        }
98
    }
99
}
100