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

api-platform / core / 6665644319

27 Oct 2023 09:57AM UTC coverage: 37.409% (+0.1%) from 37.305%
6665644319

push

github

soyuka
Merge 3.2

125 of 125 new or added lines in 32 files covered. (100.0%)

10319 of 27584 relevant lines covered (37.41%)

20.57 hits per line

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

23.33
/src/Hydra/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\Hydra\Serializer;
15

16
use ApiPlatform\Api\UrlGeneratorInterface as LegacyUrlGeneratorInterface;
17
use ApiPlatform\Metadata\UrlGeneratorInterface;
18
use ApiPlatform\Problem\Serializer\ErrorNormalizerTrait;
19
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
20
use Symfony\Component\ErrorHandler\Exception\FlattenException;
21
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
22
use Symfony\Component\Serializer\Serializer;
23

24
/**
25
 * Converts {@see \Exception} or {@see FlattenException} to a Hydra error representation.
26
 *
27
 * @deprecated we use ItemNormalizer instead
28
 *
29
 * @author Kévin Dunglas <dunglas@gmail.com>
30
 * @author Samuel ROZE <samuel.roze@gmail.com>
31
 */
32
final class ErrorNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
33
{
34
    use ErrorNormalizerTrait;
35

36
    public const FORMAT = 'jsonld';
37
    public const TITLE = 'title';
38
    private array $defaultContext = [self::TITLE => 'An error occurred'];
39

40
    public function __construct(private readonly UrlGeneratorInterface|LegacyUrlGeneratorInterface $urlGenerator, private readonly bool $debug = false, array $defaultContext = [], private readonly ?NormalizerInterface $itemNormalizer = null)
41
    {
42
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
84✔
43
    }
44

45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
49
    {
50
        trigger_deprecation('api-platform', '3.2', sprintf('The class "%s" is deprecated in favor of using an Error resource. We fallback on "api_platform.serializer.normalizer.item".', __CLASS__));
×
51

52
        if ($this->itemNormalizer) {
×
53
            return $this->itemNormalizer->normalize($object, $format, $context);
×
54
        }
55

56
        $data = [
×
57
            '@context' => $this->urlGenerator->generate('api_jsonld_context', ['shortName' => 'Error']),
×
58
            '@type' => 'hydra:Error',
×
59
            'hydra:title' => $context[self::TITLE] ?? $this->defaultContext[self::TITLE],
×
60
            'hydra:description' => $this->getErrorMessage($object, $context, $this->debug),
×
61
        ];
×
62

63
        if ($this->debug && null !== $trace = $object->getTrace()) {
×
64
            $data['trace'] = $trace;
×
65
        }
66

67
        return $data;
×
68
    }
69

70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
74
    {
75
        if ($context['skip_deprecated_exception_normalizers'] ?? false) {
×
76
            return false;
×
77
        }
78

79
        return self::FORMAT === $format && ($data instanceof \Exception || $data instanceof FlattenException);
×
80
    }
81

82
    public function getSupportedTypes($format): array
83
    {
84
        if (self::FORMAT === $format) {
54✔
85
            return [
33✔
86
                \Exception::class => false,
33✔
87
                FlattenException::class => false,
33✔
88
            ];
33✔
89
        }
90

91
        return [];
21✔
92
    }
93

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

105
        return false;
×
106
    }
107
}
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