vendor/api-platform/core/src/JsonLd/Serializer/JsonLdContextTrait.php line 54

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\JsonLd\Serializer;
  12. use ApiPlatform\JsonLd\AnonymousContextBuilderInterface;
  13. use ApiPlatform\JsonLd\ContextBuilderInterface;
  14. /**
  15.  * Creates and manipulates the Serializer context.
  16.  *
  17.  * @author Kévin Dunglas <dunglas@gmail.com>
  18.  *
  19.  * @internal
  20.  */
  21. trait JsonLdContextTrait
  22. {
  23.     /**
  24.      * Updates the given JSON-LD document to add its @context key.
  25.      */
  26.     private function addJsonLdContext(ContextBuilderInterface $contextBuilderstring $resourceClass, array &$context, array $data = []): array
  27.     {
  28.         if (isset($context['jsonld_has_context'])) {
  29.             return $data;
  30.         }
  31.         $context['jsonld_has_context'] = true;
  32.         if (isset($context['jsonld_embed_context'])) {
  33.             $data['@context'] = $contextBuilder->getResourceContext($resourceClass);
  34.             return $data;
  35.         }
  36.         $data['@context'] = $contextBuilder->getResourceContextUri($resourceClass);
  37.         return $data;
  38.     }
  39.     private function createJsonLdContext(AnonymousContextBuilderInterface $contextBuilder$object, array &$context, array $data = []): array
  40.     {
  41.         // We're in a collection, don't add the @context part
  42.         if (isset($context['jsonld_has_context'])) {
  43.             return $contextBuilder->getAnonymousResourceContext($object, ($context['output'] ?? []) + ['api_resource' => $context['api_resource'] ?? null'has_context' => true'iri' => false]);
  44.         }
  45.         $context['jsonld_has_context'] = true;
  46.         return $contextBuilder->getAnonymousResourceContext($object, ($context['output'] ?? []) + ['api_resource' => $context['api_resource'] ?? null]);
  47.     }
  48. }
  49. class_alias(JsonLdContextTrait::class, \ApiPlatform\Core\JsonLd\Serializer\JsonLdContextTrait::class);