• 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

22.22
/src/Hal/Serializer/ObjectNormalizer.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\Hal\Serializer;
15

16
use ApiPlatform\Api\IriConverterInterface;
17
use Symfony\Component\Serializer\Exception\LogicException;
18
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
19
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
20
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
21

22
/**
23
 * Decorates the output with JSON HAL metadata when appropriate, but otherwise
24
 * just passes through to the decorated normalizer.
25
 */
26
final class ObjectNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
27
{
28
    public const FORMAT = 'jsonhal';
29

30
    public function __construct(private readonly NormalizerInterface $decorated, private readonly IriConverterInterface $iriConverter)
31
    {
32
    }
58✔
33

34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
38
    {
39
        return self::FORMAT === $format && $this->decorated->supportsNormalization($data, $format, $context);
2✔
40
    }
41

42
    public function getSupportedTypes($format): array
43
    {
44
        // @deprecated remove condition when support for symfony versions under 6.3 is dropped
45
        if (!method_exists($this->decorated, 'getSupportedTypes')) {
16✔
46
            return [
×
47
                '*' => $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
×
48
            ];
×
49
        }
50

51
        return self::FORMAT === $format ? $this->decorated->getSupportedTypes($format) : [];
16✔
52
    }
53

54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function hasCacheableSupportsMethod(): bool
58
    {
59
        trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
×
60

61
        return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
×
62
    }
63

64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
68
    {
69
        if (isset($context['api_resource'])) {
×
70
            $originalResource = $context['api_resource'];
×
71
            unset($context['api_resource']);
×
72
        }
73

74
        $data = $this->decorated->normalize($object, $format, $context);
×
75
        if (!\is_array($data)) {
×
76
            return $data;
×
77
        }
78

79
        if (!isset($originalResource)) {
×
80
            return $data;
×
81
        }
82

83
        $metadata = [
×
84
            '_links' => [
×
85
                'self' => [
×
86
                    'href' => $this->iriConverter->getIriFromResource($originalResource),
×
87
                ],
×
88
            ],
×
89
        ];
×
90

91
        return $metadata + $data;
×
92
    }
93

94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
98
    {
99
        // prevent the use of lower priority normalizers (e.g. serializer.normalizer.object) for this format
100
        return self::FORMAT === $format;
2✔
101
    }
102

103
    /**
104
     * {@inheritdoc}
105
     *
106
     * @throws LogicException
107
     */
108
    public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
109
    {
110
        throw new LogicException(sprintf('%s is a read-only format.', self::FORMAT));
2✔
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

© 2026 Coveralls, Inc