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

api-platform / core / 7196499749

13 Dec 2023 02:17PM UTC coverage: 37.359% (+1.4%) from 36.003%
7196499749

push

github

web-flow
ci: conflict sebastian/comparator (#6032)

* ci: conflict sebastian/comparator

* for lowest

10295 of 27557 relevant lines covered (37.36%)

28.14 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\ResourceClassResolverInterface;
26
use ApiPlatform\Metadata\UrlGeneratorInterface;
27
use ApiPlatform\Metadata\Util\ClassInfoTrait;
28
use ApiPlatform\Serializer\AbstractItemNormalizer;
29
use ApiPlatform\Serializer\ContextTrait;
30
use ApiPlatform\Symfony\Security\ResourceAccessCheckerInterface;
31
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
32
use Symfony\Component\Serializer\Exception\LogicException;
33
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
34
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
35
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
36

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

48
    public const FORMAT = 'jsonld';
49

50
    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)
51
    {
52
        parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataCollectionFactory, $resourceAccessChecker);
120✔
53
    }
54

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

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

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

77
        if ($this->getOutputClass($context)) {
24✔
78
            return parent::normalize($object, $format, $context);
4✔
79
        }
80

81
        // 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
82
        $previousResourceClass = $context['resource_class'] ?? null;
24✔
83
        $metadata = [];
24✔
84
        if ($isResourceClass = $this->resourceClassResolver->isResourceClass($resourceClass) && (null === $previousResourceClass || $this->resourceClassResolver->isResourceClass($previousResourceClass))) {
24✔
85
            $resourceClass = $this->resourceClassResolver->getResourceClass($object, $previousResourceClass);
20✔
86
            $context = $this->initContext($resourceClass, $context);
20✔
87
            $metadata = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
20✔
88
        } elseif ($this->contextBuilder instanceof AnonymousContextBuilderInterface) {
4✔
89
            if ($context['api_collection_sub_level'] ?? false) {
4✔
90
                unset($context['api_collection_sub_level']);
×
91
                $context['output']['genid'] = true;
×
92
                $context['output']['iri'] = null;
×
93
            }
94

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

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

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

109
        $context['api_normalize'] = true;
24✔
110

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

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

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

126
        return $metadata + $data;
24✔
127
    }
128

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

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

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

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

© 2026 Coveralls, Inc