|
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) 2018, Anatoly Nekhay |
|
8
|
|
|
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
|
9
|
|
|
* @link https://github.com/sunrise-php/http-router |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Sunrise\Http\Router\Validation\ConstraintViolation; |
|
15
|
|
|
|
|
16
|
|
|
use Sunrise\Http\Router\Dictionary\TranslationDomain; |
|
17
|
|
|
use Sunrise\Http\Router\Validation\ConstraintViolationInterface as RouterConstraintViolationInterface; |
|
18
|
|
|
use Symfony\Component\Validator\ConstraintViolationInterface as ValidatorConstraintViolationInterface; |
|
19
|
|
|
|
|
20
|
|
|
use function preg_replace; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @since 3.0.0 |
|
24
|
|
|
*/ |
|
25
|
|
|
final class ValidatorConstraintViolationAdapter implements RouterConstraintViolationInterface |
|
26
|
|
|
{ |
|
27
|
6 |
|
public function __construct( |
|
28
|
|
|
private readonly ValidatorConstraintViolationInterface $validatorConstraintViolation, |
|
29
|
|
|
private readonly ?string $translationDomain = null, |
|
30
|
|
|
) { |
|
31
|
6 |
|
} |
|
32
|
|
|
|
|
33
|
5 |
|
public static function create(ValidatorConstraintViolationInterface $validatorConstraintViolation): self |
|
34
|
|
|
{ |
|
35
|
5 |
|
return new self($validatorConstraintViolation); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
6 |
|
public function getMessage(): string |
|
39
|
|
|
{ |
|
40
|
6 |
|
return (string) $this->validatorConstraintViolation->getMessage(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
public function getMessageTemplate(): string |
|
44
|
|
|
{ |
|
45
|
1 |
|
return $this->validatorConstraintViolation->getMessageTemplate(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @inheritDoc |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function getMessagePlaceholders(): array |
|
52
|
|
|
{ |
|
53
|
|
|
/** @var array<string, mixed> */ |
|
54
|
1 |
|
return $this->validatorConstraintViolation->getParameters(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
6 |
|
public function getPropertyPath(): string |
|
58
|
|
|
{ |
|
59
|
6 |
|
return self::adaptPropertyPath($this->validatorConstraintViolation->getPropertyPath()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
6 |
|
public function getCode(): ?string |
|
63
|
|
|
{ |
|
64
|
6 |
|
return $this->validatorConstraintViolation->getCode(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
6 |
|
public function getInvalidValue(): mixed |
|
68
|
|
|
{ |
|
69
|
6 |
|
return $this->validatorConstraintViolation->getInvalidValue(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
6 |
|
private static function adaptPropertyPath(string $propertyPath): string |
|
73
|
|
|
{ |
|
74
|
6 |
|
return (string) preg_replace(['/\x5b([^\x5b\x5d]+)\x5d/', '/^\x2e/'], ['.$1'], $propertyPath); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
public function getTranslationDomain(): string |
|
78
|
|
|
{ |
|
79
|
1 |
|
return $this->translationDomain ?? TranslationDomain::VALIDATOR; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|