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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 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,168✔
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) : [];
979✔
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'])) {
36✔
54
            $originalResource = $context['api_resource'];
4✔
55
            unset($context['api_resource']);
4✔
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;
36✔
69

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

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

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

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