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

api-platform / core / 9710673650

25 Jun 2024 02:01PM UTC coverage: 61.693% (-0.9%) from 62.637%
9710673650

push

github

web-flow
feat(laravel): laravel component (#5882)

* feat(laravel): laravel component

* try to skip laravel

* feat(jsonapi): component

* feat(laravel): json api support (needs review)

* work on relations

* relations (needs toMany) + skolem + IRI to resource

* links handler

* ulid

* validation

* slug post

* remove deprecations

* move classes

* fix tests

* fix tests metadata

* phpstan

* missing class

* fix laravel tests

* fix stan

33 of 77 new or added lines in 20 files covered. (42.86%)

140 existing lines in 15 files now uncovered.

10787 of 17485 relevant lines covered (61.69%)

59.64 hits per line

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

5.71
/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 ApiPlatform\Serializer\CacheableSupportsMethodInterface;
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\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, CacheableSupportsMethodInterface
30
{
31
    public const FORMAT = 'jsonapi';
32

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

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

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

55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
59
    {
UNCOV
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] : [];
328✔
66
    }
67

68
    public function hasCacheableSupportsMethod(): bool
69
    {
70
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
71
            trigger_deprecation(
×
72
                'api-platform/core',
×
73
                '3.1',
×
74
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
75
                __METHOD__
×
76
            );
×
77
        }
78

79
        return true;
×
80
    }
81

82
    private function getSourcePointerFromViolation(ConstraintViolationInterface $violation): string
83
    {
UNCOV
84
        $fieldName = $violation->getPropertyPath();
×
85

UNCOV
86
        if (!$fieldName) {
×
UNCOV
87
            return 'data';
×
88
        }
89

UNCOV
90
        $class = $violation->getRoot()::class;
×
UNCOV
91
        $propertyMetadata = $this->propertyMetadataFactory
×
UNCOV
92
            ->create(
×
93
                // Im quite sure this requires some thought in case of validations over relationships
UNCOV
94
                $class,
×
UNCOV
95
                $fieldName
×
UNCOV
96
            );
×
97

UNCOV
98
        if (null !== $this->nameConverter) {
×
UNCOV
99
            $fieldName = $this->nameConverter->normalize($fieldName, $class, self::FORMAT);
×
100
        }
101

UNCOV
102
        $type = $propertyMetadata->getBuiltinTypes()[0] ?? null;
×
UNCOV
103
        if ($type && null !== $type->getClassName()) {
×
UNCOV
104
            return "data/relationships/$fieldName";
×
105
        }
106

UNCOV
107
        return "data/attributes/$fieldName";
×
108
    }
109
}
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