Skip to content

Commit f2ece2b

Browse files
feat(doctrine) let search filters define their default strategy
1 parent 7c52f34 commit f2ece2b

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

src/Doctrine/Common/Filter/SearchFilterTrait.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ trait SearchFilterTrait
3232
{
3333
use PropertyHelperTrait;
3434

35+
protected string $defaultStrategy = self::STRATEGY_EXACT;
3536
protected IriConverterInterface|LegacyIriConverterInterface $iriConverter;
3637
protected PropertyAccessorInterface $propertyAccessor;
3738
protected IdentifiersExtractorInterface|LegacyIdentifiersExtractorInterface|null $identifiersExtractor = null;
@@ -65,7 +66,7 @@ public function getDescription(string $resourceClass): array
6566
$propertyName = $this->normalizePropertyName($property);
6667
if ($metadata->hasField($field)) {
6768
$typeOfField = $this->getType($metadata->getTypeOfField($field));
68-
$strategy = $this->getProperties()[$property] ?? self::STRATEGY_EXACT;
69+
$strategy = $this->getProperties()[$property] ?? $this->defaultStrategy;
6970
$filterParameterNames = [$propertyName];
7071

7172
if (\in_array($strategy, [self::STRATEGY_EXACT, self::STRATEGY_IEXACT], true)) {

src/Doctrine/Odm/Filter/SearchFilter.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,14 @@ final class SearchFilter extends AbstractFilter implements SearchFilterInterface
142142

143143
public const DOCTRINE_INTEGER_TYPE = [MongoDbType::INTEGER, MongoDbType::INT];
144144

145-
public function __construct(ManagerRegistry $managerRegistry, IriConverterInterface|LegacyIriConverterInterface $iriConverter, IdentifiersExtractorInterface|LegacyIdentifiersExtractorInterface|null $identifiersExtractor, ?PropertyAccessorInterface $propertyAccessor = null, ?LoggerInterface $logger = null, ?array $properties = null, ?NameConverterInterface $nameConverter = null)
145+
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)
146146
{
147147
parent::__construct($managerRegistry, $logger, $properties, $nameConverter);
148148

149149
$this->iriConverter = $iriConverter;
150150
$this->identifiersExtractor = $identifiersExtractor;
151151
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
152+
$this->defaultStrategy = $defaultStrategy ?? self::STRATEGY_EXACT;
152153
}
153154

154155
protected function getIriConverter(): LegacyIriConverterInterface|IriConverterInterface
@@ -187,7 +188,7 @@ protected function filterProperty(string $property, $value, Builder $aggregation
187188
}
188189

189190
$caseSensitive = true;
190-
$strategy = $this->properties[$property] ?? self::STRATEGY_EXACT;
191+
$strategy = $this->properties[$property] ?? $this->defaultStrategy;
191192

192193
// prefixing the strategy with i makes it case insensitive
193194
if (str_starts_with($strategy, 'i')) {

src/Doctrine/Orm/Filter/SearchFilter.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,14 @@ final class SearchFilter extends AbstractFilter implements SearchFilterInterface
141141

142142
public const DOCTRINE_INTEGER_TYPE = Types::INTEGER;
143143

144-
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)
144+
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)
145145
{
146146
parent::__construct($managerRegistry, $logger, $properties, $nameConverter);
147147

148148
$this->iriConverter = $iriConverter;
149149
$this->identifiersExtractor = $identifiersExtractor;
150150
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
151+
$this->defaultStrategy = $defaultStrategy ?? self::STRATEGY_EXACT;
151152
}
152153

153154
protected function getIriConverter(): IriConverterInterface|LegacyIriConverterInterface
@@ -187,7 +188,7 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
187188
}
188189

189190
$caseSensitive = true;
190-
$strategy = $this->properties[$property] ?? self::STRATEGY_EXACT;
191+
$strategy = $this->properties[$property] ?? $this->defaultStrategy;
191192

192193
// prefixing the strategy with i makes it case insensitive
193194
if (str_starts_with($strategy, 'i')) {

0 commit comments

Comments
 (0)