This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstraintViolationExceptionNormalizer.php
89 lines (75 loc) · 3.18 KB
/
ConstraintViolationExceptionNormalizer.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
<?php
// Copyright (c) Fusonic GmbH. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license information.
declare(strict_types=1);
namespace Fusonic\HttpKernelExtensions\Normalizer;
use Fusonic\HttpKernelExtensions\Exception\ConstraintViolationException;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Validator\ConstraintViolation;
/**
* A normalizer for {@see ConstraintViolationException}.
* It uses the Symfony ConstraintViolationListNormalizer {@see \Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer}
* and enhances it with an error name and the message template.
*/
final class ConstraintViolationExceptionNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
{
public function __construct(
private readonly NormalizerInterface $normalizer
) {
}
/**
* @param array<mixed> $context
*/
public function normalize(mixed $object, ?string $format = null, array $context = []): array
{
/** @var ConstraintViolationException $exception */
$exception = $object;
$constraintViolationList = $exception->getConstraintViolationList();
/** @var array<string, mixed> $normalized */
$normalized = $this->normalizer->normalize($constraintViolationList, $format, $context);
if (!isset($normalized['violations'])) {
return $normalized;
}
foreach ($normalized['violations'] as $index => $normalizedViolation) {
/** @var ConstraintViolation $violation */
foreach ($constraintViolationList as $violation) {
if (
isset($normalizedViolation['title'])
&& $violation->getMessage() === $normalizedViolation['title']
) {
$normalized['violations'][$index]['messageTemplate'] = $violation->getMessageTemplate();
$constraint = $violation->getConstraint();
$code = $violation->getCode();
if (null !== $constraint && null !== $code) {
/** @var class-string<\Symfony\Component\Validator\Constraint> $constraintClass */
$constraintClass = $constraint::class;
$normalized['violations'][$index]['errorName'] = $constraintClass::getErrorName($code);
}
}
}
}
return $normalized;
}
/**
* @param array<mixed> $context
*/
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return $data instanceof ConstraintViolationException
&& $this->normalizer->supportsNormalization($data->getConstraintViolationList(), $format);
}
public function hasCacheableSupportsMethod(): bool
{
return true;
}
/**
* @return array<class-string, bool>
*/
public function getSupportedTypes(?string $format): array
{
return [
ConstraintViolationException::class => true,
];
}
}