From 6cda811f4e22366a15111a91c882b9ab1673f5ec Mon Sep 17 00:00:00 2001 From: Guillaume Sainthillier Date: Fri, 31 Jan 2025 19:08:04 +0100 Subject: [PATCH] feat(doctrine): let search filters define their default strategy --- src/Doctrine/Common/Filter/SearchFilterTrait.php | 3 ++- src/Doctrine/Odm/Filter/SearchFilter.php | 5 +++-- src/Doctrine/Orm/Filter/SearchFilter.php | 5 +++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Doctrine/Common/Filter/SearchFilterTrait.php b/src/Doctrine/Common/Filter/SearchFilterTrait.php index d04fe78ba19..b10da0f77b2 100644 --- a/src/Doctrine/Common/Filter/SearchFilterTrait.php +++ b/src/Doctrine/Common/Filter/SearchFilterTrait.php @@ -32,6 +32,7 @@ trait SearchFilterTrait { use PropertyHelperTrait; + protected string $defaultStrategy = self::STRATEGY_EXACT; protected IriConverterInterface|LegacyIriConverterInterface $iriConverter; protected PropertyAccessorInterface $propertyAccessor; protected IdentifiersExtractorInterface|LegacyIdentifiersExtractorInterface|null $identifiersExtractor = null; @@ -65,7 +66,7 @@ public function getDescription(string $resourceClass): array $propertyName = $this->normalizePropertyName($property); if ($metadata->hasField($field)) { $typeOfField = $this->getType($metadata->getTypeOfField($field)); - $strategy = $this->getProperties()[$property] ?? self::STRATEGY_EXACT; + $strategy = $this->getProperties()[$property] ?? $this->defaultStrategy; $filterParameterNames = [$propertyName]; if (\in_array($strategy, [self::STRATEGY_EXACT, self::STRATEGY_IEXACT], true)) { diff --git a/src/Doctrine/Odm/Filter/SearchFilter.php b/src/Doctrine/Odm/Filter/SearchFilter.php index fdb8c9d271c..cd5b9159c48 100644 --- a/src/Doctrine/Odm/Filter/SearchFilter.php +++ b/src/Doctrine/Odm/Filter/SearchFilter.php @@ -142,13 +142,14 @@ final class SearchFilter extends AbstractFilter implements SearchFilterInterface public const DOCTRINE_INTEGER_TYPE = [MongoDbType::INTEGER, MongoDbType::INT]; - public function __construct(ManagerRegistry $managerRegistry, IriConverterInterface|LegacyIriConverterInterface $iriConverter, IdentifiersExtractorInterface|LegacyIdentifiersExtractorInterface|null $identifiersExtractor, ?PropertyAccessorInterface $propertyAccessor = null, ?LoggerInterface $logger = null, ?array $properties = null, ?NameConverterInterface $nameConverter = null) + public function __construct(ManagerRegistry $managerRegistry, IriConverterInterface|LegacyIriConverterInterface $iriConverter, IdentifiersExtractorInterface|LegacyIdentifiersExtractorInterface|null $identifiersExtractor, ?PropertyAccessorInterface $propertyAccessor = null, ?LoggerInterface $logger = null, ?array $properties = null, ?NameConverterInterface $nameConverter = null, ?string $defaultStrategy = null) { parent::__construct($managerRegistry, $logger, $properties, $nameConverter); $this->iriConverter = $iriConverter; $this->identifiersExtractor = $identifiersExtractor; $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); + $this->defaultStrategy = $defaultStrategy ?? self::STRATEGY_EXACT; } protected function getIriConverter(): LegacyIriConverterInterface|IriConverterInterface @@ -187,7 +188,7 @@ protected function filterProperty(string $property, $value, Builder $aggregation } $caseSensitive = true; - $strategy = $this->properties[$property] ?? self::STRATEGY_EXACT; + $strategy = $this->properties[$property] ?? $this->defaultStrategy; // prefixing the strategy with i makes it case insensitive if (str_starts_with($strategy, 'i')) { diff --git a/src/Doctrine/Orm/Filter/SearchFilter.php b/src/Doctrine/Orm/Filter/SearchFilter.php index a94c8627ec0..6a0c610fa06 100644 --- a/src/Doctrine/Orm/Filter/SearchFilter.php +++ b/src/Doctrine/Orm/Filter/SearchFilter.php @@ -141,13 +141,14 @@ final class SearchFilter extends AbstractFilter implements SearchFilterInterface public const DOCTRINE_INTEGER_TYPE = Types::INTEGER; - public function __construct(ManagerRegistry $managerRegistry, IriConverterInterface|LegacyIriConverterInterface $iriConverter, ?PropertyAccessorInterface $propertyAccessor = null, ?LoggerInterface $logger = null, ?array $properties = null, IdentifiersExtractorInterface|LegacyIdentifiersExtractorInterface|null $identifiersExtractor = null, ?NameConverterInterface $nameConverter = null) + public function __construct(ManagerRegistry $managerRegistry, IriConverterInterface|LegacyIriConverterInterface $iriConverter, ?PropertyAccessorInterface $propertyAccessor = null, ?LoggerInterface $logger = null, ?array $properties = null, IdentifiersExtractorInterface|LegacyIdentifiersExtractorInterface|null $identifiersExtractor = null, ?NameConverterInterface $nameConverter = null, ?string $defaultStrategy = null) { parent::__construct($managerRegistry, $logger, $properties, $nameConverter); $this->iriConverter = $iriConverter; $this->identifiersExtractor = $identifiersExtractor; $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); + $this->defaultStrategy = $defaultStrategy ?? self::STRATEGY_EXACT; } protected function getIriConverter(): IriConverterInterface|LegacyIriConverterInterface @@ -187,7 +188,7 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB } $caseSensitive = true; - $strategy = $this->properties[$property] ?? self::STRATEGY_EXACT; + $strategy = $this->properties[$property] ?? $this->defaultStrategy; // prefixing the strategy with i makes it case insensitive if (str_starts_with($strategy, 'i')) {