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

api-platform / core / 11271860720

10 Oct 2024 10:04AM UTC coverage: 7.84% (+0.007%) from 7.833%
11271860720

push

github

soyuka
Merge 3.4

0 of 34 new or added lines in 5 files covered. (0.0%)

11160 existing lines in 381 files now uncovered.

12955 of 165237 relevant lines covered (7.84%)

27.06 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
    {
UNCOV
33
    }
2,634✔
34

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

43
    public function getSupportedTypes($format): array
44
    {
UNCOV
45
        return self::FORMAT === $format ? $this->decorated->getSupportedTypes($format) : [];
2,218✔
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
    {
UNCOV
53
        if (isset($context['api_resource'])) {
100✔
UNCOV
54
            $originalResource = $context['api_resource'];
6✔
UNCOV
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
         */
UNCOV
68
        $context['api_empty_resource_as_iri'] = true;
100✔
69

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

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

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

UNCOV
86
        return $metadata + $data;
77✔
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