• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 19799380020

30 Nov 2025 01:09PM UTC coverage: 0.0%. Remained the same
19799380020

push

github

soyuka
Merge 4.2

0 of 306 new or added lines in 35 files covered. (0.0%)

22 existing lines in 15 files now uncovered.

0 of 57173 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Serializer/AbstractConstraintViolationListNormalizer.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Serializer;
15

16
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
17
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
18
use Symfony\Component\Validator\ConstraintViolation;
19
use Symfony\Component\Validator\ConstraintViolationListInterface;
20

21
/**
22
 * Common features regarding Constraint Violation normalization.
23
 *
24
 * @author Kévin Dunglas <dunglas@gmail.com>
25
 *
26
 * @internal
27
 */
28
abstract class AbstractConstraintViolationListNormalizer implements NormalizerInterface
29
{
30
    public const FORMAT = null; // Must be overridden
31

32
    private readonly ?array $serializePayloadFields;
33

34
    public function __construct(?array $serializePayloadFields = null, private readonly ?NameConverterInterface $nameConverter = null)
35
    {
36
        $this->serializePayloadFields = null === $serializePayloadFields ? null : array_flip($serializePayloadFields);
×
37
    }
38

39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
43
    {
44
        if (!($context['api_error_resource'] ?? false)) {
×
45
            return false;
×
46
        }
47

48
        return static::FORMAT === $format && $data instanceof ConstraintViolationListInterface;
×
49
    }
50

51
    /**
52
     * @param string|null $format
53
     */
54
    public function getSupportedTypes($format): array
55
    {
56
        return $format === static::FORMAT ? [ConstraintViolationListInterface::class => true] : [];
×
57
    }
58

59
    protected function getMessagesAndViolations(ConstraintViolationListInterface $constraintViolationList): array
60
    {
61
        $violations = $messages = [];
×
62

63
        foreach ($constraintViolationList as $violation) {
×
64
            $class = \is_object($root = $violation->getRoot()) ? $root::class : null;
×
65

NEW
66
            if ($this->nameConverter) {
×
67
                $propertyPath = $this->nameConverter->normalize($violation->getPropertyPath(), $class, static::FORMAT);
×
68
            } else {
69
                $propertyPath = $violation->getPropertyPath();
×
70
            }
71

72
            $violationData = [
×
73
                'propertyPath' => $propertyPath,
×
74
                'message' => $violation->getMessage(),
×
75
                'code' => $violation->getCode(),
×
76
            ];
×
77

78
            if ($hint = $violation->getParameters()['hint'] ?? false) {
×
79
                $violationData['hint'] = $hint;
×
80
            }
81

82
            $constraint = $violation instanceof ConstraintViolation ? $violation->getConstraint() : null;
×
83
            if (
84
                [] !== $this->serializePayloadFields
×
85
                && $constraint
86
                && $constraint->payload
×
87
                // If some fields are whitelisted, only them are added
88
                && $payloadFields = null === $this->serializePayloadFields ? $constraint->payload : array_intersect_key($constraint->payload, $this->serializePayloadFields)
×
89
            ) {
90
                $violationData['payload'] = $payloadFields;
×
91
            }
92

93
            $violations[] = $violationData;
×
94
            $messages[] = ($violationData['propertyPath'] ? "{$violationData['propertyPath']}: " : '').$violationData['message'];
×
95
        }
96

97
        return [$messages, $violations];
×
98
    }
99
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc