From c01e10ff323fac6f6e3549e7e90d8ee01d9285f3 Mon Sep 17 00:00:00 2001 From: Nicolas LAURENT Date: Fri, 16 Feb 2024 09:27:14 +0100 Subject: [PATCH] fix(hydra): remove dependency from ApiPlatform/Api dependency (#6154) --- .../Serializer/CollectionFiltersNormalizer.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Hydra/Serializer/CollectionFiltersNormalizer.php b/src/Hydra/Serializer/CollectionFiltersNormalizer.php index c39f3ac621e..1ccc0203222 100644 --- a/src/Hydra/Serializer/CollectionFiltersNormalizer.php +++ b/src/Hydra/Serializer/CollectionFiltersNormalizer.php @@ -14,7 +14,6 @@ namespace ApiPlatform\Hydra\Serializer; use ApiPlatform\Api\FilterInterface as LegacyFilterInterface; -use ApiPlatform\Api\FilterLocatorTrait; use ApiPlatform\Api\ResourceClassResolverInterface as LegacyResourceClassResolverInterface; use ApiPlatform\Doctrine\Odm\State\Options as ODMOptions; use ApiPlatform\Doctrine\Orm\State\Options; @@ -37,14 +36,14 @@ */ final class CollectionFiltersNormalizer implements NormalizerInterface, NormalizerAwareInterface, CacheableSupportsMethodInterface { - use FilterLocatorTrait; + private ?ContainerInterface $filterLocator = null; /** * @param ContainerInterface $filterLocator The new filter locator or the deprecated filter collection */ public function __construct(private readonly NormalizerInterface $collectionNormalizer, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, private readonly LegacyResourceClassResolverInterface|ResourceClassResolverInterface $resourceClassResolver, ContainerInterface $filterLocator) { - $this->setFilterLocator($filterLocator); + $this->filterLocator = $filterLocator; } /** @@ -159,4 +158,16 @@ private function getSearch(string $resourceClass, array $parts, array $filters): return ['@type' => 'hydra:IriTemplate', 'hydra:template' => sprintf('%s{?%s}', $parts['path'], implode(',', $variables)), 'hydra:variableRepresentation' => 'BasicRepresentation', 'hydra:mapping' => $mapping]; } + + /** + * Gets a filter with a backward compatibility. + */ + private function getFilter(string $filterId): LegacyFilterInterface|FilterInterface|null + { + if ($this->filterLocator && $this->filterLocator->has($filterId)) { + return $this->filterLocator->get($filterId); + } + + return null; + } }