• 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

72.5
/src/JsonLd/Serializer/ItemNormalizer.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\Api\ResourceClassResolverInterface as LegacyResourceClassResolverInterface;
18
use ApiPlatform\JsonLd\AnonymousContextBuilderInterface;
19
use ApiPlatform\JsonLd\ContextBuilderInterface;
20
use ApiPlatform\Metadata\HttpOperation;
21
use ApiPlatform\Metadata\IriConverterInterface;
22
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
23
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
24
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
25
use ApiPlatform\Metadata\ResourceAccessCheckerInterface;
26
use ApiPlatform\Metadata\ResourceClassResolverInterface;
27
use ApiPlatform\Metadata\UrlGeneratorInterface;
28
use ApiPlatform\Metadata\Util\ClassInfoTrait;
29
use ApiPlatform\Serializer\AbstractItemNormalizer;
30
use ApiPlatform\Serializer\ContextTrait;
31
use ApiPlatform\Serializer\TagCollectorInterface;
32
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
33
use Symfony\Component\Serializer\Exception\LogicException;
34
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
35
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
36
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
37

38
/**
39
 * Converts between objects and array including JSON-LD and Hydra metadata.
40
 *
41
 * @author Kévin Dunglas <dunglas@gmail.com>
42
 */
43
final class ItemNormalizer extends AbstractItemNormalizer
44
{
45
    use ClassInfoTrait;
46
    use ContextTrait;
47
    use JsonLdContextTrait;
48

49
    public const FORMAT = 'jsonld';
50

51
    public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface|LegacyIriConverterInterface $iriConverter, ResourceClassResolverInterface|LegacyResourceClassResolverInterface $resourceClassResolver, private readonly ContextBuilderInterface $contextBuilder, ?PropertyAccessorInterface $propertyAccessor = null, ?NameConverterInterface $nameConverter = null, ?ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ?ResourceAccessCheckerInterface $resourceAccessChecker = null, protected ?TagCollectorInterface $tagCollector = null)
52
    {
53
        parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataCollectionFactory, $resourceAccessChecker, $tagCollector);
263✔
54
    }
55

56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
60
    {
61
        return self::FORMAT === $format && parent::supportsNormalization($data, $format, $context);
136✔
62
    }
63

64
    public function getSupportedTypes($format): array
65
    {
66
        return self::FORMAT === $format ? parent::getSupportedTypes($format) : [];
211✔
67
    }
68

69
    /**
70
     * {@inheritdoc}
71
     *
72
     * @throws LogicException
73
     */
74
    public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
75
    {
76
        $resourceClass = $this->getObjectClass($object);
116✔
77

78
        if ($this->getOutputClass($context)) {
116✔
79
            return parent::normalize($object, $format, $context);
8✔
80
        }
81

82
        // TODO: we should not remove the resource_class in the normalizeRawCollection as we would find out anyway that it's not the same as the requested one
83
        $previousResourceClass = $context['resource_class'] ?? null;
116✔
84
        $metadata = [];
116✔
85
        if ($isResourceClass = $this->resourceClassResolver->isResourceClass($resourceClass) && (null === $previousResourceClass || $this->resourceClassResolver->isResourceClass($previousResourceClass))) {
116✔
86
            $resourceClass = $this->resourceClassResolver->getResourceClass($object, $previousResourceClass);
108✔
87
            $context = $this->initContext($resourceClass, $context);
108✔
88
            $metadata = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
108✔
89
        } elseif ($this->contextBuilder instanceof AnonymousContextBuilderInterface) {
8✔
90
            if ($context['api_collection_sub_level'] ?? false) {
8✔
91
                unset($context['api_collection_sub_level']);
×
92
                $context['output']['genid'] = true;
×
93
                $context['output']['iri'] = null;
×
94
            }
95

96
            // We should improve what's behind the context creation, its probably more complicated then it should
97
            $metadata = $this->createJsonLdContext($this->contextBuilder, $object, $context);
8✔
98
        }
99

100
        // maybe not needed anymore
101
        if (isset($context['operation']) && $previousResourceClass !== $resourceClass) {
116✔
102
            unset($context['operation'], $context['operation_name']);
×
103
        }
104

105
        if (true === ($context['force_iri_generation'] ?? true) && $iri = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context)) {
116✔
106
            $context['iri'] = $iri;
116✔
107
            $metadata['@id'] = $iri;
116✔
108
        }
109

110
        $context['api_normalize'] = true;
116✔
111

112
        $data = parent::normalize($object, $format, $context);
116✔
113
        if (!\is_array($data)) {
116✔
114
            return $data;
×
115
        }
116

117
        if (!isset($metadata['@type']) && $isResourceClass) {
116✔
118
            $operation = $context['operation'] ?? $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
108✔
119

120
            $types = $operation instanceof HttpOperation ? $operation->getTypes() : null;
108✔
121
            if (null === $types) {
108✔
122
                $types = [$operation->getShortName()];
104✔
123
            }
124
            $metadata['@type'] = 1 === \count($types) ? $types[0] : $types;
108✔
125
        }
126

127
        return $metadata + $data;
116✔
128
    }
129

130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
134
    {
135
        return self::FORMAT === $format && parent::supportsDenormalization($data, $type, $format, $context);
×
136
    }
137

138
    /**
139
     * {@inheritdoc}
140
     *
141
     * @throws NotNormalizableValueException
142
     */
143
    public function denormalize(mixed $data, string $class, ?string $format = null, array $context = []): mixed
144
    {
145
        // Avoid issues with proxies if we populated the object
146
        if (isset($data['@id']) && !isset($context[self::OBJECT_TO_POPULATE])) {
×
147
            if (true !== ($context['api_allow_update'] ?? true)) {
×
148
                throw new NotNormalizableValueException('Update is not allowed for this operation.');
×
149
            }
150

151
            $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri($data['@id'], $context + ['fetch_data' => true]);
×
152
        }
153

154
        return parent::denormalize($data, $class, $format, $context);
×
155
    }
156
}
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