-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormError.php
319 lines (277 loc) · 7.72 KB
/
FormError.php
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
312
313
314
315
316
317
318
319
<?php
namespace Bdf\Form\Error;
use Bdf\Form\Child\ChildInterface;
use Bdf\Form\Child\Http\HttpFieldPath;
use Bdf\Form\ElementInterface;
use InvalidArgumentException;
use Stringable;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationInterface;
/**
* Store errors of a form element
*
* @see ElementInterface::error()
* @see ChildInterface::error()
*/
final class FormError implements Stringable
{
/**
* @var FormError|null
*/
private static $null;
/**
* @var HttpFieldPath|null
*/
private $field;
/**
* @var string|null
*/
private $global;
/**
* @var string|null
*/
private $code;
/**
* @var FormError[]
*/
private $children;
/**
* FormError constructor.
* Prefer use static methods to instantiate the FormError
*
* @param string|null $global
* @param string|null $code
* @param FormError[] $children
*/
public function __construct(?string $global, ?string $code, array $children)
{
$this->global = $global;
$this->code = $code;
$this->children = $children;
}
/**
* Get the HTTP field name of the current element
*
* @return HttpFieldPath|null The field path, or null is it's on the root element
*/
public function field(): ?HttpFieldPath
{
return $this->field;
}
/**
* Get the error of the current element, or the global error on an aggregate element
*
* @return string|null The error message, or null if there is no errors
*/
public function global(): ?string
{
return $this->global;
}
/**
* The error code of the current element
*
* @return string|null The error code, or null if not provided
*/
public function code(): ?string
{
return $this->code;
}
/**
* Get the children's errors
* The errors are indexed by the child's name
* Contains only non-empty errors
*
* @return FormError[]
*/
public function children(): array
{
return $this->children;
}
/**
* Check if the error object is an empty one
*
* If true, there is no errors, neither on current elements, nor on children
* An element returning an empty error is a valid one
*
* @return bool
*/
public function empty(): bool
{
return empty($this->global) && empty($this->code) && empty($this->children);
}
/**
* Get a single error message
*
* Note: if the error is an aggregate error, the method will return null,
* so it must not be used to check if a child has an error
*
* @param string $child The child name
*
* @return string|null The child error message, or null if the child has no global error, or is not found
* @since 1.5
*/
public function get(string $child): ?string
{
$child = $this->children[$child] ?? null;
return $child ? $child->global : null;
}
/**
* Get the error of a child
* An object is always returned, even if the child has no error, so check {@see FormError::empty()} to know if the child has an error
* @param string $child The child name
*
* @return FormError The child error
* @since 1.5
*/
public function child(string $child): FormError
{
return $this->children[$child] ?? self::null();
}
/**
* Export the errors into an array
*
* - The errors are indexed by the children's name
* - If a child contains a "global" error, the error value will be the global error
* - Else, call toArray() on the child's error
* - If the current element contains a global error, return it at the int(0) index
*
* @return array
*/
public function toArray(): array
{
$errors = [];
if ($this->global) {
$errors[0] = $this->global;
}
foreach ($this->children as $name => $child) {
if ($child->global) {
$errors[$name] = $child->global;
} else {
$errors[$name] = $child->toArray();
}
}
return $errors;
}
/**
* Format the error using the given printer
*
* @param FormErrorPrinterInterface $printer
*
* @return mixed The printer result
*/
public function print(FormErrorPrinterInterface $printer)
{
if ($this->field) {
$printer->field($this->field);
}
if ($this->global) {
$printer->global($this->global);
}
if ($this->code) {
$printer->code($this->code);
}
foreach ($this->children as $name => $child) {
$printer->child($name, $child);
}
return $printer->print();
}
/**
* Format errors as string
*
* @return string
*/
public function __toString(): string
{
return $this->print(new StringErrorPrinter());
}
/**
* Add the HTTP field on the form error
* The field will be applied to all children as prefix
*
* @param HttpFieldPath $field The current element field
*
* @return FormError The new FormError instance
*/
public function withField(HttpFieldPath $field): FormError
{
$error = clone $this;
$error->field = $field;
$error->children = [];
foreach ($this->children as $name => $child) {
$error->children[$name] = $child->withPrefixField($field);
}
return $error;
}
/**
* Get an empty error instance
*
* @return FormError
*/
public static function null(): FormError
{
if (self::$null) {
return self::$null;
}
return self::$null = new FormError(null, null, []);
}
/**
* Creates an error containing only the global message
*
* @param string $message The error message
* @param string|null $code The error code
*
* @return FormError
*/
public static function message(string $message, ?string $code = null): FormError
{
return new FormError($message, $code, []);
}
/**
* Creates an aggregation of children errors
*
* @param FormError[] $errors The children errors, indexed by the child name
*
* @return FormError
*/
public static function aggregate(array $errors): FormError
{
return new FormError(null, null, $errors);
}
/**
* Create a form error from a symfony violation
*
* @param ConstraintViolationInterface $violation The violation instance
*
* @return FormError
*/
public static function violation(ConstraintViolationInterface $violation): FormError
{
$message = (string) $violation->getMessage();
$code = $violation->getCode();
if ($code !== null && $violation instanceof ConstraintViolation && ($constraint = $violation->getConstraint()) !== null) {
try {
$code = $constraint->getErrorName($code);
} catch (InvalidArgumentException $e) {
// Ignore error
}
}
return self::message($message, $code);
}
/**
* Add recursively a prefix field on children
*
* @param HttpFieldPath $prefix Prefix to add
*
* @return FormError
*/
private function withPrefixField(HttpFieldPath $prefix)
{
$error = clone $this;
$error->field = $this->field ? $prefix->concat($this->field) : $prefix;
$error->children = [];
foreach ($this->children as $name => $child) {
$error->children[$name] = $child->withPrefixField($prefix);
}
return $error;
}
}