/
ItemNormalizer.php
122 lines (101 loc) · 4.9 KB
/
ItemNormalizer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Core\JsonLd\Serializer;
use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
use ApiPlatform\Core\JsonLd\ContextBuilderInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Security\ResourceAccessCheckerInterface;
use ApiPlatform\Core\Serializer\AbstractItemNormalizer;
use ApiPlatform\Core\Serializer\ContextTrait;
use ApiPlatform\Core\Util\ClassInfoTrait;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
/**
* Converts between objects and array including JSON-LD and Hydra metadata.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
final class ItemNormalizer extends AbstractItemNormalizer
{
use ClassInfoTrait;
use ContextTrait;
use JsonLdContextTrait;
public const FORMAT = 'jsonld';
private $contextBuilder;
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 = [], ResourceAccessCheckerInterface $resourceAccessChecker = null)
{
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, null, false, $defaultContext, $dataTransformers, $resourceMetadataFactory, $resourceAccessChecker);
$this->contextBuilder = $contextBuilder;
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null): bool
{
return self::FORMAT === $format && parent::supportsNormalization($data, $format);
}
/**
* {@inheritdoc}
*
* @throws LogicException
*/
public function normalize($object, $format = null, array $context = [])
{
$objectClass = $this->getObjectClass($object);
$outputClass = $this->getOutputClass($objectClass, $context);
if (null !== $outputClass && !isset($context[self::IS_TRANSFORMED_TO_SAME_CLASS])) {
return parent::normalize($object, $format, $context);
}
$resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null);
$context = $this->initContext($resourceClass, $context);
$iri = $this->iriConverter->getIriFromItem($object);
$context['iri'] = $iri;
$context['api_normalize'] = true;
$metadata = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
$data = parent::normalize($object, $format, $context);
if (!\is_array($data)) {
return $data;
}
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
$metadata['@id'] = $iri;
$metadata['@type'] = $resourceMetadata->getIri() ?: $resourceMetadata->getShortName();
return $metadata + $data;
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null): bool
{
return self::FORMAT === $format && parent::supportsDenormalization($data, $type, $format);
}
/**
* {@inheritdoc}
*
* @throws NotNormalizableValueException
*/
public function denormalize($data, $class, $format = null, array $context = [])
{
// Avoid issues with proxies if we populated the object
if (isset($data['@id']) && !isset($context[self::OBJECT_TO_POPULATE])) {
if (true !== ($context['api_allow_update'] ?? true)) {
throw new NotNormalizableValueException('Update is not allowed for this operation.');
}
$context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getItemFromIri($data['@id'], $context + ['fetch_data' => true]);
}
return parent::denormalize($data, $class, $format, $context);
}
}