vendor/api-platform/core/src/DataProvider/OperationDataProviderTrait.php line 50

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\Core\DataProvider;
  12. use ApiPlatform\Core\Exception\InvalidIdentifierException;
  13. use ApiPlatform\Core\Exception\RuntimeException;
  14. use ApiPlatform\Core\Identifier\IdentifierConverterInterface;
  15. /**
  16.  * @internal
  17.  */
  18. trait OperationDataProviderTrait
  19. {
  20.     /**
  21.      * @var CollectionDataProviderInterface
  22.      */
  23.     private $collectionDataProvider;
  24.     /**
  25.      * @var ItemDataProviderInterface
  26.      */
  27.     private $itemDataProvider;
  28.     /**
  29.      * @var SubresourceDataProviderInterface|null
  30.      */
  31.     private $subresourceDataProvider;
  32.     /**
  33.      * @var IdentifierConverterInterface|null
  34.      */
  35.     private $identifierConverter;
  36.     /**
  37.      * Retrieves data for a collection operation.
  38.      *
  39.      * @return iterable
  40.      */
  41.     private function getCollectionData(array $attributes, array $context)
  42.     {
  43.         return $this->collectionDataProvider->getCollection($attributes['resource_class'], $attributes['collection_operation_name'], $context);
  44.     }
  45.     /**
  46.      * Gets data for an item operation.
  47.      *
  48.      * @return object|null
  49.      */
  50.     private function getItemData($identifiers, array $attributes, array $context)
  51.     {
  52.         return $this->itemDataProvider->getItem($attributes['resource_class'], $identifiers$attributes['item_operation_name'], $context);
  53.     }
  54.     /**
  55.      * Gets data for a nested operation.
  56.      *
  57.      * @throws RuntimeException
  58.      *
  59.      * @return array|object|null
  60.      */
  61.     private function getSubresourceData($identifiers, array $attributes, array $context)
  62.     {
  63.         if (null === $this->subresourceDataProvider) {
  64.             throw new RuntimeException('Subresources not supported');
  65.         }
  66.         return $this->subresourceDataProvider->getSubresource($attributes['resource_class'], $identifiers$attributes['subresource_context'] + $context$attributes['subresource_operation_name']);
  67.     }
  68.     /**
  69.      * @param array $parameters - usually comes from $request->attributes->all()
  70.      *
  71.      * @throws InvalidIdentifierException
  72.      */
  73.     private function extractIdentifiers(array $parameters, array $attributes)
  74.     {
  75.         if (isset($attributes['item_operation_name'])) {
  76.             if (!isset($parameters['id'])) {
  77.                 throw new InvalidIdentifierException('Parameter "id" not found');
  78.             }
  79.             $id $parameters['id'];
  80.             if (null !== $this->identifierConverter) {
  81.                 return $this->identifierConverter->convert((string) $id$attributes['resource_class']);
  82.             }
  83.             return $id;
  84.         }
  85.         if (!isset($attributes['subresource_context'])) {
  86.             throw new RuntimeException('Either "item_operation_name" or "collection_operation_name" must be defined, unless the "_api_receive" request attribute is set to false.');
  87.         }
  88.         $identifiers = [];
  89.         foreach ($attributes['subresource_context']['identifiers'] as $key => [$id$resourceClass$hasIdentifier]) {
  90.             if (false === $hasIdentifier) {
  91.                 continue;
  92.             }
  93.             $identifiers[$id] = $parameters[$id];
  94.             if (null !== $this->identifierConverter) {
  95.                 $identifiers[$id] = $this->identifierConverter->convert((string) $identifiers[$id], $resourceClass);
  96.             }
  97.         }
  98.         return $identifiers;
  99.     }
  100. }