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

api-platform / core / 17487610263

05 Sep 2025 08:12AM UTC coverage: 22.608% (+0.3%) from 22.319%
17487610263

push

github

web-flow
chore: remove @experimental flag from parameters (#7357)

12049 of 53296 relevant lines covered (22.61%)

26.21 hits per line

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

100.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\AdvancedNameConverterInterface;
17
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
18
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20
use Symfony\Component\Validator\ConstraintViolation;
21
use Symfony\Component\Validator\ConstraintViolationListInterface;
22

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

34
    private readonly ?array $serializePayloadFields;
35

36
    public function __construct(?array $serializePayloadFields = null, private readonly ?NameConverterInterface $nameConverter = null)
37
    {
38
        $this->serializePayloadFields = null === $serializePayloadFields ? null : array_flip($serializePayloadFields);
662✔
39
    }
40

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

50
        return static::FORMAT === $format && $data instanceof ConstraintViolationListInterface;
68✔
51
    }
52

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

61
    protected function getMessagesAndViolations(ConstraintViolationListInterface $constraintViolationList): array
62
    {
63
        $violations = $messages = [];
72✔
64

65
        foreach ($constraintViolationList as $violation) {
72✔
66
            $class = \is_object($root = $violation->getRoot()) ? $root::class : null;
72✔
67

68
            if ($this->nameConverter instanceof AdvancedNameConverterInterface || $this->nameConverter instanceof MetadataAwareNameConverter) {
72✔
69
                $propertyPath = $this->nameConverter->normalize($violation->getPropertyPath(), $class, static::FORMAT);
68✔
70
            } elseif ($this->nameConverter instanceof NameConverterInterface) {
4✔
71
                $propertyPath = $this->nameConverter->normalize($violation->getPropertyPath());
2✔
72
            } else {
73
                $propertyPath = $violation->getPropertyPath();
2✔
74
            }
75

76
            $violationData = [
72✔
77
                'propertyPath' => $propertyPath,
72✔
78
                'message' => $violation->getMessage(),
72✔
79
                'code' => $violation->getCode(),
72✔
80
            ];
72✔
81

82
            if ($hint = $violation->getParameters()['hint'] ?? false) {
72✔
83
                $violationData['hint'] = $hint;
2✔
84
            }
85

86
            $constraint = $violation instanceof ConstraintViolation ? $violation->getConstraint() : null;
72✔
87
            if (
88
                [] !== $this->serializePayloadFields
72✔
89
                && $constraint
90
                && $constraint->payload
72✔
91
                // If some fields are whitelisted, only them are added
92
                && $payloadFields = null === $this->serializePayloadFields ? $constraint->payload : array_intersect_key($constraint->payload, $this->serializePayloadFields)
72✔
93
            ) {
94
                $violationData['payload'] = $payloadFields;
6✔
95
            }
96

97
            $violations[] = $violationData;
72✔
98
            $messages[] = ($violationData['propertyPath'] ? "{$violationData['propertyPath']}: " : '').$violationData['message'];
72✔
99
        }
100

101
        return [$messages, $violations];
72✔
102
    }
103
}
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