vendor/api-platform/core/src/Hydra/Serializer/CollectionFiltersNormalizer.php line 53

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\Hydra\Serializer;
  12. use ApiPlatform\Api\FilterInterface;
  13. use ApiPlatform\Api\ResourceClassResolverInterface;
  14. use ApiPlatform\Core\Api\FilterCollection;
  15. use ApiPlatform\Core\Api\FilterLocatorTrait;
  16. use ApiPlatform\Core\Api\ResourceClassResolverInterface as LegacyResourceClassResolverInterface;
  17. use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
  18. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  19. use Psr\Container\ContainerInterface;
  20. use Symfony\Component\Serializer\Exception\UnexpectedValueException;
  21. use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
  22. use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
  23. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  24. /**
  25.  * Enhances the result of collection by adding the filters applied on collection.
  26.  *
  27.  * @author Samuel ROZE <samuel.roze@gmail.com>
  28.  */
  29. final class CollectionFiltersNormalizer implements NormalizerInterfaceNormalizerAwareInterfaceCacheableSupportsMethodInterface
  30. {
  31.     use FilterLocatorTrait;
  32.     private $collectionNormalizer;
  33.     private $resourceMetadataFactory;
  34.     private $resourceClassResolver;
  35.     /**
  36.      * @param ContainerInterface|FilterCollection                                 $filterLocator           The new filter locator or the deprecated filter collection
  37.      * @param mixed                                                               $resourceMetadataFactory
  38.      * @param ResourceClassResolverInterface|LegacyResourceClassResolverInterface $resourceClassResolver
  39.      */
  40.     public function __construct(NormalizerInterface $collectionNormalizer$resourceMetadataFactory$resourceClassResolver$filterLocator)
  41.     {
  42.         $this->setFilterLocator($filterLocator);
  43.         $this->collectionNormalizer $collectionNormalizer;
  44.         $this->resourceMetadataFactory $resourceMetadataFactory;
  45.         $this->resourceClassResolver $resourceClassResolver;
  46.         if (!$resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
  47.             trigger_deprecation('api-platform/core''2.7'sprintf('Use "%s" instead of "%s".'ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class));
  48.         }
  49.     }
  50.     /**
  51.      * {@inheritdoc}
  52.      */
  53.     public function supportsNormalization($data$format null, array $context = []): bool
  54.     {
  55.         return $this->collectionNormalizer->supportsNormalization($data$format$context);
  56.     }
  57.     /**
  58.      * {@inheritdoc}
  59.      */
  60.     public function hasCacheableSupportsMethod(): bool
  61.     {
  62.         return $this->collectionNormalizer instanceof CacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod();
  63.     }
  64.     /**
  65.      * {@inheritdoc}
  66.      *
  67.      * @return array|string|int|float|bool|\ArrayObject|null
  68.      */
  69.     public function normalize($object$format null, array $context = [])
  70.     {
  71.         $data $this->collectionNormalizer->normalize($object$format$context);
  72.         if (!\is_array($data)) {
  73.             throw new UnexpectedValueException('Expected data to be an array');
  74.         }
  75.         if (!isset($context['resource_class']) || isset($context['api_sub_level'])) {
  76.             return $data;
  77.         }
  78.         $resourceClass $this->resourceClassResolver->getResourceClass($object$context['resource_class']);
  79.         $resourceFilters null;
  80.         if ($this->resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
  81.             $resourceMetadata $this->resourceMetadataFactory->create($resourceClass);
  82.             $operationName $context['collection_operation_name'] ?? null;
  83.             if (null === $operationName) {
  84.                 $resourceFilters $resourceMetadata->getAttribute('filters', []);
  85.             } else {
  86.                 $resourceFilters $resourceMetadata->getCollectionOperationAttribute($operationName'filters', [], true);
  87.             }
  88.         } elseif ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
  89.             $operation $context['operation'] ?? $this->resourceMetadataFactory->create($resourceClass)->getOperation($context['operation_name'] ?? null);
  90.             $resourceFilters $operation->getFilters();
  91.         }
  92.         if (!$resourceFilters) {
  93.             return $data;
  94.         }
  95.         $requestParts parse_url($context['request_uri'] ?? '');
  96.         if (!\is_array($requestParts)) {
  97.             return $data;
  98.         }
  99.         $currentFilters = [];
  100.         foreach ($resourceFilters as $filterId) {
  101.             if ($filter $this->getFilter($filterId)) {
  102.                 $currentFilters[] = $filter;
  103.             }
  104.         }
  105.         if ($currentFilters) {
  106.             $data['hydra:search'] = $this->getSearch($resourceClass$requestParts$currentFilters);
  107.         }
  108.         return $data;
  109.     }
  110.     /**
  111.      * {@inheritdoc}
  112.      */
  113.     public function setNormalizer(NormalizerInterface $normalizer)
  114.     {
  115.         if ($this->collectionNormalizer instanceof NormalizerAwareInterface) {
  116.             $this->collectionNormalizer->setNormalizer($normalizer);
  117.         }
  118.     }
  119.     /**
  120.      * Returns the content of the Hydra search property.
  121.      *
  122.      * @param FilterInterface[] $filters
  123.      */
  124.     private function getSearch(string $resourceClass, array $parts, array $filters): array
  125.     {
  126.         $variables = [];
  127.         $mapping = [];
  128.         foreach ($filters as $filter) {
  129.             foreach ($filter->getDescription($resourceClass) as $variable => $data) {
  130.                 $variables[] = $variable;
  131.                 $mapping[] = ['@type' => 'IriTemplateMapping''variable' => $variable'property' => $data['property'], 'required' => $data['required']];
  132.             }
  133.         }
  134.         return ['@type' => 'hydra:IriTemplate''hydra:template' => sprintf('%s{?%s}'$parts['path'], implode(','$variables)), 'hydra:variableRepresentation' => 'BasicRepresentation''hydra:mapping' => $mapping];
  135.     }
  136. }
  137. class_alias(CollectionFiltersNormalizer::class, \ApiPlatform\Core\Hydra\Serializer\CollectionFiltersNormalizer::class);