• 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

10.0
/src/GraphQl/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\GraphQl\Serializer;
15

16
use ApiPlatform\Api\IdentifiersExtractorInterface as LegacyIdentifiersExtractorInterface;
17
use ApiPlatform\Api\IriConverterInterface as LegacyIriConverterInterface;
18
use ApiPlatform\Metadata\IdentifiersExtractorInterface;
19
use ApiPlatform\Metadata\IriConverterInterface;
20
use ApiPlatform\Metadata\Util\ClassInfoTrait;
21
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
22
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
23
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
24
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
25
use Symfony\Component\Serializer\Serializer;
26

27
/**
28
 * Decorates the output with GraphQL metadata when appropriate, but otherwise just
29
 * passes through to the decorated normalizer.
30
 */
31
final class ObjectNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
32
{
33
    use ClassInfoTrait;
34

35
    public const FORMAT = 'graphql';
36
    public const ITEM_RESOURCE_CLASS_KEY = '#itemResourceClass';
37
    public const ITEM_IDENTIFIERS_KEY = '#itemIdentifiers';
38

39
    public function __construct(private readonly NormalizerInterface $decorated, private readonly IriConverterInterface|LegacyIriConverterInterface $iriConverter, private readonly IdentifiersExtractorInterface|LegacyIdentifiersExtractorInterface $identifiersExtractor)
40
    {
41
    }
259✔
42

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

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

60
        return self::FORMAT === $format ? $this->decorated->getSupportedTypes($format) : [];
175✔
61
    }
62

63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function hasCacheableSupportsMethod(): bool
67
    {
68
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
69
            trigger_deprecation(
×
70
                'api-platform/core',
×
71
                '3.1',
×
72
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
73
                __METHOD__
×
74
            );
×
75
        }
76

77
        return $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
×
78
    }
79

80
    /**
81
     * {@inheritdoc}
82
     *
83
     * @throws UnexpectedValueException
84
     */
85
    public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
86
    {
87
        if (isset($context['api_resource'])) {
×
88
            $originalResource = $context['api_resource'];
×
89
            unset($context['api_resource']);
×
90
        }
91

92
        $data = $this->decorated->normalize($object, $format, $context);
×
93
        if (!\is_array($data)) {
×
94
            throw new UnexpectedValueException('Expected data to be an array.');
×
95
        }
96

97
        if (!isset($originalResource)) {
×
98
            return $data;
×
99
        }
100

101
        if (isset($data['id'])) {
×
102
            $data['_id'] = $data['id'];
×
103
            $data['id'] = $this->iriConverter->getIriFromResource($originalResource);
×
104
        }
105

106
        if (!($context['no_resolver_data'] ?? false)) {
×
107
            $data[self::ITEM_RESOURCE_CLASS_KEY] = $this->getObjectClass($originalResource);
×
108
            $data[self::ITEM_IDENTIFIERS_KEY] = $this->identifiersExtractor->getIdentifiersFromItem($originalResource);
×
109
        }
110

111
        return $data;
×
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