vendor/api-platform/core/src/Doctrine/Common/State/LinksHandlerTrait.php line 91

  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\Doctrine\Common\State;
  12. use ApiPlatform\Exception\OperationNotFoundException;
  13. use ApiPlatform\Exception\RuntimeException;
  14. use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation;
  15. use ApiPlatform\Metadata\GraphQl\Query;
  16. use ApiPlatform\Metadata\HttpOperation;
  17. use ApiPlatform\Metadata\Link;
  18. use ApiPlatform\Metadata\Operation;
  19. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  20. trait LinksHandlerTrait
  21. {
  22.     private ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory;
  23.     /**
  24.      * @return Link[]
  25.      */
  26.     private function getLinks(string $resourceClassOperation $operation, array $context): array
  27.     {
  28.         $links $this->getOperationLinks($operation);
  29.         if (!($linkClass $context['linkClass'] ?? false)) {
  30.             return $links;
  31.         }
  32.         $newLink null;
  33.         $linkProperty $context['linkProperty'] ?? null;
  34.         foreach ($links as $link) {
  35.             if ($linkClass === $link->getFromClass() && $linkProperty === $link->getFromProperty()) {
  36.                 $newLink $link;
  37.                 break;
  38.             }
  39.         }
  40.         if ($newLink) {
  41.             return [$newLink];
  42.         }
  43.         if (!$this->resourceMetadataCollectionFactory) {
  44.             return [];
  45.         }
  46.         // Using GraphQL, it's possible that we won't find a GraphQL Operation of the same type (e.g. it is disabled).
  47.         try {
  48.             $resourceMetadataCollection $this->resourceMetadataCollectionFactory->create($linkClass);
  49.             $linkedOperation $resourceMetadataCollection->getOperation($operation->getName());
  50.         } catch (OperationNotFoundException $e) {
  51.             if (!$operation instanceof GraphQlOperation) {
  52.                 throw $e;
  53.             }
  54.             // Instead, we'll look for the first Query available.
  55.             foreach ($resourceMetadataCollection as $resourceMetadata) {
  56.                 foreach ($resourceMetadata->getGraphQlOperations() as $op) {
  57.                     if ($op instanceof Query) {
  58.                         $linkedOperation $op;
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.         foreach ($this->getOperationLinks($linkedOperation ?? null) as $link) {
  64.             if ($resourceClass === $link->getToClass() && $linkProperty === $link->getFromProperty()) {
  65.                 $newLink $link;
  66.                 break;
  67.             }
  68.         }
  69.         if (!$newLink) {
  70.             throw new RuntimeException(sprintf('The class "%s" cannot be retrieved from "%s".'$resourceClass$linkClass));
  71.         }
  72.         return [$newLink];
  73.     }
  74.     private function getIdentifierValue(array &$identifiersstring $name null): mixed
  75.     {
  76.         if (isset($identifiers[$name])) {
  77.             $value $identifiers[$name];
  78.             unset($identifiers[$name]);
  79.             return $value;
  80.         }
  81.         return array_shift($identifiers);
  82.     }
  83.     private function getOperationLinks(Operation $operation null): array
  84.     {
  85.         if ($operation instanceof GraphQlOperation) {
  86.             return $operation->getLinks() ?? [];
  87.         }
  88.         if ($operation instanceof HttpOperation) {
  89.             return $operation->getUriVariables() ?? [];
  90.         }
  91.         return [];
  92.     }
  93. }