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

api-platform / core / 16050929464

03 Jul 2025 12:51PM UTC coverage: 22.065% (+0.2%) from 21.821%
16050929464

push

github

soyuka
chore: todo improvement

11516 of 52192 relevant lines covered (22.06%)

22.08 hits per line

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

6.67
/src/JsonApi/Serializer/ConstraintViolationListNormalizer.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\JsonApi\Serializer;
15

16
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
17
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
18
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20
use Symfony\Component\TypeInfo\Type\ObjectType;
21
use Symfony\Component\Validator\ConstraintViolationInterface;
22
use Symfony\Component\Validator\ConstraintViolationListInterface;
23

24
/**
25
 * Converts {@see ConstraintViolationListInterface} to a JSON API error representation.
26
 *
27
 * @author Héctor Hurtarte <hectorh30@gmail.com>
28
 */
29
final class ConstraintViolationListNormalizer implements NormalizerInterface
30
{
31
    public const FORMAT = 'jsonapi';
32

33
    public function __construct(private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly ?NameConverterInterface $nameConverter = null)
34
    {
35
    }
520✔
36

37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function normalize(mixed $object, ?string $format = null, array $context = []): array
41
    {
42
        $violations = [];
×
43
        foreach ($object as $violation) {
×
44
            $violations[] = [
×
45
                'detail' => $violation->getMessage(),
×
46
                'source' => [
×
47
                    'pointer' => $this->getSourcePointerFromViolation($violation),
×
48
                ],
×
49
            ];
×
50
        }
51

52
        return ['errors' => $violations];
×
53
    }
54

55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
59
    {
60
        return self::FORMAT === $format && $data instanceof ConstraintViolationListInterface;
×
61
    }
62

63
    public function getSupportedTypes($format): array
64
    {
65
        return self::FORMAT === $format ? [ConstraintViolationListInterface::class => true] : [];
484✔
66
    }
67

68
    private function getSourcePointerFromViolation(ConstraintViolationInterface $violation): string
69
    {
70
        $fieldName = $violation->getPropertyPath();
×
71

72
        if (!$fieldName) {
×
73
            return 'data';
×
74
        }
75

76
        $class = $violation->getRoot()::class;
×
77
        $propertyMetadata = $this->propertyMetadataFactory
×
78
            ->create(
×
79
                // Im quite sure this requires some thought in case of validations over relationships
80
                $class,
×
81
                $fieldName
×
82
            );
×
83

84
        if (null !== $this->nameConverter) {
×
85
            $fieldName = $this->nameConverter->normalize($fieldName, $class, self::FORMAT);
×
86
        }
87

88
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
89
            $type = $propertyMetadata->getBuiltinTypes()[0] ?? null;
×
90
            if ($type && null !== $type->getClassName()) {
×
91
                return "data/relationships/$fieldName";
×
92
            }
93
        } else {
94
            if ($propertyMetadata->getNativeType()?->isSatisfiedBy(fn ($t) => $t instanceof ObjectType)) {
×
95
                return "data/relationships/$fieldName";
×
96
            }
97
        }
98

99
        return "data/attributes/$fieldName";
×
100
    }
101
}
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