|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* It's free open-source software released under the MIT License. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Anatoly Nekhay <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2021, Anatoly Nekhay |
|
8
|
|
|
* @license https://github.com/sunrise-php/hydrator/blob/master/LICENSE |
|
9
|
|
|
* @link https://github.com/sunrise-php/hydrator |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Sunrise\Hydrator\Exception; |
|
15
|
|
|
|
|
16
|
|
|
use RuntimeException; |
|
17
|
|
|
use Sunrise\Hydrator\Dictionary\ErrorCode; |
|
18
|
|
|
use Sunrise\Hydrator\Dictionary\ErrorMessage; |
|
19
|
|
|
use Sunrise\Hydrator\Dictionary\TranslationDomain; |
|
20
|
|
|
use Symfony\Component\Validator\ConstraintViolation; |
|
21
|
|
|
use Symfony\Component\Validator\ConstraintViolationInterface; |
|
22
|
|
|
|
|
23
|
|
|
use function join; |
|
24
|
|
|
use function strtr; |
|
25
|
|
|
|
|
26
|
|
|
class InvalidValueException extends RuntimeException implements ExceptionInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @see ErrorCode |
|
30
|
|
|
*/ |
|
31
|
|
|
private string $errorCode; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var list<array-key> |
|
|
|
|
|
|
35
|
|
|
*/ |
|
36
|
|
|
private array $propertyPath; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @see ErrorMessage |
|
40
|
|
|
*/ |
|
41
|
|
|
private string $messageTemplate; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var array<string, int|float|string> |
|
45
|
|
|
*/ |
|
46
|
|
|
private array $messagePlaceholders; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var mixed |
|
50
|
|
|
*/ |
|
51
|
|
|
private $invalidValue; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @see TranslationDomain |
|
55
|
|
|
*/ |
|
56
|
|
|
private string $translationDomain; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param list<array-key> $propertyPath |
|
60
|
|
|
* @param array<string, int|float|string> $messagePlaceholders |
|
61
|
|
|
* @param mixed $invalidValue |
|
62
|
|
|
*/ |
|
63
|
247 |
|
public function __construct( |
|
64
|
|
|
string $message, |
|
65
|
|
|
string $errorCode, |
|
66
|
|
|
array $propertyPath, |
|
67
|
|
|
string $messageTemplate, |
|
68
|
|
|
array $messagePlaceholders, |
|
69
|
|
|
$invalidValue = null, |
|
70
|
|
|
string $translationDomain = TranslationDomain::HYDRATOR |
|
71
|
|
|
) { |
|
72
|
247 |
|
parent::__construct($message); |
|
73
|
|
|
|
|
74
|
247 |
|
$this->errorCode = $errorCode; |
|
75
|
247 |
|
$this->propertyPath = $propertyPath; |
|
|
|
|
|
|
76
|
247 |
|
$this->messageTemplate = $messageTemplate; |
|
77
|
247 |
|
$this->messagePlaceholders = $messagePlaceholders; |
|
78
|
247 |
|
$this->invalidValue = $invalidValue; |
|
79
|
247 |
|
$this->translationDomain = $translationDomain; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
247 |
|
final public function getPropertyPath(): string |
|
83
|
|
|
{ |
|
84
|
247 |
|
return join('.', $this->propertyPath); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @since 3.0.0 |
|
89
|
|
|
*/ |
|
90
|
246 |
|
final public function getErrorCode(): string |
|
91
|
|
|
{ |
|
92
|
246 |
|
return $this->errorCode; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @since 3.7.0 |
|
97
|
|
|
*/ |
|
98
|
1 |
|
final public function getMessageTemplate(): string |
|
99
|
|
|
{ |
|
100
|
1 |
|
return $this->messageTemplate; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return array<string, int|float|string> |
|
105
|
|
|
* |
|
106
|
|
|
* @since 3.7.0 |
|
107
|
|
|
*/ |
|
108
|
1 |
|
final public function getMessagePlaceholders(): array |
|
109
|
|
|
{ |
|
110
|
1 |
|
return $this->messagePlaceholders; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return mixed |
|
115
|
|
|
* |
|
116
|
|
|
* @since 3.13.0 |
|
117
|
|
|
*/ |
|
118
|
1 |
|
final public function getInvalidValue() |
|
119
|
|
|
{ |
|
120
|
1 |
|
return $this->invalidValue; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @since 3.18.0 |
|
125
|
|
|
*/ |
|
126
|
33 |
|
final public function getTranslationDomain(): string |
|
127
|
|
|
{ |
|
128
|
33 |
|
return $this->translationDomain; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @since 3.9.0 |
|
133
|
|
|
*/ |
|
134
|
1 |
|
final public function getViolation(): ConstraintViolationInterface |
|
135
|
|
|
{ |
|
136
|
1 |
|
return new ConstraintViolation( |
|
137
|
1 |
|
$this->getMessage(), |
|
138
|
1 |
|
$this->getMessageTemplate(), |
|
139
|
1 |
|
$this->getMessagePlaceholders(), |
|
140
|
1 |
|
null, |
|
141
|
1 |
|
$this->getPropertyPath(), |
|
142
|
1 |
|
$this->getInvalidValue(), |
|
143
|
1 |
|
null, |
|
144
|
1 |
|
$this->getErrorCode(), |
|
145
|
1 |
|
); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param list<array-key> $propertyPath |
|
150
|
|
|
* |
|
151
|
|
|
* @since 3.0.0 |
|
152
|
|
|
*/ |
|
153
|
20 |
|
final public static function mustBeProvided(array $propertyPath): self |
|
154
|
|
|
{ |
|
155
|
20 |
|
return new self( |
|
156
|
20 |
|
ErrorMessage::MUST_BE_PROVIDED, |
|
157
|
20 |
|
ErrorCode::MUST_BE_PROVIDED, |
|
158
|
20 |
|
$propertyPath, |
|
159
|
20 |
|
ErrorMessage::MUST_BE_PROVIDED, |
|
160
|
20 |
|
[], |
|
161
|
20 |
|
); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param list<array-key> $propertyPath |
|
166
|
|
|
* @param mixed $invalidValue |
|
167
|
|
|
* |
|
168
|
|
|
* @since 3.0.0 |
|
169
|
|
|
*/ |
|
170
|
51 |
|
final public static function mustNotBeEmpty(array $propertyPath, $invalidValue = null): self |
|
171
|
|
|
{ |
|
172
|
51 |
|
return new self( |
|
173
|
51 |
|
ErrorMessage::MUST_NOT_BE_EMPTY, |
|
174
|
51 |
|
ErrorCode::MUST_NOT_BE_EMPTY, |
|
175
|
51 |
|
$propertyPath, |
|
176
|
51 |
|
ErrorMessage::MUST_NOT_BE_EMPTY, |
|
177
|
51 |
|
[], |
|
178
|
51 |
|
$invalidValue, |
|
179
|
51 |
|
); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param list<array-key> $propertyPath |
|
184
|
|
|
* @param mixed $invalidValue |
|
185
|
|
|
* |
|
186
|
|
|
* @since 3.0.0 |
|
187
|
|
|
*/ |
|
188
|
43 |
|
final public static function mustBeBoolean(array $propertyPath, $invalidValue = null): self |
|
189
|
|
|
{ |
|
190
|
43 |
|
return new self( |
|
191
|
43 |
|
ErrorMessage::MUST_BE_BOOLEAN, |
|
192
|
43 |
|
ErrorCode::MUST_BE_BOOLEAN, |
|
193
|
43 |
|
$propertyPath, |
|
194
|
43 |
|
ErrorMessage::MUST_BE_BOOLEAN, |
|
195
|
43 |
|
[], |
|
196
|
43 |
|
$invalidValue, |
|
197
|
43 |
|
); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @param list<array-key> $propertyPath |
|
202
|
|
|
* @param mixed $invalidValue |
|
203
|
|
|
* |
|
204
|
|
|
* @since 3.0.0 |
|
205
|
|
|
*/ |
|
206
|
11 |
|
final public static function mustBeInteger(array $propertyPath, $invalidValue = null): self |
|
207
|
|
|
{ |
|
208
|
11 |
|
return new self( |
|
209
|
11 |
|
ErrorMessage::MUST_BE_INTEGER, |
|
210
|
11 |
|
ErrorCode::MUST_BE_INTEGER, |
|
211
|
11 |
|
$propertyPath, |
|
212
|
11 |
|
ErrorMessage::MUST_BE_INTEGER, |
|
213
|
11 |
|
[], |
|
214
|
11 |
|
$invalidValue, |
|
215
|
11 |
|
); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @param list<array-key> $propertyPath |
|
220
|
|
|
* @param mixed $invalidValue |
|
221
|
|
|
* |
|
222
|
|
|
* @since 3.0.0 |
|
223
|
|
|
*/ |
|
224
|
4 |
|
final public static function mustBeNumber(array $propertyPath, $invalidValue = null): self |
|
225
|
|
|
{ |
|
226
|
4 |
|
return new self( |
|
227
|
4 |
|
ErrorMessage::MUST_BE_NUMBER, |
|
228
|
4 |
|
ErrorCode::MUST_BE_NUMBER, |
|
229
|
4 |
|
$propertyPath, |
|
230
|
4 |
|
ErrorMessage::MUST_BE_NUMBER, |
|
231
|
4 |
|
[], |
|
232
|
4 |
|
$invalidValue, |
|
233
|
4 |
|
); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @param list<array-key> $propertyPath |
|
238
|
|
|
* @param mixed $invalidValue |
|
239
|
|
|
* |
|
240
|
|
|
* @since 3.0.0 |
|
241
|
|
|
*/ |
|
242
|
25 |
|
final public static function mustBeString(array $propertyPath, $invalidValue = null): self |
|
243
|
|
|
{ |
|
244
|
25 |
|
return new self( |
|
245
|
25 |
|
ErrorMessage::MUST_BE_STRING, |
|
246
|
25 |
|
ErrorCode::MUST_BE_STRING, |
|
247
|
25 |
|
$propertyPath, |
|
248
|
25 |
|
ErrorMessage::MUST_BE_STRING, |
|
249
|
25 |
|
[], |
|
250
|
25 |
|
$invalidValue, |
|
251
|
25 |
|
); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* @param list<array-key> $propertyPath |
|
256
|
|
|
* @param mixed $invalidValue |
|
257
|
|
|
* |
|
258
|
|
|
* @since 3.0.0 |
|
259
|
|
|
*/ |
|
260
|
35 |
|
final public static function mustBeArray(array $propertyPath, $invalidValue = null): self |
|
261
|
|
|
{ |
|
262
|
35 |
|
return new self( |
|
263
|
35 |
|
ErrorMessage::MUST_BE_ARRAY, |
|
264
|
35 |
|
ErrorCode::MUST_BE_ARRAY, |
|
265
|
35 |
|
$propertyPath, |
|
266
|
35 |
|
ErrorMessage::MUST_BE_ARRAY, |
|
267
|
35 |
|
[], |
|
268
|
35 |
|
$invalidValue, |
|
269
|
35 |
|
); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* @param list<array-key> $propertyPath |
|
274
|
|
|
* @param int<0, max> $maximumElements |
|
275
|
|
|
* @param mixed $invalidValue |
|
276
|
|
|
* |
|
277
|
|
|
* @since 3.0.0 |
|
278
|
|
|
*/ |
|
279
|
51 |
|
final public static function arrayOverflow(array $propertyPath, int $maximumElements, $invalidValue = null): self |
|
280
|
|
|
{ |
|
281
|
51 |
|
$placeholders = [ |
|
282
|
51 |
|
'{{ maximum_elements }}' => $maximumElements, |
|
283
|
51 |
|
]; |
|
284
|
|
|
|
|
285
|
51 |
|
return new self( |
|
286
|
51 |
|
strtr(ErrorMessage::ARRAY_OVERFLOW, $placeholders), |
|
287
|
51 |
|
ErrorCode::ARRAY_OVERFLOW, |
|
288
|
51 |
|
$propertyPath, |
|
289
|
51 |
|
ErrorMessage::ARRAY_OVERFLOW, |
|
290
|
51 |
|
$placeholders, |
|
291
|
51 |
|
$invalidValue, |
|
292
|
51 |
|
); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* @param list<array-key> $propertyPath |
|
297
|
|
|
* @param list<int|string> $expectedValues |
|
298
|
|
|
* @param mixed $invalidValue |
|
299
|
|
|
* |
|
300
|
|
|
* @since 3.0.0 |
|
301
|
|
|
*/ |
|
302
|
3 |
|
final public static function invalidChoice(array $propertyPath, array $expectedValues, $invalidValue = null): self |
|
303
|
|
|
{ |
|
304
|
3 |
|
$placeholders = [ |
|
305
|
3 |
|
'{{ expected_values }}' => join(', ', $expectedValues), |
|
306
|
3 |
|
]; |
|
307
|
|
|
|
|
308
|
3 |
|
return new self( |
|
309
|
3 |
|
strtr(ErrorMessage::INVALID_CHOICE, $placeholders), |
|
310
|
3 |
|
ErrorCode::INVALID_CHOICE, |
|
311
|
3 |
|
$propertyPath, |
|
312
|
3 |
|
ErrorMessage::INVALID_CHOICE, |
|
313
|
3 |
|
$placeholders, |
|
314
|
3 |
|
$invalidValue, |
|
315
|
3 |
|
); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* @param list<array-key> $propertyPath |
|
320
|
|
|
* @param mixed $invalidValue |
|
321
|
|
|
* |
|
322
|
|
|
* @since 3.0.0 |
|
323
|
|
|
*/ |
|
324
|
1 |
|
final public static function invalidTimestamp( |
|
325
|
|
|
array $propertyPath, |
|
326
|
|
|
string $expectedFormat, |
|
327
|
|
|
$invalidValue = null |
|
328
|
|
|
): self { |
|
329
|
1 |
|
$placeholders = [ |
|
330
|
1 |
|
'{{ expected_format }}' => $expectedFormat, |
|
331
|
1 |
|
]; |
|
332
|
|
|
|
|
333
|
1 |
|
return new self( |
|
334
|
1 |
|
strtr(ErrorMessage::INVALID_TIMESTAMP, $placeholders), |
|
335
|
1 |
|
ErrorCode::INVALID_TIMESTAMP, |
|
336
|
1 |
|
$propertyPath, |
|
337
|
1 |
|
ErrorMessage::INVALID_TIMESTAMP, |
|
338
|
1 |
|
$placeholders, |
|
339
|
1 |
|
$invalidValue, |
|
340
|
1 |
|
); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* @param list<array-key> $propertyPath |
|
345
|
|
|
* @param mixed $invalidValue |
|
346
|
|
|
* |
|
347
|
|
|
* @since 3.0.0 |
|
348
|
|
|
*/ |
|
349
|
1 |
|
final public static function invalidTimezone(array $propertyPath, $invalidValue = null): self |
|
350
|
|
|
{ |
|
351
|
1 |
|
return new self( |
|
352
|
1 |
|
ErrorMessage::INVALID_TIMEZONE, |
|
353
|
1 |
|
ErrorCode::INVALID_TIMEZONE, |
|
354
|
1 |
|
$propertyPath, |
|
355
|
1 |
|
ErrorMessage::INVALID_TIMEZONE, |
|
356
|
1 |
|
[], |
|
357
|
1 |
|
$invalidValue, |
|
358
|
1 |
|
); |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
/** |
|
362
|
|
|
* @param list<array-key> $propertyPath |
|
363
|
|
|
* @param mixed $invalidValue |
|
364
|
|
|
* |
|
365
|
|
|
* @since 3.0.0 |
|
366
|
|
|
*/ |
|
367
|
2 |
|
final public static function invalidUid(array $propertyPath, $invalidValue = null): self |
|
368
|
|
|
{ |
|
369
|
2 |
|
return new self( |
|
370
|
2 |
|
ErrorMessage::INVALID_UID, |
|
371
|
2 |
|
ErrorCode::INVALID_UID, |
|
372
|
2 |
|
$propertyPath, |
|
373
|
2 |
|
ErrorMessage::INVALID_UID, |
|
374
|
2 |
|
[], |
|
375
|
2 |
|
$invalidValue, |
|
376
|
2 |
|
); |
|
377
|
|
|
} |
|
378
|
|
|
} |
|
379
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths