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

Check   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 32
c 1
b 1
f 0
dl 0
loc 85
ccs 27
cts 27
cp 1
rs 10
wmc 9

2 Methods

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