Completed
Push — master ( a1da24...44a686 )
by Antoine
18s queued 11s
created

ItemNormalizer::normalize()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 3
nop 3
dl 0
loc 26
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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\Core\JsonLd\Serializer;
15
16
use ApiPlatform\Core\Api\IriConverterInterface;
17
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
18
use ApiPlatform\Core\JsonLd\ContextBuilderInterface;
19
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
20
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
21
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
22
use ApiPlatform\Core\Serializer\AbstractItemNormalizer;
23
use ApiPlatform\Core\Serializer\ContextTrait;
24
use ApiPlatform\Core\Util\ClassInfoTrait;
25
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
26
use Symfony\Component\Serializer\Exception\LogicException;
27
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
28
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
29
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
30
31
/**
32
 * Converts between objects and array including JSON-LD and Hydra metadata.
33
 *
34
 * @author Kévin Dunglas <[email protected]>
35
 */
36
final class ItemNormalizer extends AbstractItemNormalizer
37
{
38
    use ClassInfoTrait;
39
    use ContextTrait;
40
    use JsonLdContextTrait;
41
42
    public const FORMAT = 'jsonld';
43
44
    private $contextBuilder;
45
46
    public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, ContextBuilderInterface $contextBuilder, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], iterable $dataTransformers = [])
47
    {
48
        parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, null, false, $defaultContext, $dataTransformers, $resourceMetadataFactory);
49
50
        $this->contextBuilder = $contextBuilder;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function supportsNormalization($data, $format = null, array $context = []): bool
57
    {
58
        return self::FORMAT === $format && parent::supportsNormalization($data, $format, $context);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     *
64
     * @throws LogicException
65
     */
66
    public function normalize($object, $format = null, array $context = [])
67
    {
68
        if (null !== $outputClass = $this->getOutputClass($this->getObjectClass($object), $context)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $outputClass is dead and can be removed.
Loading history...
69
            return parent::normalize($object, $format, $context);
70
        }
71
72
        // Use resolved resource class instead of given resource class to support multiple inheritance child types
73
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null, true);
74
        $context = $this->initContext($resourceClass, $context);
75
        $iri = $this->iriConverter->getIriFromItem($object);
76
        $context['iri'] = $iri;
77
        $context['api_normalize'] = true;
78
79
        $metadata = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
80
81
        $data = parent::normalize($object, $format, $context);
82
        if (!\is_array($data)) {
83
            return $data;
84
        }
85
86
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
87
88
        $metadata['@id'] = $iri;
89
        $metadata['@type'] = $resourceMetadata->getIri() ?: $resourceMetadata->getShortName();
90
91
        return $metadata + $data;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
98
    {
99
        return self::FORMAT === $format && parent::supportsDenormalization($data, $type, $format, $context);
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     *
105
     * @throws NotNormalizableValueException
106
     */
107
    public function denormalize($data, $class, $format = null, array $context = [])
108
    {
109
        // Avoid issues with proxies if we populated the object
110
        if (isset($data['@id']) && !isset($context[self::OBJECT_TO_POPULATE])) {
111
            if (true !== ($context['api_allow_update'] ?? true)) {
112
                throw new NotNormalizableValueException('Update is not allowed for this operation.');
113
            }
114
115
            $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getItemFromIri($data['@id'], $context + ['fetch_data' => true]);
116
        }
117
118
        return parent::denormalize($data, $class, $format, $context);
119
    }
120
}
121