vendor/api-platform/core/src/Core/Bridge/Symfony/Messenger/DataTransformer.php line 37

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\Bridge\Symfony\Messenger;
  12. use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
  13. use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
  14. use ApiPlatform\Exception\OperationNotFoundException;
  15. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  16. use ApiPlatform\Util\ClassInfoTrait;
  17. /**
  18.  * Transforms an Input to itself. This gives the ability to send the Input to a
  19.  * message handler and process it asynchronously.
  20.  *
  21.  * @author Antoine Bluchet <soyuka@gmail.com>
  22.  */
  23. final class DataTransformer implements DataTransformerInterface
  24. {
  25.     use ClassInfoTrait;
  26.     /**
  27.      * @var ResourceMetadataCollectionFactoryInterface|ResourceMetadataFactoryInterface
  28.      */
  29.     private $resourceMetadataFactory;
  30.     public function __construct($resourceMetadataFactory)
  31.     {
  32.         $this->resourceMetadataFactory $resourceMetadataFactory;
  33.         if (!$resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
  34.             trigger_deprecation('api-platform/core''2.7'sprintf('Use "%s" instead of "%s".'ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class));
  35.         }
  36.     }
  37.     /**
  38.      * {@inheritdoc}
  39.      *
  40.      * @return object
  41.      */
  42.     public function transform($objectstring $to, array $context = [])
  43.     {
  44.         return $object;
  45.     }
  46.     /**
  47.      * {@inheritdoc}
  48.      */
  49.     public function supportsTransformation($datastring $to, array $context = []): bool
  50.     {
  51.         if (
  52.             \is_object($data// data is not normalized yet, it should be an array
  53.             ||
  54.             null === ($context['input']['class'] ?? null)
  55.         ) {
  56.             return false;
  57.         }
  58.         if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
  59.             try {
  60.                 $resourceMetadataCollection $this->resourceMetadataFactory->create($context['resource_class'] ?? $to);
  61.                 $operation $resourceMetadataCollection->getOperation($context['operation_name'] ?? null);
  62.                 return 'input' === $operation->getMessenger();
  63.             } catch (OperationNotFoundException $e) {
  64.                 return false;
  65.             }
  66.         }
  67.         $metadata $this->resourceMetadataFactory->create($context['resource_class'] ?? $to);
  68.         if (isset($context['graphql_operation_name'])) {
  69.             return 'input' === $metadata->getGraphqlAttribute($context['graphql_operation_name'], 'messenger'nulltrue);
  70.         }
  71.         if (!isset($context['operation_type'])) {
  72.             return 'input' === $metadata->getAttribute('messenger');
  73.         }
  74.         return 'input' === $metadata->getTypedOperationAttribute(
  75.             $context['operation_type'],
  76.             $context[$context['operation_type'].'_operation_name'] ?? '',
  77.             'messenger',
  78.             null,
  79.             true
  80.         );
  81.     }
  82. }