|
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
|
390 |
|
public function __construct(array $constraints = [], ?TransformerExceptionConstraint $transformerExceptionConstraint = null) |
|
42
|
|
|
{ |
|
43
|
390 |
|
$this->constraints = $constraints; |
|
44
|
390 |
|
$this->transformerExceptionConstraint = $transformerExceptionConstraint ?? new TransformerExceptionConstraint(); |
|
45
|
390 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
372 |
|
public function validate($value, ElementInterface $element): FormError |
|
51
|
|
|
{ |
|
52
|
372 |
|
if (!$this->constraints) { |
|
|
|
|
|
|
53
|
208 |
|
return FormError::null(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
204 |
|
$root = $element->root(); |
|
57
|
|
|
|
|
58
|
|
|
/** @psalm-suppress TooManyArguments */ |
|
59
|
204 |
|
$errors = $root->getValidator() |
|
60
|
204 |
|
->startContext($element) |
|
|
|
|
|
|
61
|
204 |
|
->validate($value, $this->constraints, $root->constraintGroups()) |
|
62
|
204 |
|
->getViolations() |
|
63
|
|
|
; |
|
64
|
|
|
|
|
65
|
204 |
|
if ($errors->has(0)) { |
|
66
|
172 |
|
return FormError::violation($errors->get(0)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
107 |
|
return FormError::null(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
35 |
|
public function onTransformerException(Exception $exception, $value, ElementInterface $element): FormError |
|
76
|
|
|
{ |
|
77
|
35 |
|
if ($this->transformerExceptionConstraint->ignoreException) { |
|
78
|
15 |
|
return FormError::null(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** @psalm-suppress TooManyArguments */ |
|
82
|
21 |
|
$errors = $element->root() |
|
83
|
21 |
|
->getValidator() |
|
84
|
21 |
|
->startContext($element) |
|
|
|
|
|
|
85
|
21 |
|
->validate($value, $this->transformerExceptionConstraint->withException($exception)) |
|
86
|
21 |
|
->getViolations() |
|
87
|
|
|
; |
|
88
|
|
|
|
|
89
|
21 |
|
if ($errors->has(0)) { |
|
90
|
21 |
|
return FormError::violation($errors->get(0)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
return FormError::null(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* {@inheritdoc} |
|
98
|
|
|
*/ |
|
99
|
41 |
|
public function constraints(): array |
|
100
|
|
|
{ |
|
101
|
41 |
|
return $this->constraints; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get the empty value validator instance |
|
106
|
|
|
* |
|
107
|
|
|
* @return ConstraintValueValidator<mixed> |
|
108
|
|
|
*/ |
|
109
|
311 |
|
public static function empty(): self |
|
110
|
|
|
{ |
|
111
|
311 |
|
if (self::$emptyInstance) { |
|
112
|
311 |
|
return self::$emptyInstance; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
1 |
|
return self::$emptyInstance = new self(); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
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.