|
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
|
|
|
namespace ApiPlatform\Core\JsonLd\Serializer; |
|
13
|
|
|
|
|
14
|
|
|
use ApiPlatform\Core\JsonLd\ContextBuilderInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Creates and manipulates the Serializer context. |
|
18
|
|
|
* |
|
19
|
|
|
* @author Kévin Dunglas <[email protected]> |
|
20
|
|
|
* |
|
21
|
|
|
* @internal |
|
22
|
|
|
*/ |
|
23
|
|
|
trait ContextTrait |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Import the context defined in metadata and set some default values. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $resourceClass |
|
29
|
|
|
* @param array $context |
|
30
|
|
|
* |
|
31
|
|
|
* @return array |
|
32
|
|
|
*/ |
|
33
|
|
|
private function createContext(string $resourceClass, array $context) : array |
|
34
|
|
|
{ |
|
35
|
|
|
if (isset($context['jsonld_has_context'])) { |
|
36
|
|
|
return $context; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return array_merge($context, [ |
|
40
|
|
|
'jsonld_has_context' => true, |
|
41
|
|
|
// Don't use hydra:Collection in sub levels |
|
42
|
|
|
'jsonld_sub_level' => true, |
|
43
|
|
|
'resource_class' => $resourceClass, |
|
44
|
|
|
]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Updates the given JSON-LD document to add its @context key. |
|
49
|
|
|
* |
|
50
|
|
|
* @param ContextBuilderInterface $contextBuilder |
|
51
|
|
|
* @param string $resourceClass |
|
52
|
|
|
* @param array $context |
|
53
|
|
|
* @param array $data |
|
54
|
|
|
* |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
|
|
private function addJsonLdContext(ContextBuilderInterface $contextBuilder, string $resourceClass, array $context, array $data = []) : array |
|
58
|
|
|
{ |
|
59
|
|
|
if (isset($context['jsonld_has_context'])) { |
|
60
|
|
|
return $data; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (isset($context['jsonld_embed_context'])) { |
|
64
|
|
|
$data['@context'] = $contextBuilder->getResourceContext($resourceClass); |
|
65
|
|
|
|
|
66
|
|
|
return $data; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$data['@context'] = $contextBuilder->getResourceContextUri($resourceClass); |
|
70
|
|
|
|
|
71
|
|
|
return $data; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|