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

api-platform / core / 5644825543

pending completion
5644825543

push

github

web-flow
feat(serializer): support for getSupportedTypes (symfony 6.3) (#5672)

109 of 109 new or added lines in 29 files covered. (100.0%)

10881 of 18245 relevant lines covered (59.64%)

20.04 hits per line

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

77.78
/src/Problem/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\Problem\Serializer;
15

16
use Symfony\Component\ErrorHandler\Exception\FlattenException;
17
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
18
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
19

20
/**
21
 * Normalizes errors according to the API Problem spec (RFC 7807).
22
 *
23
 * @see https://tools.ietf.org/html/rfc7807
24
 *
25
 * @author Kévin Dunglas <dunglas@gmail.com>
26
 */
27
final class ErrorNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
28
{
29
    use ErrorNormalizerTrait;
30
    public const FORMAT = 'jsonproblem';
31
    public const TYPE = 'type';
32
    public const TITLE = 'title';
33
    private array $defaultContext = [
34
        self::TYPE => 'https://tools.ietf.org/html/rfc2616#section-10',
35
        self::TITLE => 'An error occurred',
36
    ];
37

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

43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function normalize(mixed $object, string $format = null, array $context = []): array
47
    {
48
        $data = [
18✔
49
            'type' => $context[self::TYPE] ?? $this->defaultContext[self::TYPE],
18✔
50
            'title' => $context[self::TITLE] ?? $this->defaultContext[self::TITLE],
18✔
51
            'detail' => $this->getErrorMessage($object, $context, $this->debug),
18✔
52
        ];
18✔
53

54
        if ($this->debug && null !== $trace = $object->getTrace()) {
18✔
55
            $data['trace'] = $trace;
8✔
56
        }
57

58
        return $data;
18✔
59
    }
60

61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
65
    {
66
        return self::FORMAT === $format && ($data instanceof \Exception || $data instanceof FlattenException);
2✔
67
    }
68

69
    public function getSupportedTypes($format): array
70
    {
71
        if (self::FORMAT === $format) {
28✔
72
            return [
×
73
                \Exception::class => true,
×
74
                FlattenException::class => true,
×
75
            ];
×
76
        }
77

78
        return [];
28✔
79
    }
80

81
    public function hasCacheableSupportsMethod(): bool
82
    {
83
        trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
2✔
84

85
        return true;
2✔
86
    }
87
}
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

© 2026 Coveralls, Inc