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

api-platform / core / 10814494863

11 Sep 2024 03:11PM UTC coverage: 7.008% (-0.7%) from 7.679%
10814494863

push

github

web-flow
fix(state): remove resource_class change (#6607)

11516 of 164321 relevant lines covered (7.01%)

22.85 hits per line

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

82.14
/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);
2,259✔
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)) {
56✔
47
            return false;
×
48
        }
49

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

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

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

62
        foreach ($constraintViolationList as $violation) {
56✔
63
            $class = \is_object($root = $violation->getRoot()) ? $root::class : null;
53✔
64

65
            if ($this->nameConverter instanceof AdvancedNameConverterInterface || $this->nameConverter instanceof MetadataAwareNameConverter) {
53✔
66
                $propertyPath = $this->nameConverter->normalize($violation->getPropertyPath(), $class, static::FORMAT);
53✔
67
            } elseif ($this->nameConverter instanceof NameConverterInterface) {
×
68
                $propertyPath = $this->nameConverter->normalize($violation->getPropertyPath());
×
69
            } else {
70
                $propertyPath = $violation->getPropertyPath();
×
71
            }
72

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

79
            if ($hint = $violation->getParameters()['hint'] ?? false) {
53✔
80
                $violationData['hint'] = $hint;
1✔
81
            }
82

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

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

98
        return [$messages, $violations];
56✔
99
    }
100
}
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