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