Completed
Pull Request — master (#608)
by Amrouche
05:13 queued 01:46
created

JsonLdContextTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 31
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A addJsonLdContext() 0 18 3
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\Hypermedia\ContextBuilderInterface;
15
16
/**
17
 * Creates and manipulates the Serializer context.
18
 *
19
 * @author Kévin Dunglas <[email protected]>
20
 *
21
 * @internal
22
 */
23
trait JsonLdContextTrait
24
{
25
    /**
26
     * Updates the given JSON-LD document to add its @context key.
27
     *
28
     * @param ContextBuilderInterface $contextBuilder
29
     * @param string                  $resourceClass
30
     * @param array                   $context
31
     * @param array                   $data
32
     *
33
     * @return array
34
     */
35
    private function addJsonLdContext(ContextBuilderInterface $contextBuilder, string $resourceClass, array &$context, array $data = []) : array
36
    {
37
        if (isset($context['jsonld_has_context'])) {
38
            return $data;
39
        }
40
41
        $context['jsonld_has_context'] = true;
42
43
        if (isset($context['jsonld_embed_context'])) {
44
            $data['@context'] = $contextBuilder->getResourceContext($resourceClass);
45
46
            return $data;
47
        }
48
49
        $data['@context'] = $contextBuilder->getResourceContextUri($resourceClass);
50
51
        return $data;
52
    }
53
}
54