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

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 ResourceClassResolverInterface|LegacyResourceClassResolverInterface $resourceClassResolver
  38.      */
  39.     public function __construct(NormalizerInterface $collectionNormalizer$resourceMetadataFactory$resourceClassResolver$filterLocator)
  40.     {
  41.         $this->setFilterLocator($filterLocator);
  42.         $this->collectionNormalizer $collectionNormalizer;
  43.         $this->resourceMetadataFactory $resourceMetadataFactory;
  44.         $this->resourceClassResolver $resourceClassResolver;
  45.         if (!$resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
  46.             trigger_deprecation('api-platform/core''2.7'sprintf('Use "%s" instead of "%s".'ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class));
  47.         }
  48.     }
  49.     public function supportsNormalization($data$format null, array $context = []): bool
  50.     {
  51.         return $this->collectionNormalizer->supportsNormalization($data$format$context);
  52.     }
  53.     public function hasCacheableSupportsMethod(): bool
  54.     {
  55.         return $this->collectionNormalizer instanceof CacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod();
  56.     }
  57.     /**
  58.      * @param mixed|null $format
  59.      *
  60.      * @return array|string|int|float|bool|\ArrayObject|null
  61.      */
  62.     public function normalize($object$format null, array $context = [])
  63.     {
  64.         $data $this->collectionNormalizer->normalize($object$format$context);
  65.         if (!\is_array($data)) {
  66.             throw new UnexpectedValueException('Expected data to be an array');
  67.         }
  68.         if (!isset($context['resource_class']) || isset($context['api_sub_level'])) {
  69.             return $data;
  70.         }
  71.         $resourceClass $this->resourceClassResolver->getResourceClass($object$context['resource_class']);
  72.         $resourceFilters null;
  73.         if ($this->resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
  74.             $resourceMetadata $this->resourceMetadataFactory->create($resourceClass);
  75.             $operationName $context['collection_operation_name'] ?? null;
  76.             if (null === $operationName) {
  77.                 $resourceFilters $resourceMetadata->getAttribute('filters', []);
  78.             } else {
  79.                 $resourceFilters $resourceMetadata->getCollectionOperationAttribute($operationName'filters', [], true);
  80.             }
  81.         } elseif ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
  82.             $operation $context['operation'] ?? $this->resourceMetadataFactory->create($resourceClass)->getOperation($context['operation_name'] ?? null);
  83.             $resourceFilters $operation->getFilters();
  84.         }
  85.         if (!$resourceFilters) {
  86.             return $data;
  87.         }
  88.         $requestParts parse_url($context['request_uri'] ?? '');
  89.         if (!\is_array($requestParts)) {
  90.             return $data;
  91.         }
  92.         $currentFilters = [];
  93.         foreach ($resourceFilters as $filterId) {
  94.             if ($filter $this->getFilter($filterId)) {
  95.                 $currentFilters[] = $filter;
  96.             }
  97.         }
  98.         if ($currentFilters) {
  99.             $data['hydra:search'] = $this->getSearch($resourceClass$requestParts$currentFilters);
  100.         }
  101.         return $data;
  102.     }
  103.     public function setNormalizer(NormalizerInterface $normalizer)
  104.     {
  105.         if ($this->collectionNormalizer instanceof NormalizerAwareInterface) {
  106.             $this->collectionNormalizer->setNormalizer($normalizer);
  107.         }
  108.     }
  109.     /**
  110.      * Returns the content of the Hydra search property.
  111.      *
  112.      * @param FilterInterface[] $filters
  113.      */
  114.     private function getSearch(string $resourceClass, array $parts, array $filters): array
  115.     {
  116.         $variables = [];
  117.         $mapping = [];
  118.         foreach ($filters as $filter) {
  119.             foreach ($filter->getDescription($resourceClass) as $variable => $data) {
  120.                 $variables[] = $variable;
  121.                 $mapping[] = ['@type' => 'IriTemplateMapping''variable' => $variable'property' => $data['property'], 'required' => $data['required']];
  122.             }
  123.         }
  124.         return ['@type' => 'hydra:IriTemplate''hydra:template' => sprintf('%s{?%s}'$parts['path'], implode(','$variables)), 'hydra:variableRepresentation' => 'BasicRepresentation''hydra:mapping' => $mapping];
  125.     }
  126. }
  127. class_alias(CollectionFiltersNormalizer::class, \ApiPlatform\Core\Hydra\Serializer\CollectionFiltersNormalizer::class);