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
|
|
|
public function getCheckNames(): array |
115
|
|
|
{ |
116
|
|
|
$result = []; |
117
|
|
|
foreach ($this->checks as $check) { |
118
|
|
|
$name = $check->getCheck()->getName(); |
119
|
|
|
if ($name !== null) { |
120
|
|
|
$result[] = $name; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
return $result; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritDoc} |
128
|
|
|
* |
129
|
|
|
* @return static |
130
|
|
|
*/ |
131
|
|
|
public function stopOnViolation(): self |
132
|
|
|
{ |
133
|
|
|
return $this->check(new RetrospectiveCheck()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritDoc} |
138
|
|
|
* |
139
|
|
|
* @return static |
140
|
|
|
*/ |
141
|
|
|
public function stopOnAnyPriorViolation(): self |
142
|
|
|
{ |
143
|
|
|
foreach ($this->checks as $check) { |
144
|
|
|
$check->setInterrupting(); |
145
|
|
|
} |
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritDoc} |
151
|
|
|
*/ |
152
|
68 |
|
public function validate($value): void |
153
|
|
|
{ |
154
|
68 |
|
$this->execute($value); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritDoc} |
159
|
|
|
*/ |
160
|
|
|
public function isValid($value): bool |
161
|
|
|
{ |
162
|
|
|
try { |
163
|
|
|
$this->validate($value); |
164
|
|
|
return true; |
165
|
|
|
} catch (ValidationError $e) { |
166
|
|
|
return false; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritDoc} |
172
|
|
|
*/ |
173
|
68 |
|
protected function execute($value): ValidationResultInterface |
174
|
|
|
{ |
175
|
68 |
|
$result = parent::execute($value); |
176
|
68 |
|
if ($result->preventNextChecks()) { |
177
|
|
|
return $result; |
178
|
|
|
} |
179
|
|
|
|
180
|
68 |
|
if ($value === null) { |
181
|
8 |
|
if ($this->isNullable) { |
182
|
5 |
|
return new ValidationSuccessResult(true); |
183
|
|
|
} |
184
|
|
|
|
185
|
3 |
|
throw new ValidationError($value, [[self::ERROR_NULL, []]]); |
186
|
|
|
} |
187
|
|
|
|
188
|
65 |
|
$errors = []; |
189
|
|
|
|
190
|
65 |
|
foreach ($this->checks as $check) { |
191
|
|
|
try { |
192
|
64 |
|
$check->getCheck()->execute($value, $errors); |
193
|
39 |
|
} catch (CheckError $e) { |
194
|
39 |
|
$errors[] = $e; |
195
|
39 |
|
if ($check->isInterrupting()) { |
196
|
12 |
|
throw ValidationError::fromCheckErrors($value, $errors); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
57 |
|
if (\count($errors) > 0) { |
202
|
27 |
|
throw ValidationError::fromCheckErrors($value, $errors); |
203
|
|
|
} |
204
|
|
|
|
205
|
33 |
|
return new ValidationSuccessResult(false); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|