Passed
Push — master ( 9d1d31...2b74c9 )
by Smoren
02:13
created

Rule::stopOnViolation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\Validator\Rules;
6
7
use Smoren\Validator\Checks\Check;
8
use Smoren\Validator\Checks\RetrospectiveCheck;
9
use Smoren\Validator\Exceptions\CheckError;
10
use Smoren\Validator\Exceptions\ValidationError;
11
use Smoren\Validator\Interfaces\CheckInterface;
12
use Smoren\Validator\Interfaces\CheckWrapperInterface;
13
use Smoren\Validator\Interfaces\RuleInterface;
14
use Smoren\Validator\Interfaces\ValidationResultInterface;
15
use Smoren\Validator\Structs\CheckWrapper;
16
use Smoren\Validator\Structs\ValidationSuccessResult;
17
18
class Rule extends BaseRule implements RuleInterface
19
{
20
    public const ERROR_NULL = 'null';
21
    public const ERROR_NOT_TRUTHY = 'not_truthy';
22
    public const ERROR_NOT_FALSY = 'not_falsy';
23
    public const ERROR_NOT_EQUEAL = 'equal';
24
    public const ERROR_NOT_SAME = 'same';
25
26
    /**
27
     * @var array<CheckWrapperInterface>
28
     */
29
    protected array $checks = [];
30
    /**
31
     * @var bool
32
     */
33
    protected bool $isNullable = false;
34
35
    /**
36
     * {@inheritDoc}
37
     *
38
     * @return static
39
     */
40
    public function nullable(): self
41
    {
42
        $this->isNullable = true;
43
        return $this;
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     *
49
     * @return static
50
     */
51
    public function truthy(): self
52
    {
53
        return $this->check(new Check(
54
            self::ERROR_NOT_TRUTHY,
55
            fn ($value) => boolval($value),
56
        ));
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     *
62
     * @return static
63
     */
64
    public function falsy(): self
65
    {
66
        return $this->check(new Check(
67
            self::ERROR_NOT_FALSY,
68
            fn ($value) => !boolval($value),
69
        ));
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     *
75
     * @return static
76
     */
77
    public function equal($values): self
78
    {
79
        return $this->check(new Check(
80
            self::ERROR_NOT_EQUEAL,
81
            fn ($value) => $value == $values,
82
            ['number' => $values]
83
        ));
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     *
89
     * @return static
90
     */
91
    public function same($value): self
92
    {
93
        return $this->check(new Check(
94
            self::ERROR_NOT_SAME,
95
            fn ($value) => $value === $value,
96
            ['number' => $value]
97
        ));
98
    }
99
100
    /**
101
     * {@inheritDoc}
102
     *
103
     * @return static
104
     */
105
    public function check(CheckInterface $check, bool $isInterrupting = false): self
106
    {
107
        $this->checks[] = new CheckWrapper($check, $isInterrupting);
108
        return $this;
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     *
114
     * @return static
115
     */
116
    public function stopOnViolation(): self
117
    {
118
        return $this->check(new RetrospectiveCheck());
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     *
124
     * @return static
125
     */
126
    public function stopOnAnyPriorViolation(): self
127
    {
128
        foreach ($this->checks as $check) {
129
            $check->setInterrupting();
130
        }
131
        return $this;
132
    }
133
134
    /**
135
     * {@inheritDoc}
136
     */
137 68
    protected function execute($value): ValidationResultInterface
138
    {
139 68
        $result = parent::execute($value);
140 68
        if ($result->preventNextChecks()) {
141
            return $result;
142
        }
143
144 68
        if ($value === null) {
145 8
            if ($this->isNullable) {
146 5
                return new ValidationSuccessResult(true);
147
            }
148
149 3
            throw new ValidationError($value, [[self::ERROR_NULL, []]]);
150
        }
151
152 65
        $errors = [];
153
154 65
        foreach ($this->checks as $check) {
155
            try {
156 64
                $check->getCheck()->execute($value, $errors);
157 39
            } catch (CheckError $e) {
158 39
                $errors[] = $e;
159 39
                if ($check->isInterrupting()) {
160 12
                    throw ValidationError::fromCheckErrors($value, $errors);
161
                }
162
            }
163
        }
164
165 57
        if (\count($errors) > 0) {
166 27
            throw ValidationError::fromCheckErrors($value, $errors);
167
        }
168
169 33
        return new ValidationSuccessResult(false);
170
    }
171
}
172