• 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

53.57
/src/JsonLd/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\JsonLd\Serializer;
15

16
use ApiPlatform\Api\IriConverterInterface as LegacyIriConverterInterface;
17
use ApiPlatform\Exception\InvalidArgumentException;
18
use ApiPlatform\JsonLd\AnonymousContextBuilderInterface;
19
use ApiPlatform\Metadata\IriConverterInterface;
20
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
21
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
22
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
23
use Symfony\Component\Serializer\Serializer;
24

25
/**
26
 * Decorates the output with JSON-LD metadata when appropriate, but otherwise just
27
 * passes through to the decorated normalizer.
28
 */
29
final class ObjectNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
30
{
31
    use JsonLdContextTrait;
32

33
    public const FORMAT = 'jsonld';
34

35
    public function __construct(private readonly NormalizerInterface $decorated, private readonly IriConverterInterface|LegacyIriConverterInterface $iriConverter, private AnonymousContextBuilderInterface $anonymousContextBuilder)
36
    {
37
    }
275✔
38

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

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

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

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

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

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

83
        /*
84
         * Converts the normalized data array of a resource into an IRI, if the
85
         * normalized data array is empty.
86
         *
87
         * This is useful when traversing from a non-resource towards an attribute
88
         * which is a resource, as we do not have the benefit of {@see ApiProperty::isReadableLink}.
89
         *
90
         * It must not be propagated to resources, as {@see ApiProperty::isReadableLink}
91
         * should take effect.
92
         */
93
        $context['api_empty_resource_as_iri'] = true;
16✔
94

95
        $data = $this->decorated->normalize($object, $format, $context);
16✔
96
        if (!\is_array($data) || !$data) {
16✔
97
            return $data;
4✔
98
        }
99

100
        if (isset($originalResource)) {
12✔
101
            try {
102
                $context['output']['iri'] = $this->iriConverter->getIriFromResource($originalResource);
8✔
103
            } catch (InvalidArgumentException) {
×
104
                // The original resource has no identifiers
105
            }
106
            $context['api_resource'] = $originalResource;
8✔
107
        }
108

109
        $metadata = $this->createJsonLdContext($this->anonymousContextBuilder, $object, $context);
12✔
110

111
        return $metadata + $data;
12✔
112
    }
113
}
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