Skip to content

Commit

Permalink
feat(doctrine) let search filters define their default strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-sainthillier committed Jan 31, 2025
1 parent 7c52f34 commit f2ece2b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/Doctrine/Common/Filter/SearchFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
5 changes: 3 additions & 2 deletions src/Doctrine/Odm/Filter/SearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')) {
Expand Down
5 changes: 3 additions & 2 deletions src/Doctrine/Orm/Filter/SearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')) {
Expand Down

0 comments on commit f2ece2b

Please sign in to comment.