-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldError.php
More file actions
124 lines (106 loc) · 3.35 KB
/
Copy pathFieldError.php
File metadata and controls
124 lines (106 loc) · 3.35 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
<?php
namespace Quatrevieux\Form\Validator;
use JsonSerializable;
use Quatrevieux\Form\DummyTranslator;
use Quatrevieux\Form\Validator\Constraint\ConstraintInterface;
use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Store error for a field
* This class can be serialized to JSON
*/
final class FieldError implements JsonSerializable, TranslatableInterface
{
public function __construct(
/**
* Error message
*
* Placeholders can be used, with the following format: `{{ placeholder }}`
* and values can be passed in $parameters constructor argument
*/
public readonly string $message,
/**
* Parameters to replace placeholders in $message
* Takes as key the placeholder name and as value the value to replace
*
* @var array<string, mixed>
*/
public readonly array $parameters = [],
/**
* Error code used to identify the error ignoring localized or parameterized messages
*/
public readonly string $code = ConstraintInterface::CODE,
/**
* Translator to use to translate the message and replace placeholders
* If null, a DummyTranslator will be used
*/
private ?TranslatorInterface $translator = null,
) {}
/**
* Set the translator instance
* This method is called by the validator
*
* @return self A new instance with the translator set
*
* @internal
*/
public function withTranslator(?TranslatorInterface $translator): self
{
if ($this->translator === $translator) {
return $this;
}
$error = clone $this;
$error->translator = $translator;
return $error;
}
/**
* {@inheritdoc}
*
* @return array{code: string, message: string, parameters?: array<string, mixed>}
*/
public function jsonSerialize(): array
{
$json = [
'code' => $this->code,
'message' => (string) $this,
];
if ($this->parameters) {
$json['parameters'] = $this->parameters;
}
return $json;
}
/**
* Translates the error message to the given locale
*/
public function trans(TranslatorInterface $translator, ?string $locale = null): string
{
// Normalize placeholders parameters
if ($parameters = $this->parameters) {
$normalized = [];
foreach ($parameters as $key => $value) {
$normalized['{{ ' . $key . ' }}'] = $value;
}
$parameters = $normalized;
}
return $translator->trans($this->message, $parameters, null, $locale);
}
/**
* Get the translated error message
* All placeholders will be replaced by their value
*
* @param string|null $locale Locale to translate the message to
* @return string Translated error message
*/
public function localizedMessage(?string $locale = null): string
{
return $this->trans($this->translator ?? DummyTranslator::instance(), $locale);
}
/**
* Get the translated error message
* All placeholders will be replaced by their value
*/
public function __toString(): string
{
return $this->localizedMessage();
}
}