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

api-platform / core / 9562658349

18 Jun 2024 09:35AM UTC coverage: 62.637% (+0.4%) from 62.272%
9562658349

push

github

soyuka
Merge 3.4

52 of 55 new or added lines in 6 files covered. (94.55%)

236 existing lines in 20 files now uncovered.

11016 of 17587 relevant lines covered (62.64%)

60.45 hits per line

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

75.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\Serializer\Serializer;
21
use Symfony\Component\Validator\ConstraintViolation;
22
use Symfony\Component\Validator\ConstraintViolationListInterface;
23

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

35
    private readonly ?array $serializePayloadFields;
36

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

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

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

54
    public function getSupportedTypes($format): array
55
    {
56
        return $format === static::FORMAT ? [ConstraintViolationListInterface::class => true] : [];
336✔
57
    }
58

59
    public function hasCacheableSupportsMethod(): bool
60
    {
61
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
62
            trigger_deprecation(
×
63
                'api-platform/core',
×
64
                '3.1',
×
65
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
66
                __METHOD__
×
UNCOV
67
            );
×
68
        }
69

UNCOV
70
        return true;
×
71
    }
72

73
    protected function getMessagesAndViolations(ConstraintViolationListInterface $constraintViolationList): array
74
    {
75
        $violations = $messages = [];
48✔
76

77
        foreach ($constraintViolationList as $violation) {
48✔
78
            $class = \is_object($root = $violation->getRoot()) ? $root::class : null;
48✔
79

80
            if ($this->nameConverter instanceof AdvancedNameConverterInterface || $this->nameConverter instanceof MetadataAwareNameConverter) {
48✔
81
                $propertyPath = $this->nameConverter->normalize($violation->getPropertyPath(), $class, static::FORMAT);
16✔
82
            } elseif ($this->nameConverter instanceof NameConverterInterface) {
32✔
83
                $propertyPath = $this->nameConverter->normalize($violation->getPropertyPath());
16✔
84
            } else {
85
                $propertyPath = $violation->getPropertyPath();
16✔
86
            }
87

88
            $violationData = [
48✔
89
                'propertyPath' => $propertyPath,
48✔
90
                'message' => $violation->getMessage(),
48✔
91
                'code' => $violation->getCode(),
48✔
92
            ];
48✔
93

94
            if ($hint = $violation->getParameters()['hint'] ?? false) {
48✔
UNCOV
95
                $violationData['hint'] = $hint;
×
96
            }
97

98
            $constraint = $violation instanceof ConstraintViolation ? $violation->getConstraint() : null;
48✔
99
            if (
100
                [] !== $this->serializePayloadFields
48✔
101
                && $constraint
102
                && $constraint->payload
48✔
103
                // If some fields are whitelisted, only them are added
104
                && $payloadFields = null === $this->serializePayloadFields ? $constraint->payload : array_intersect_key($constraint->payload, $this->serializePayloadFields)
48✔
105
            ) {
106
                $violationData['payload'] = $payloadFields;
36✔
107
            }
108

109
            $violations[] = $violationData;
48✔
110
            $messages[] = ($violationData['propertyPath'] ? "{$violationData['propertyPath']}: " : '').$violationData['message'];
48✔
111
        }
112

113
        return [$messages, $violations];
48✔
114
    }
115
}
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