Completed
Push — master ( d2b1b9...c80d49 )
by Antoine
22s queued 13s
created

JsonLdContextTrait::createJsonLdContext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 4
dl 0
loc 9
rs 10
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\JsonLd\AnonymousContextBuilderInterface;
17
use ApiPlatform\Core\JsonLd\ContextBuilderInterface;
18
19
/**
20
 * Creates and manipulates the Serializer context.
21
 *
22
 * @author Kévin Dunglas <[email protected]>
23
 *
24
 * @internal
25
 */
26
trait JsonLdContextTrait
27
{
28
    /**
29
     * Updates the given JSON-LD document to add its @context key.
30
     */
31
    private function addJsonLdContext(ContextBuilderInterface $contextBuilder, string $resourceClass, array &$context, array $data = []): array
32
    {
33
        if (isset($context['jsonld_has_context'])) {
34
            return $data;
35
        }
36
37
        $context['jsonld_has_context'] = true;
38
39
        if (isset($context['jsonld_embed_context'])) {
40
            $data['@context'] = $contextBuilder->getResourceContext($resourceClass);
41
42
            return $data;
43
        }
44
45
        $data['@context'] = $contextBuilder->getResourceContextUri($resourceClass);
46
47
        return $data;
48
    }
49
50
    private function createJsonLdContext(AnonymousContextBuilderInterface $contextBuilder, $object, array &$context, array $data = []): array
51
    {
52
        if (isset($context['jsonld_has_context'])) {
53
            return $data;
54
        }
55
56
        $context['jsonld_has_context'] = true;
57
58
        return $contextBuilder->getAnonymousResourceContext($object, $context['output']);
59
    }
60
}
61