• 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

92.59
/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\Serializer\NameConverter\NameConverterInterface;
18
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
19
use Symfony\Component\Validator\ConstraintViolationInterface;
20
use Symfony\Component\Validator\ConstraintViolationListInterface;
21

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

31
    public function __construct(private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly ?NameConverterInterface $nameConverter = null)
32
    {
33
    }
2,259✔
34

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

50
        return ['errors' => $violations];
1✔
51
    }
52

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

61
    public function getSupportedTypes($format): array
62
    {
63
        return self::FORMAT === $format ? [ConstraintViolationListInterface::class => true] : [];
2,129✔
64
    }
65

66
    private function getSourcePointerFromViolation(ConstraintViolationInterface $violation): string
67
    {
68
        $fieldName = $violation->getPropertyPath();
1✔
69

70
        if (!$fieldName) {
1✔
71
            return 'data';
×
72
        }
73

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

82
        if (null !== $this->nameConverter) {
1✔
83
            $fieldName = $this->nameConverter->normalize($fieldName, $class, self::FORMAT);
1✔
84
        }
85

86
        $type = $propertyMetadata->getBuiltinTypes()[0] ?? null;
1✔
87
        if ($type && null !== $type->getClassName()) {
1✔
88
            return "data/relationships/$fieldName";
×
89
        }
90

91
        return "data/attributes/$fieldName";
1✔
92
    }
93
}
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