1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Model; |
12
|
|
|
|
13
|
|
|
use Ynlo\GraphQLBundle\Annotation as GraphQL; |
14
|
|
|
use Ynlo\GraphQLBundle\Util\IDEncoder; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @GraphQL\ObjectType(description="A violation of a constraint that happened during validation. |
18
|
|
|
|
19
|
|
|
For each constraint that fails during validation one or more violations are |
20
|
|
|
created. The violations store the violation message, the path to the failing |
21
|
|
|
element in the validation graph and other helpful information, |
22
|
|
|
like template and arguments to use with a translation engine") |
23
|
|
|
*/ |
24
|
|
|
class ConstraintViolation |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
* |
29
|
|
|
* @GraphQL\Field(type="string", |
30
|
|
|
* description="Returns the property path from the root element to the violation.") |
31
|
|
|
*/ |
32
|
|
|
protected $propertyPath; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
* |
37
|
|
|
* @GraphQL\Field(type="string!", |
38
|
|
|
* description="Returns the violation message.") |
39
|
|
|
*/ |
40
|
|
|
protected $message; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
* |
45
|
|
|
* @GraphQL\Field(type="string!", description="Returns the raw violation message. |
46
|
|
|
The raw violation message contains placeholders for the parameters returned by parameters. |
47
|
|
|
Typically you'll pass the message template and parameters to a translation engine.") |
48
|
|
|
*/ |
49
|
|
|
protected $messageTemplate; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var ConstraintViolationParameter[] |
53
|
|
|
* |
54
|
|
|
* @GraphQL\Field(type="[Ynlo\GraphQLBundle\Model\ConstraintViolationParameter]", |
55
|
|
|
* description="Returns the parameters to be inserted into the raw violation message.") |
56
|
|
|
*/ |
57
|
|
|
protected $parameters; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var int |
61
|
|
|
* |
62
|
|
|
* @GraphQL\Field(type="int", |
63
|
|
|
* description="Returns a number for pluralizing the violation message") |
64
|
|
|
*/ |
65
|
|
|
protected $plural = 0; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var string |
69
|
|
|
* |
70
|
|
|
* @GraphQL\Field(type="string", |
71
|
|
|
* description="Returns the value that caused the violation.") |
72
|
|
|
*/ |
73
|
|
|
protected $invalidValue; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var string |
77
|
|
|
* |
78
|
|
|
* @GraphQL\Field(type="string!", |
79
|
|
|
* description="Returns a machine-digestible error code for the violation.") |
80
|
|
|
*/ |
81
|
|
|
protected $code; |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getMessage(): string |
88
|
|
|
{ |
89
|
|
|
return $this->message; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $message |
94
|
|
|
* |
95
|
|
|
* @return ConstraintViolation |
96
|
|
|
*/ |
97
|
|
|
public function setMessage(string $message): ConstraintViolation |
98
|
|
|
{ |
99
|
|
|
$this->message = $message; |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getPropertyPath(): ?string |
108
|
|
|
{ |
109
|
|
|
return $this->propertyPath; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $path |
114
|
|
|
* |
115
|
|
|
* @return ConstraintViolation |
116
|
|
|
*/ |
117
|
|
|
public function setPropertyPath(?string $path): ConstraintViolation |
118
|
|
|
{ |
119
|
|
|
$this->propertyPath = $path; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return mixed |
126
|
|
|
*/ |
127
|
|
|
public function getInvalidValue() |
128
|
|
|
{ |
129
|
|
|
return $this->invalidValue; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param mixed $invalidValue |
134
|
|
|
* |
135
|
|
|
* @return ConstraintViolation |
136
|
|
|
*/ |
137
|
|
|
public function setInvalidValue($invalidValue): ConstraintViolation |
138
|
|
|
{ |
139
|
|
|
$this->invalidValue = $this->normalizeValue($invalidValue); |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return ConstraintViolationParameter[] |
146
|
|
|
*/ |
147
|
|
|
public function getParameters(): array |
148
|
|
|
{ |
149
|
|
|
return $this->parameters; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $name |
154
|
|
|
* @param mixed $value |
155
|
|
|
* |
156
|
|
|
* @return ConstraintViolation |
157
|
|
|
*/ |
158
|
|
|
public function addParameter($name, $value): ConstraintViolation |
159
|
|
|
{ |
160
|
|
|
$this->parameters[] = new ConstraintViolationParameter($name, $this->normalizeValue($value)); |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
|
|
public function getMessageTemplate(): string |
169
|
|
|
{ |
170
|
|
|
return $this->messageTemplate; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param string $messageTemplate |
175
|
|
|
* |
176
|
|
|
* @return ConstraintViolation |
177
|
|
|
*/ |
178
|
|
|
public function setMessageTemplate(string $messageTemplate): ConstraintViolation |
179
|
|
|
{ |
180
|
|
|
$this->messageTemplate = $messageTemplate; |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return int |
187
|
|
|
*/ |
188
|
|
|
public function getPlural(): ?int |
189
|
|
|
{ |
190
|
|
|
return $this->plural; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param int $plural |
195
|
|
|
* |
196
|
|
|
* @return ConstraintViolation |
197
|
|
|
*/ |
198
|
|
|
public function setPlural(?int $plural): ConstraintViolation |
199
|
|
|
{ |
200
|
|
|
$this->plural = $plural; |
201
|
|
|
|
202
|
|
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
|
|
public function getCode(): string |
209
|
|
|
{ |
210
|
|
|
return $this->code; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param string $code |
215
|
|
|
* |
216
|
|
|
* @return ConstraintViolation |
217
|
|
|
*/ |
218
|
|
|
public function setCode(string $code): ConstraintViolation |
219
|
|
|
{ |
220
|
|
|
$this->code = $code; |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return array |
227
|
|
|
*/ |
228
|
|
|
public function toArray() |
229
|
|
|
{ |
230
|
|
|
$params = []; |
231
|
|
|
foreach ($this->getParameters() as $parameter) { |
232
|
|
|
$params[$parameter->getName()] = $parameter->getValue(); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return [ |
236
|
|
|
'code' => $this->getCode(), |
237
|
|
|
'message' => $this->getMessage(), |
238
|
|
|
'messageTemplate' => $this->getMessageTemplate(), |
239
|
|
|
'propertyPath' => $this->getPropertyPath(), |
240
|
|
|
'parameters' => $params, |
241
|
|
|
'invalidValue' => $this->getInvalidValue(), |
242
|
|
|
'plural' => $this->getPlural(), |
243
|
|
|
]; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param mixed $value |
248
|
|
|
* |
249
|
|
|
* @return null|string|mixed |
250
|
|
|
*/ |
251
|
|
|
private function normalizeValue($value) |
252
|
|
|
{ |
253
|
|
|
if ($value instanceof NodeInterface) { |
254
|
|
|
$value = IDEncoder::encode($value); |
255
|
|
|
} elseif (\is_object($value)) { |
256
|
|
|
if (method_exists($value, '__toString')) { |
257
|
|
|
$value = (string) $value; |
258
|
|
|
} else { |
259
|
|
|
$value = null; |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return $value; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|