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

api-platform / core / 9710836697

28 Jun 2024 09:35AM UTC coverage: 63.285% (+1.2%) from 62.122%
9710836697

push

github

soyuka
docs: changelog v3.3.7

11104 of 17546 relevant lines covered (63.29%)

52.26 hits per line

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

18.18
/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 as LegacyIriConverterInterface;
17
use ApiPlatform\Metadata\IriConverterInterface;
18
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
19
use Symfony\Component\Serializer\Exception\LogicException;
20
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
21
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
22
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
23
use Symfony\Component\Serializer\Serializer;
24

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

33
    public function __construct(private readonly NormalizerInterface $decorated, private readonly IriConverterInterface|LegacyIriConverterInterface $iriConverter)
34
    {
35
    }
267✔
36

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

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

54
        return self::FORMAT === $format ? $this->decorated->getSupportedTypes($format) : [];
175✔
55
    }
56

57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function hasCacheableSupportsMethod(): bool
61
    {
62
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
63
            trigger_deprecation(
×
64
                'api-platform/core',
×
65
                '3.1',
×
66
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
67
                __METHOD__
×
68
            );
×
69
        }
70

71
        return $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
×
72
    }
73

74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
78
    {
79
        if (isset($context['api_resource'])) {
×
80
            $originalResource = $context['api_resource'];
×
81
            unset($context['api_resource']);
×
82
        }
83

84
        $data = $this->decorated->normalize($object, $format, $context);
×
85
        if (!\is_array($data)) {
×
86
            return $data;
×
87
        }
88

89
        if (!isset($originalResource)) {
×
90
            return $data;
×
91
        }
92

93
        $metadata = [
×
94
            '_links' => [
×
95
                'self' => [
×
96
                    'href' => $this->iriConverter->getIriFromResource($originalResource),
×
97
                ],
×
98
            ],
×
99
        ];
×
100

101
        return $metadata + $data;
×
102
    }
103

104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
108
    {
109
        // prevent the use of lower priority normalizers (e.g. serializer.normalizer.object) for this format
110
        return self::FORMAT === $format;
4✔
111
    }
112

113
    /**
114
     * {@inheritdoc}
115
     *
116
     * @throws LogicException
117
     */
118
    public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
119
    {
120
        throw new LogicException(sprintf('%s is a read-only format.', self::FORMAT));
4✔
121
    }
122
}
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