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

api-platform / core / 7142557150

08 Dec 2023 02:28PM UTC coverage: 36.003% (-1.4%) from 37.36%
7142557150

push

github

web-flow
fix(jsonld): remove link to ApiDocumentation when doc is disabled (#6029)

0 of 1 new or added line in 1 file covered. (0.0%)

2297 existing lines in 182 files now uncovered.

9992 of 27753 relevant lines covered (36.0%)

147.09 hits per line

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

27.27
/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
    }
2,499✔
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);
6✔
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')) {
2,061✔
49
            return [
×
50
                '*' => $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
×
51
            ];
×
52
        }
53

54
        return self::FORMAT === $format ? $this->decorated->getSupportedTypes($format) : [];
2,061✔
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'])) {
6✔
80
            $originalResource = $context['api_resource'];
×
81
            unset($context['api_resource']);
×
82
        }
83

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

89
        if (!isset($originalResource)) {
6✔
90
            return $data;
6✔
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
UNCOV
110
        return self::FORMAT === $format;
×
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
    {
UNCOV
120
        throw new LogicException(sprintf('%s is a read-only format.', self::FORMAT));
×
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

© 2026 Coveralls, Inc