vendor/api-platform/core/src/JsonLd/ContextBuilder.php line 65

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;
  12. use ApiPlatform\Api\IriConverterInterface;
  13. use ApiPlatform\Api\UrlGeneratorInterface;
  14. use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface as LegacyPropertyMetadataFactoryInterface;
  15. use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface as LegacyPropertyNameCollectionFactoryInterface;
  16. use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
  17. use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
  18. use ApiPlatform\Metadata\ApiProperty;
  19. use ApiPlatform\Metadata\HttpOperation;
  20. use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
  21. use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
  22. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  23. use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
  24. use ApiPlatform\Util\ClassInfoTrait;
  25. use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
  26. /**
  27.  * {@inheritdoc}
  28.  * TODO: 3.0 simplify or remove the class.
  29.  *
  30.  * @author Kévin Dunglas <dunglas@gmail.com>
  31.  */
  32. final class ContextBuilder implements AnonymousContextBuilderInterface
  33. {
  34.     use ClassInfoTrait;
  35.     public const FORMAT 'jsonld';
  36.     private $resourceNameCollectionFactory;
  37.     /**
  38.      * @param ResourceMetadataFactoryInterface|ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory
  39.      */
  40.     private $resourceMetadataFactory;
  41.     /**
  42.      * @var LegacyPropertyNameCollectionFactoryInterface|PropertyNameCollectionFactoryInterface
  43.      */
  44.     private $propertyNameCollectionFactory;
  45.     /**
  46.      * @var LegacyPropertyMetadataFactoryInterface|PropertyMetadataFactoryInterface
  47.      */
  48.     private $propertyMetadataFactory;
  49.     private $urlGenerator;
  50.     /**
  51.      * @var NameConverterInterface|null
  52.      */
  53.     private $nameConverter;
  54.     private $iriConverter;
  55.     public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory$resourceMetadataFactory$propertyNameCollectionFactory$propertyMetadataFactoryUrlGeneratorInterface $urlGeneratorNameConverterInterface $nameConverter nullIriConverterInterface $iriConverter null)
  56.     {
  57.         $this->resourceNameCollectionFactory $resourceNameCollectionFactory;
  58.         $this->resourceMetadataFactory $resourceMetadataFactory;
  59.         $this->propertyNameCollectionFactory $propertyNameCollectionFactory;
  60.         $this->propertyMetadataFactory $propertyMetadataFactory;
  61.         $this->urlGenerator $urlGenerator;
  62.         $this->nameConverter $nameConverter;
  63.         $this->iriConverter $iriConverter;
  64.         if ($resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
  65.             trigger_deprecation('api-platform/core''2.7'sprintf('Use "%s" instead of "%s".'ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class));
  66.         }
  67.     }
  68.     /**
  69.      * {@inheritdoc}
  70.      */
  71.     public function getBaseContext(int $referenceType UrlGeneratorInterface::ABS_URL): array
  72.     {
  73.         return [
  74.             '@vocab' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT], UrlGeneratorInterface::ABS_URL).'#',
  75.             'hydra' => self::HYDRA_NS,
  76.         ];
  77.     }
  78.     /**
  79.      * {@inheritdoc}
  80.      */
  81.     public function getEntrypointContext(int $referenceType UrlGeneratorInterface::ABS_PATH): array
  82.     {
  83.         $context $this->getBaseContext($referenceType);
  84.         foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
  85.             // TODO: remove in 3.0
  86.             if ($this->resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
  87.                 $shortName $this->resourceMetadataFactory->create($resourceClass)->getShortName();
  88.             } else {
  89.                 $shortName $this->resourceMetadataFactory->create($resourceClass)[0]->getShortName();
  90.             }
  91.             $resourceName lcfirst($shortName);
  92.             $context[$resourceName] = [
  93.                 '@id' => 'Entrypoint/'.$resourceName,
  94.                 '@type' => '@id',
  95.             ];
  96.         }
  97.         return $context;
  98.     }
  99.     /**
  100.      * {@inheritdoc}
  101.      */
  102.     public function getResourceContext(string $resourceClassint $referenceType UrlGeneratorInterface::ABS_PATH): array
  103.     {
  104.         // TODO: Remove in 3.0
  105.         if ($this->resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
  106.             $resourceMetadata $this->resourceMetadataFactory->create($resourceClass);
  107.             if (null === $shortName $resourceMetadata->getShortName()) {
  108.                 return [];
  109.             }
  110.             if ($resourceMetadata->getAttribute('normalization_context')['iri_only'] ?? false) {
  111.                 $context $this->getBaseContext($referenceType);
  112.                 $context['hydra:member']['@type'] = '@id';
  113.                 return $context;
  114.             }
  115.             return $this->getResourceContextWithShortname($resourceClass$referenceType$shortName);
  116.         }
  117.         $operation $this->resourceMetadataFactory->create($resourceClass)->getOperation();
  118.         if (null === $shortName $operation->getShortName()) {
  119.             return [];
  120.         }
  121.         if ($operation->getNormalizationContext()['iri_only'] ?? false) {
  122.             $context $this->getBaseContext($referenceType);
  123.             $context['hydra:member']['@type'] = '@id';
  124.             return $context;
  125.         }
  126.         return $this->getResourceContextWithShortname($resourceClass$referenceType$shortName$operation);
  127.     }
  128.     /**
  129.      * {@inheritdoc}
  130.      */
  131.     public function getResourceContextUri(string $resourceClassint $referenceType null): string
  132.     {
  133.         // TODO: remove in 3.0
  134.         if ($this->resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
  135.             $resourceMetadata $this->resourceMetadataFactory->create($resourceClass);
  136.             if (null === $referenceType) {
  137.                 $referenceType $resourceMetadata->getAttribute('url_generation_strategy');
  138.             }
  139.             return $this->urlGenerator->generate('api_jsonld_context', ['shortName' => $resourceMetadata->getShortName()], $referenceType);
  140.         }
  141.         $resourceMetadata $this->resourceMetadataFactory->create($resourceClass)[0];
  142.         if (null === $referenceType) {
  143.             $referenceType $resourceMetadata->getUrlGenerationStrategy();
  144.         }
  145.         return $this->urlGenerator->generate('api_jsonld_context', ['shortName' => $resourceMetadata->getShortName()], $referenceType);
  146.     }
  147.     /**
  148.      * {@inheritdoc}
  149.      */
  150.     public function getAnonymousResourceContext($object, array $context = [], int $referenceType UrlGeneratorInterface::ABS_PATH): array
  151.     {
  152.         $outputClass $this->getObjectClass($object);
  153.         $shortName = (new \ReflectionClass($outputClass))->getShortName();
  154.         $jsonLdContext = [
  155.             '@context' => $this->getResourceContextWithShortname(
  156.                 $outputClass,
  157.                 $referenceType,
  158.                 $shortName
  159.             ),
  160.             '@type' => $shortName,
  161.         ];
  162.         if (!isset($context['iri']) || false !== $context['iri']) {
  163.             // Not using an IriConverter here is deprecated in 2.7, avoid spl_object_hash as it may collide
  164.             if (isset($this->iriConverter)) {
  165.                 $jsonLdContext['@id'] = $context['iri'] ?? $this->iriConverter->getIriFromResource($object);
  166.             } else {
  167.                 $jsonLdContext['@id'] = $context['iri'] ?? '/.well-known/genid/'.bin2hex(random_bytes(10));
  168.             }
  169.         }
  170.         if ($context['has_context'] ?? false) {
  171.             unset($jsonLdContext['@context']);
  172.         }
  173.         // here the object can be different from the resource given by the $context['api_resource'] value
  174.         if (isset($context['api_resource'])) {
  175.             if ($this->resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
  176.                 $jsonLdContext['@type'] = $this->resourceMetadataFactory->create($this->getObjectClass($context['api_resource']))->getShortName();
  177.             } else {
  178.                 $jsonLdContext['@type'] = $this->resourceMetadataFactory->create($this->getObjectClass($context['api_resource']))[0]->getShortName();
  179.             }
  180.         }
  181.         return $jsonLdContext;
  182.     }
  183.     private function getResourceContextWithShortname(string $resourceClassint $referenceTypestring $shortName, ?HttpOperation $operation null): array
  184.     {
  185.         $context $this->getBaseContext($referenceType);
  186.         if ($this->propertyMetadataFactory instanceof LegacyPropertyMetadataFactoryInterface) {
  187.             $propertyContext = [];
  188.         } else {
  189.             $propertyContext $operation ? ['normalization_groups' => $operation->getNormalizationContext()['groups'] ?? null'denormalization_groups' => $operation->getDenormalizationContext()['groups'] ?? null] : ['normalization_groups' => [], 'denormalization_groups' => []];
  190.         }
  191.         foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
  192.             /** @var PropertyMetadata|ApiProperty */
  193.             $propertyMetadata $this->propertyMetadataFactory->create($resourceClass$propertyName$propertyContext);
  194.             if ($propertyMetadata->isIdentifier() && true !== $propertyMetadata->isWritable()) {
  195.                 continue;
  196.             }
  197.             $convertedName $this->nameConverter $this->nameConverter->normalize($propertyName$resourceClassself::FORMAT) : $propertyName;
  198.             if ($propertyMetadata instanceof PropertyMetadata) {
  199.                 $jsonldContext = ($propertyMetadata->getAttributes() ?? [])['jsonld_context'] ?? [];
  200.                 $id $propertyMetadata->getIri();
  201.             } else {
  202.                 $jsonldContext $propertyMetadata->getJsonldContext() ?? [];
  203.                 if ($id $propertyMetadata->getIris()) {
  204.                     $id === \count($id) ? $id[0] : $id;
  205.                 }
  206.             }
  207.             if (!$id) {
  208.                 $id sprintf('%s/%s'$shortName$convertedName);
  209.             }
  210.             if (false === $propertyMetadata->isReadableLink()) {
  211.                 $jsonldContext += [
  212.                     '@id' => $id,
  213.                     '@type' => '@id',
  214.                 ];
  215.             }
  216.             if (empty($jsonldContext)) {
  217.                 $context[$convertedName] = $id;
  218.             } else {
  219.                 $context[$convertedName] = $jsonldContext + [
  220.                     '@id' => $id,
  221.                 ];
  222.             }
  223.         }
  224.         return $context;
  225.     }
  226. }
  227. class_alias(ContextBuilder::class, \ApiPlatform\Core\JsonLd\ContextBuilder::class);