• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 11035928124

25 Sep 2024 03:10PM UTC coverage: 5.963% (-1.9%) from 7.833%
11035928124

push

github

dunglas
Merge branch '4.0'

2 of 55 new or added lines in 6 files covered. (3.64%)

2414 existing lines in 166 files now uncovered.

9834 of 164915 relevant lines covered (5.96%)

4.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/src/JsonLd/Serializer/JsonLdContextTrait.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\JsonLd\Serializer;
15

16
use ApiPlatform\JsonLd\AnonymousContextBuilderInterface;
17
use ApiPlatform\JsonLd\ContextBuilderInterface;
18

19
/**
20
 * Creates and manipulates the Serializer context.
21
 *
22
 * @author Kévin Dunglas <dunglas@gmail.com>
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'])) {
153✔
34
            return $data;
48✔
35
        }
36

37
        $context['jsonld_has_context'] = true;
153✔
38

39
        if (isset($context['jsonld_embed_context'])) {
153✔
UNCOV
40
            $data['@context'] = $contextBuilder->getResourceContext($resourceClass);
×
41

UNCOV
42
            return $data;
×
43
        }
44

45
        $data['@context'] = $contextBuilder->getResourceContextUri($resourceClass);
153✔
46

47
        return $data;
153✔
48
    }
49

50
    private function createJsonLdContext(AnonymousContextBuilderInterface $contextBuilder, $object, array &$context): array
51
    {
52
        // We're in a collection, don't add the @context part
53
        if (isset($context['jsonld_has_context'])) {
15✔
54
            return $contextBuilder->getAnonymousResourceContext($object, ($context['output'] ?? []) + ['api_resource' => $context['api_resource'] ?? null, 'has_context' => true]);
6✔
55
        }
56

57
        $context['jsonld_has_context'] = true;
12✔
58

59
        return $contextBuilder->getAnonymousResourceContext($object, ($context['output'] ?? []) + ['api_resource' => $context['api_resource'] ?? null]);
12✔
60
    }
61
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc