-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractConstraintViolationListNormalizer.php
100 lines (82 loc) · 3.73 KB
/
AbstractConstraintViolationListNormalizer.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
90
91
92
93
94
95
96
97
98
99
100
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Serializer;
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationListInterface;
/**
* Common features regarding Constraint Violation normalization.
*
* @author Kévin Dunglas <[email protected]>
*
* @internal
*/
abstract class AbstractConstraintViolationListNormalizer implements NormalizerInterface
{
public const FORMAT = null; // Must be overridden
private readonly ?array $serializePayloadFields;
public function __construct(?array $serializePayloadFields = null, private readonly ?NameConverterInterface $nameConverter = null)
{
$this->serializePayloadFields = null === $serializePayloadFields ? null : array_flip($serializePayloadFields);
}
/**
* {@inheritdoc}
*/
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
if (!($context['api_error_resource'] ?? false)) {
return false;
}
return static::FORMAT === $format && $data instanceof ConstraintViolationListInterface;
}
public function getSupportedTypes($format): array
{
return $format === static::FORMAT ? [ConstraintViolationListInterface::class => true] : [];
}
protected function getMessagesAndViolations(ConstraintViolationListInterface $constraintViolationList): array
{
$violations = $messages = [];
foreach ($constraintViolationList as $violation) {
$class = \is_object($root = $violation->getRoot()) ? $root::class : null;
if ($this->nameConverter instanceof AdvancedNameConverterInterface || $this->nameConverter instanceof MetadataAwareNameConverter) {
$propertyPath = $this->nameConverter->normalize($violation->getPropertyPath(), $class, static::FORMAT);
} elseif ($this->nameConverter instanceof NameConverterInterface) {
$propertyPath = $this->nameConverter->normalize($violation->getPropertyPath());
} else {
$propertyPath = $violation->getPropertyPath();
}
$violationData = [
'propertyPath' => $propertyPath,
'message' => $violation->getMessage(),
'code' => $violation->getCode(),
];
if ($hint = $violation->getParameters()['hint'] ?? false) {
$violationData['hint'] = $hint;
}
$constraint = $violation instanceof ConstraintViolation ? $violation->getConstraint() : null;
if (
[] !== $this->serializePayloadFields
&& $constraint
&& $constraint->payload
// If some fields are whitelisted, only them are added
&& $payloadFields = null === $this->serializePayloadFields ? $constraint->payload : array_intersect_key($constraint->payload, $this->serializePayloadFields)
) {
$violationData['payload'] = $payloadFields;
}
$violations[] = $violationData;
$messages[] = ($violationData['propertyPath'] ? "{$violationData['propertyPath']}: " : '').$violationData['message'];
}
return [$messages, $violations];
}
}