vendor/api-platform/core/src/Core/DataProvider/ChainItemDataProvider.php line 38

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\Exception\ResourceClassNotSupportedException;
  13. /**
  14. * Tries each configured data provider and returns the result of the first able to handle the resource class.
  15. *
  16. * @author Kévin Dunglas <dunglas@gmail.com>
  17. */
  18. final class ChainItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
  19. {
  20. use RestrictDataProviderTrait;
  21. /**
  22. * @param ItemDataProviderInterface[] $dataProviders
  23. */
  24. public function __construct(iterable $dataProviders)
  25. {
  26. $this->dataProviders = $dataProviders;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
  32. {
  33. foreach ($this->dataProviders as $dataProvider) {
  34. try {
  35. if ($dataProvider instanceof RestrictedDataProviderInterface
  36. && !$dataProvider->supports($resourceClass, $operationName, $context)) {
  37. continue;
  38. }
  39. $identifier = $id;
  40. if (!$dataProvider instanceof DenormalizedIdentifiersAwareItemDataProviderInterface && $identifier && \is_array($identifier)) {
  41. if (\count($identifier) > 1) {
  42. @trigger_error(sprintf('Receiving "$id" as non-array in an item data provider is deprecated in 2.3 in favor of implementing "%s".', DenormalizedIdentifiersAwareItemDataProviderInterface::class), \E_USER_DEPRECATED);
  43. $identifier = http_build_query($identifier, '', ';');
  44. } else {
  45. $identifier = current($identifier);
  46. }
  47. }
  48. return $dataProvider->getItem($resourceClass, $identifier, $operationName, $context);
  49. } catch (ResourceClassNotSupportedException $e) {
  50. @trigger_error(sprintf('Throwing a "%s" is deprecated in favor of implementing "%s"', \get_class($e), RestrictedDataProviderInterface::class), \E_USER_DEPRECATED);
  51. continue;
  52. }
  53. }
  54. return null;
  55. }
  56. }