|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Form\Validator; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Form\ElementInterface; |
|
6
|
|
|
use Bdf\Form\Error\FormError; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Symfony\Component\Validator\Constraint; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Value validator using symfony constraint |
|
12
|
|
|
* The element will be used as "root" context object on the symfony validator |
|
13
|
|
|
* |
|
14
|
|
|
* @template T |
|
15
|
|
|
* @implements ValueValidatorInterface<T> |
|
16
|
|
|
*/ |
|
17
|
|
|
final class ConstraintValueValidator implements ValueValidatorInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var self |
|
21
|
|
|
*/ |
|
22
|
|
|
private static $emptyInstance; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Constraint[] |
|
26
|
|
|
*/ |
|
27
|
|
|
private $constraints; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var TransformerExceptionConstraint |
|
31
|
|
|
*/ |
|
32
|
|
|
private $transformerExceptionConstraint; |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* ConstraintValueValidator constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param Constraint[] $constraints |
|
39
|
|
|
* @param TransformerExceptionConstraint|null $transformerExceptionConstraint |
|
40
|
|
|
*/ |
|
41
|
417 |
|
public function __construct(array $constraints = [], ?TransformerExceptionConstraint $transformerExceptionConstraint = null) |
|
42
|
|
|
{ |
|
43
|
417 |
|
$this->constraints = $constraints; |
|
44
|
417 |
|
$this->transformerExceptionConstraint = $transformerExceptionConstraint ?? new TransformerExceptionConstraint(); |
|
45
|
417 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
399 |
|
public function validate($value, ElementInterface $element): FormError |
|
51
|
|
|
{ |
|
52
|
399 |
|
if (!$this->constraints) { |
|
|
|
|
|
|
53
|
205 |
|
return FormError::null(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
224 |
|
$root = $element->root(); |
|
57
|
224 |
|
$groups = $root->constraintGroups(); |
|
58
|
|
|
|
|
59
|
|
|
/** @psalm-suppress TooManyArguments */ |
|
60
|
224 |
|
$context = $root->getValidator()->startContext($element); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
224 |
|
foreach ($this->constraints as $constraint) { |
|
63
|
224 |
|
$errors = $context->validate($value, $constraint, $groups)->getViolations(); |
|
64
|
|
|
|
|
65
|
224 |
|
if ($errors->has(0)) { |
|
66
|
188 |
|
return FormError::violation($errors->get(0)); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
115 |
|
return FormError::null(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
38 |
|
public function onTransformerException(Exception $exception, $value, ElementInterface $element): FormError |
|
77
|
|
|
{ |
|
78
|
38 |
|
if ($this->transformerExceptionConstraint->ignoreException) { |
|
79
|
17 |
|
return FormError::null(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** @psalm-suppress TooManyArguments */ |
|
83
|
22 |
|
$errors = $element->root() |
|
84
|
22 |
|
->getValidator() |
|
85
|
22 |
|
->startContext($element) |
|
|
|
|
|
|
86
|
22 |
|
->validate($value, $this->transformerExceptionConstraint->withException($exception)) |
|
87
|
22 |
|
->getViolations() |
|
88
|
|
|
; |
|
89
|
|
|
|
|
90
|
22 |
|
if ($errors->has(0)) { |
|
91
|
22 |
|
return FormError::violation($errors->get(0)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
return FormError::null(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritdoc} |
|
99
|
|
|
*/ |
|
100
|
46 |
|
public function constraints(): array |
|
101
|
|
|
{ |
|
102
|
46 |
|
return $this->constraints; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* {@inheritdoc} |
|
107
|
|
|
*/ |
|
108
|
54 |
|
public function hasConstraints(): bool |
|
109
|
|
|
{ |
|
110
|
54 |
|
return !empty($this->constraints); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get the empty value validator instance |
|
115
|
|
|
* |
|
116
|
|
|
* @return ConstraintValueValidator<mixed> |
|
117
|
|
|
*/ |
|
118
|
329 |
|
public static function empty(): self |
|
119
|
|
|
{ |
|
120
|
329 |
|
if (self::$emptyInstance) { |
|
121
|
329 |
|
return self::$emptyInstance; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
return self::$emptyInstance = new self(); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.