-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContextBuilder.php
199 lines (166 loc) · 7.62 KB
/
ContextBuilder.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?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\JsonLd;
use ApiPlatform\JsonLd\Serializer\HydraPrefixTrait;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\IriConverterInterface;
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Metadata\UrlGeneratorInterface;
use ApiPlatform\Metadata\Util\ClassInfoTrait;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
/**
* {@inheritdoc}
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
final class ContextBuilder implements AnonymousContextBuilderInterface
{
use ClassInfoTrait;
use HydraPrefixTrait;
public const FORMAT = 'jsonld';
public const HYDRA_PREFIX = 'hydra:';
public const HYDRA_CONTEXT_HAS_PREFIX = 'hydra_prefix';
public function __construct(private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly UrlGeneratorInterface $urlGenerator, private readonly ?IriConverterInterface $iriConverter = null, private readonly ?NameConverterInterface $nameConverter = null, private array $defaultContext = [])
{
}
/**
* {@inheritdoc}
*/
public function getBaseContext(int $referenceType = UrlGeneratorInterface::ABS_URL): array
{
return [
'@vocab' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT], UrlGeneratorInterface::ABS_URL).'#',
'hydra' => self::HYDRA_NS,
];
}
/**
* {@inheritdoc}
*/
public function getEntrypointContext(int $referenceType = UrlGeneratorInterface::ABS_PATH): array
{
$context = $this->getBaseContext($referenceType);
foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
$shortName = $this->resourceMetadataFactory->create($resourceClass)[0]->getShortName();
$resourceName = lcfirst($shortName);
$context[$resourceName] = [
'@id' => 'Entrypoint/'.$resourceName,
'@type' => '@id',
];
}
return $context;
}
/**
* {@inheritdoc}
*/
public function getResourceContext(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH): array
{
/** @var HttpOperation $operation */
$operation = $this->resourceMetadataFactory->create($resourceClass)->getOperation(null, false, true);
if (null === $shortName = $operation->getShortName()) {
return [];
}
$context = $operation->getNormalizationContext();
if ($context['iri_only'] ?? false) {
$context = $this->getBaseContext($referenceType);
$context[$this->getHydraPrefix($context).'member']['@type'] = '@id';
return $context;
}
return $this->getResourceContextWithShortname($resourceClass, $referenceType, $shortName, $operation);
}
/**
* {@inheritdoc}
*/
public function getResourceContextUri(string $resourceClass, ?int $referenceType = null): string
{
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass)[0];
if (null === $referenceType) {
$referenceType = $resourceMetadata->getUrlGenerationStrategy();
}
return $this->urlGenerator->generate('api_jsonld_context', ['shortName' => $resourceMetadata->getShortName()], $referenceType ?? UrlGeneratorInterface::ABS_PATH);
}
/**
* {@inheritdoc}
*/
public function getAnonymousResourceContext(object $object, array $context = [], int $referenceType = UrlGeneratorInterface::ABS_PATH): array
{
$outputClass = $this->getObjectClass($object);
$operation = $context['operation'] ?? new Get(
shortName: (new \ReflectionClass($outputClass))->getShortName(),
normalizationContext: [
self::HYDRA_CONTEXT_HAS_PREFIX => $context[self::HYDRA_CONTEXT_HAS_PREFIX] ?? $this->defaultContext[self::HYDRA_CONTEXT_HAS_PREFIX] ?? true,
'groups' => [],
],
denormalizationContext: [
'groups' => [],
]
);
$shortName = $operation->getShortName();
$jsonLdContext = [
'@context' => $this->getResourceContextWithShortname(
$outputClass,
$referenceType,
$shortName,
$operation
),
'@type' => $shortName,
];
if (isset($context['iri'])) {
$jsonLdContext['@id'] = $context['iri'];
} elseif (true === ($context['gen_id'] ?? true) && $this->iriConverter) {
$jsonLdContext['@id'] = $this->iriConverter->getIriFromResource($object);
}
if ($context['has_context'] ?? false) {
unset($jsonLdContext['@context']);
}
// here the object can be different from the resource given by the $context['api_resource'] value
if (isset($context['api_resource'])) {
$jsonLdContext['@type'] = $this->resourceMetadataFactory->create($this->getObjectClass($context['api_resource']))[0]->getShortName();
}
return $jsonLdContext;
}
private function getResourceContextWithShortname(string $resourceClass, int $referenceType, string $shortName, ?HttpOperation $operation = null): array
{
$context = $this->getBaseContext($referenceType);
$propertyContext = $operation ? ['normalization_groups' => $operation->getNormalizationContext()['groups'] ?? null, 'denormalization_groups' => $operation->getDenormalizationContext()['groups'] ?? null] : ['normalization_groups' => [], 'denormalization_groups' => []];
foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName, $propertyContext);
if ($propertyMetadata->isIdentifier() && true !== $propertyMetadata->isWritable()) {
continue;
}
$convertedName = $this->nameConverter ? $this->nameConverter->normalize($propertyName, $resourceClass, self::FORMAT) : $propertyName;
$jsonldContext = $propertyMetadata->getJsonldContext() ?? [];
if ($id = $propertyMetadata->getIris()) {
$id = 1 === (is_countable($id) ? \count($id) : 0) ? $id[0] : $id;
}
if (!$id) {
$id = \sprintf('%s/%s', $shortName, $convertedName);
}
if (false === $propertyMetadata->isReadableLink()) {
$jsonldContext += [
'@id' => $id,
'@type' => '@id',
];
}
if (empty($jsonldContext)) {
$context[$convertedName] = $id;
} else {
$context[$convertedName] = $jsonldContext + [
'@id' => $id,
];
}
}
return $context;
}
}