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

api-platform / core / 9710726294

25 Jun 2024 02:01PM UTC coverage: 62.122% (-0.5%) from 62.637%
9710726294

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.

10862 of 17485 relevant lines covered (62.12%)

59.64 hits per line

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

21.21
/src/JsonApi/Serializer/ErrorNormalizer.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\Serializer\CacheableSupportsMethodInterface;
17
use ApiPlatform\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface as LegacyConstraintViolationListAwareExceptionInterface;
18
use ApiPlatform\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
19
use Symfony\Component\ErrorHandler\Exception\FlattenException;
20
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
21
use Symfony\Component\Serializer\Serializer;
22

23
/**
24
 * Converts {@see \Exception} or {@see FlattenException} or to a JSON API error representation.
25
 *
26
 * @author Héctor Hurtarte <hectorh30@gmail.com>
27
 */
28
final class ErrorNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
29
{
30
    use ErrorNormalizerTrait;
31

32
    public const FORMAT = 'jsonapi';
33
    public const TITLE = 'title';
34
    private array $defaultContext = [
35
        self::TITLE => 'An error occurred',
36
    ];
37

38
    public function __construct(private readonly bool $debug = false, array $defaultContext = [], private ?NormalizerInterface $itemNormalizer = null, private ?NormalizerInterface $constraintViolationListNormalizer = null)
39
    {
40
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
352✔
41
    }
42

43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function normalize(mixed $object, ?string $format = null, array $context = []): array
47
    {
48
        // TODO: in api platform 4 this will be the default, note that JSON:API is close to Problem so we should use the same normalizer
UNCOV
49
        if ($context['rfc_7807_compliant_errors'] ?? false) {
×
50
            if ($object instanceof LegacyConstraintViolationListAwareExceptionInterface || $object instanceof ConstraintViolationListAwareExceptionInterface) {
×
51
                // TODO: return ['errors' => $this->constraintViolationListNormalizer(...)]
52
                return $this->constraintViolationListNormalizer->normalize($object->getConstraintViolationList(), $format, $context);
×
53
            }
54

55
            $jsonApiObject = $this->itemNormalizer->normalize($object, $format, $context);
×
56
            $error = $jsonApiObject['data']['attributes'];
×
57
            $error['id'] = $jsonApiObject['data']['id'];
×
58
            $error['type'] = $jsonApiObject['data']['id'];
×
59

60
            return ['errors' => [$error]];
×
61
        }
62

UNCOV
63
        $data = [
×
UNCOV
64
            'title' => $context[self::TITLE] ?? $this->defaultContext[self::TITLE],
×
UNCOV
65
            'description' => $this->getErrorMessage($object, $context, $this->debug),
×
UNCOV
66
        ];
×
67

UNCOV
68
        if (null !== $errorCode = $this->getErrorCode($object)) {
×
UNCOV
69
            $data['code'] = $errorCode;
×
70
        }
71

UNCOV
72
        if ($this->debug && null !== $trace = $object->getTrace()) {
×
UNCOV
73
            $data['trace'] = $trace;
×
74
        }
75

UNCOV
76
        return $data;
×
77
    }
78

79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
83
    {
UNCOV
84
        return self::FORMAT === $format && ($data instanceof \Exception || $data instanceof FlattenException);
×
85
    }
86

87
    public function getSupportedTypes($format): array
88
    {
89
        if (self::FORMAT === $format) {
328✔
90
            return [
56✔
91
                \Exception::class => true,
56✔
92
                FlattenException::class => true,
56✔
93
            ];
56✔
94
        }
95

96
        return [];
272✔
97
    }
98

99
    public function hasCacheableSupportsMethod(): bool
100
    {
101
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
102
            trigger_deprecation(
×
103
                'api-platform/core',
×
104
                '3.1',
×
105
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
106
                __METHOD__
×
107
            );
×
108
        }
109

110
        return true;
×
111
    }
112
}
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