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

api-platform / core / 13944674159

19 Mar 2025 10:47AM UTC coverage: 7.275% (-0.01%) from 7.289%
13944674159

push

github

soyuka
Merge 3.4

6 of 23 new or added lines in 4 files covered. (26.09%)

2380 existing lines in 154 files now uncovered.

12415 of 170643 relevant lines covered (7.28%)

12.04 hits per line

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

93.75
/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\JsonLd\AnonymousContextBuilderInterface;
17
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
18
use ApiPlatform\Metadata\IriConverterInterface;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20

21
/**
22
 * Decorates the output with JSON-LD metadata when appropriate, but otherwise just
23
 * passes through to the decorated normalizer.
24
 */
25
final class ObjectNormalizer implements NormalizerInterface
26
{
27
    use JsonLdContextTrait;
28

29
    public const FORMAT = 'jsonld';
30

31
    public function __construct(private readonly NormalizerInterface $decorated, private readonly IriConverterInterface $iriConverter, private AnonymousContextBuilderInterface $anonymousContextBuilder)
32
    {
33
    }
1,111✔
34

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

43
    public function getSupportedTypes($format): array
44
    {
45
        return self::FORMAT === $format ? $this->decorated->getSupportedTypes($format) : [];
905✔
46
    }
47

48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
52
    {
53
        if (isset($context['api_resource'])) {
40✔
54
            $originalResource = $context['api_resource'];
6✔
55
            unset($context['api_resource']);
6✔
56
        }
57

58
        /*
59
         * Converts the normalized data array of a resource into an IRI, if the
60
         * normalized data array is empty.
61
         *
62
         * This is useful when traversing from a non-resource towards an attribute
63
         * which is a resource, as we do not have the benefit of {@see ApiProperty::isReadableLink}.
64
         *
65
         * It must not be propagated to resources, as {@see ApiProperty::isReadableLink}
66
         * should take effect.
67
         */
68
        $context['api_empty_resource_as_iri'] = true;
40✔
69

70
        $data = $this->decorated->normalize($object, $format, $context);
40✔
71
        if (!\is_array($data) || !$data) {
40✔
72
            return $data;
9✔
73
        }
74

75
        if (isset($originalResource)) {
31✔
76
            try {
77
                $context['output']['iri'] = $this->iriConverter->getIriFromResource($originalResource);
6✔
78
            } catch (InvalidArgumentException) {
×
79
                // The original resource has no identifiers
80
            }
81
            $context['api_resource'] = $originalResource;
6✔
82
        }
83

84
        $metadata = $this->createJsonLdContext($this->anonymousContextBuilder, $object, $context);
31✔
85

86
        return $metadata + $data;
31✔
87
    }
88
}
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