-
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathValidator.php
More file actions
311 lines (253 loc) · 9.82 KB
/
Validator.php
File metadata and controls
311 lines (253 loc) · 9.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
declare(strict_types=1);
namespace Tempest\Validation;
use Closure;
use Tempest\Container\Singleton;
use Tempest\Intl\Translator;
use Tempest\Reflection\ClassReflector;
use Tempest\Reflection\PropertyReflector;
use Tempest\Support\Arr;
use Tempest\Validation\Exceptions\TranslatorWasRequired;
use Tempest\Validation\Exceptions\ValidationFailed;
use Tempest\Validation\Rules\IsBoolean;
use Tempest\Validation\Rules\IsEnum;
use Tempest\Validation\Rules\IsFloat;
use Tempest\Validation\Rules\IsInteger;
use Tempest\Validation\Rules\IsNotNull;
use Tempest\Validation\Rules\IsString;
use function Tempest\Support\arr;
use function Tempest\Support\str;
/**
* @phpstan-type ValidationClosure Closure(mixed $value): (bool|string|null)
* @phpstan-type ValidationRule Rule|ValidationClosure|false|null
*/
#[Singleton]
final readonly class Validator
{
public function __construct(
private ?Translator $translator = null,
) {}
/**
* Validates the values of public properties on the specified object using attribute rules.
*/
public function validateObject(object $object): void
{
$class = new ClassReflector($object);
$failingRules = [];
foreach ($class->getPublicProperties() as $property) {
if (! $property->isInitialized($object)) {
continue;
}
$value = $property->getValue($object);
$failingRules[$property->getName()] = $this->validateValueForProperty($property, $value);
}
if ($failingRules !== []) {
throw $this->createValidationFailureException($failingRules, $object);
}
}
/**
* Creates a {@see ValidationFailed} exception from the given rule failures, populated with error messages.
*
* @param array<string,list<FailingRule>> $failingRules
* @param class-string|null $targetClass
*/
public function createValidationFailureException(array $failingRules, null|object|string $subject = null, ?string $targetClass = null): ValidationFailed
{
return new ValidationFailed(
failingRules: $failingRules,
subject: $subject,
errorMessages: Arr\map($failingRules, fn (array $rules, string $field) => Arr\map($rules, fn (FailingRule $rule) => $this->getErrorMessage($rule, $field))),
targetClass: $targetClass,
);
}
/**
* Validates the specified `$values` for the corresponding public properties on the specified `$class`, using built-in PHP types and attribute rules.
*
* @param ClassReflector|class-string $class
* @return array<string,list<FailingRule>>
*/
public function validateValuesForClass(ClassReflector|string $class, ?array $values, string $prefix = ''): array
{
$class = is_string($class) ? new ClassReflector($class) : $class;
$failingRules = [];
$values = arr($values)->undot();
foreach ($class->getPublicProperties() as $property) {
if ($property->hasAttribute(SkipValidation::class)) {
continue;
}
$key = $prefix . $property->getName();
if (! $values->hasKey($key) && $property->hasDefaultValue()) {
continue;
}
$value = $values->get($key);
$failingRulesForProperty = $this->validateValueForProperty($property, $value);
if ($failingRulesForProperty !== []) {
$failingRules[$key] = $failingRulesForProperty;
}
if ($property->isNullable() && $value === null) {
continue;
}
if ($property->getType()->isClass() && ! $property->getType()->isEnum()) {
$failingRules = [
...$failingRules,
...$this->validateValuesForClass(
class: $property->getType()->asClass(),
values: $values->dot()->toArray(),
prefix: $key . '.',
),
];
}
}
return $failingRules;
}
/**
* Validates `$value` against the specified `$property`, using built-in PHP types and attribute rules.
*
* @return list<FailingRule>
*/
public function validateValueForProperty(PropertyReflector $property, mixed $value): array
{
$rules = $property->getAttributes(Rule::class);
if ($property->getType()->isScalar()) {
$rules[] = match ($property->getType()->getName()) {
'string' => new IsString(orNull: $property->isNullable()),
'int' => new IsInteger(orNull: $property->isNullable()),
'float' => new IsFloat(orNull: $property->isNullable()),
'bool' => new IsBoolean(orNull: $property->isNullable()),
default => null,
};
} elseif (! $property->isNullable()) {
// We only add the NotNull rule if we're not dealing with scalar types, since the null check is included in the scalar rules
$rules[] = new IsNotNull();
}
if ($property->getType()->isEnum()) {
$rules[] = new IsEnum(enum: $property->getType()->getName(), orNull: $property->isNullable());
}
$key = $property->getAttribute(TranslationKey::class)?->key;
return Arr\map(
array: $this->validateValue($value, $rules),
map: fn (FailingRule $rule) => $rule->withKey($key),
);
}
/**
* Validates the specified `$value` against the specified set of `$rules`. If a rule is a closure, it may return a string as a validation error.
*
* @param Rule|ValidationClosure|array<ValidationRule> $rules
* @return list<FailingRule>
*/
public function validateValue(mixed $value, Closure|Rule|array $rules): array
{
$failingRules = [];
foreach (Arr\wrap($rules) as $rule) {
if (! $rule) {
continue;
}
$rule = $this->convertToRule($rule, $value);
if (! $rule->isValid($value)) {
$failingRules[] = new FailingRule($rule, value: $value);
}
}
return $failingRules;
}
/**
* Validates the specified `$values` against the specified set `$rules`.
* The `$rules` array is expected to have the same keys as `$values`, associated with instance of {@see Tempest\Validation\Rule}.
* If `$rules` doesn't contain a key for a value, it will not be validated.
*
* @param iterable<string,mixed> $values
* @param array<string,Rule|ValidationClosure|array<ValidationRule>> $rules
*
* @return array<string,list<FailingRule>>
*/
public function validateValues(iterable $values, array $rules): array
{
$failingRules = [];
foreach ($values as $key => $value) {
if (! array_key_exists($key, $rules)) {
continue;
}
if ($failures = $this->validateValue($value, $rules[$key])) {
$failingRules[$key] = $failures;
}
}
return $failingRules;
}
/**
* Gets a localized validation error message for the specified rule.
*/
public function getErrorMessage(Rule|FailingRule $rule, ?string $field = null): string
{
if (is_null($this->translator)) {
throw new TranslatorWasRequired();
}
if ($rule instanceof HasErrorMessage) {
return $rule->getErrorMessage();
}
$ruleTranslationKey = $this->getTranslationKey($rule);
if ($rule instanceof FailingRule) {
$field ??= $rule->field;
$rule = $rule->rule;
}
$variables = [
'field' => $this->getFieldName($ruleTranslationKey, $field),
];
if ($rule instanceof HasTranslationVariables) {
$variables = [...$variables, ...$rule->getTranslationVariables()];
}
return $this->translator->translate("validation_error.{$ruleTranslationKey}", ...$variables);
}
private function getTranslationKey(Rule|FailingRule $rule): string
{
$key = '';
if ($rule instanceof FailingRule && $rule->key) {
$key .= $rule->key;
}
if ($rule instanceof FailingRule) {
$rule = $rule->rule;
}
return str($rule::class)
->classBasename()
->snake()
->replaceEvery([ // those are snake case issues that we manually fix for consistency
'i_pv6' => 'ipv6',
'i_pv4' => 'ipv4',
'reg_ex' => 'regex',
])
->when($key !== '', fn ($s) => $s->append('.', $key))
->toString();
}
private function getFieldName(string $key, ?string $field = null): string
{
$translatedField = $this->translator?->translate("validation_field.{$key}");
if ($translatedField === "validation_field.{$key}") {
return $field ?? 'Value';
}
return $field ?? $translatedField;
}
private function convertToRule(Rule|Closure $rule, mixed $value): Rule
{
if ($rule instanceof Rule) {
return $rule;
}
$result = $rule($value);
[$isValid, $message] = match (true) {
is_string($result) => [false, $result],
$result === false => [false, 'Value did not pass validation.'],
default => [true, ''],
};
return new class($isValid, $message) implements Rule, HasErrorMessage {
public function __construct(
private bool $isValid,
public string $message,
) {}
public function isValid(mixed $value): bool
{
return $this->isValid;
}
public function getErrorMessage(): string
{
return $this->message;
}
};
}
}