Skip to content

let search filters define their default strategy #6938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Doctrine/Common/Filter/SearchFilterTrait.php
Original file line number Diff line number Diff line change
@@ -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)) {
5 changes: 3 additions & 2 deletions src/Doctrine/Odm/Filter/SearchFilter.php
Original file line number Diff line number Diff line change
@@ -142,13 +142,14 @@

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)

Check warning on line 145 in src/Doctrine/Odm/Filter/SearchFilter.php

Codecov / codecov/patch

src/Doctrine/Odm/Filter/SearchFilter.php#L145

Added line #L145 was not covered by tests
{
parent::__construct($managerRegistry, $logger, $properties, $nameConverter);

$this->iriConverter = $iriConverter;
$this->identifiersExtractor = $identifiersExtractor;
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
$this->defaultStrategy = $defaultStrategy ?? self::STRATEGY_EXACT;

Check warning on line 152 in src/Doctrine/Odm/Filter/SearchFilter.php

Codecov / codecov/patch

src/Doctrine/Odm/Filter/SearchFilter.php#L152

Added line #L152 was not covered by tests
}

protected function getIriConverter(): LegacyIriConverterInterface|IriConverterInterface
@@ -187,7 +188,7 @@
}

$caseSensitive = true;
$strategy = $this->properties[$property] ?? self::STRATEGY_EXACT;
$strategy = $this->properties[$property] ?? $this->defaultStrategy;

Check warning on line 191 in src/Doctrine/Odm/Filter/SearchFilter.php

Codecov / codecov/patch

src/Doctrine/Odm/Filter/SearchFilter.php#L191

Added line #L191 was not covered by tests

// prefixing the strategy with i makes it case insensitive
if (str_starts_with($strategy, 'i')) {
5 changes: 3 additions & 2 deletions src/Doctrine/Orm/Filter/SearchFilter.php
Original file line number Diff line number Diff line change
@@ -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')) {