⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions src/Doctrine/Odm/Filter/PartialSearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ final class PartialSearchFilter implements FilterInterface, OpenApiParameterFilt
use BackwardCompatibleFilterDescriptionTrait;
use OpenApiFilterTrait;

public function __construct(private readonly bool $caseSensitive = true)
{
}

public function apply(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void
{
$parameter = $context['parameter'];
Expand All @@ -47,7 +51,7 @@ public function apply(Builder $aggregationBuilder, string $resourceClass, ?Opera
if (!is_iterable($values)) {
$escapedValue = preg_quote($values, '/');
$match->{$operator}(
$aggregationBuilder->matchExpr()->field($property)->equals(new Regex($escapedValue, 'i'))
$aggregationBuilder->matchExpr()->field($property)->equals(new Regex($escapedValue, $this->caseSensitive ? '' : 'i'))
);

return;
Expand All @@ -60,7 +64,7 @@ public function apply(Builder $aggregationBuilder, string $resourceClass, ?Opera
$or->addOr(
$aggregationBuilder->matchExpr()
->field($property)
->equals(new Regex($escapedValue, 'i'))
->equals(new Regex($escapedValue, $this->caseSensitive ? '' : 'i'))
);
}

Expand Down
13 changes: 11 additions & 2 deletions src/Doctrine/Orm/Filter/PartialSearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ final class PartialSearchFilter implements FilterInterface, OpenApiParameterFilt
use BackwardCompatibleFilterDescriptionTrait;
use OpenApiFilterTrait;

public function __construct(private readonly bool $caseSensitive = false)
{
}

public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
{
$parameter = $context['parameter'];
Expand All @@ -46,7 +50,9 @@ public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $q
$parameterName = $queryNameGenerator->generateParameterName($property);
$queryBuilder->setParameter($parameterName, $this->formatLikeValue($values));

$likeExpression = 'LOWER('.$field.') LIKE LOWER(:'.$parameterName.') ESCAPE \'\\\'';
$likeExpression = $this->caseSensitive
? $field.' LIKE :'.$parameterName.' ESCAPE \'\\\''
: 'LOWER('.$field.') LIKE LOWER(:'.$parameterName.') ESCAPE \'\\\'';
$queryBuilder->{$context['whereClause'] ?? 'andWhere'}($likeExpression);

return;
Expand All @@ -55,7 +61,10 @@ public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $q
$likeExpressions = [];
foreach ($values as $val) {
$parameterName = $queryNameGenerator->generateParameterName($property);
$likeExpressions[] = 'LOWER('.$field.') LIKE LOWER(:'.$parameterName.') ESCAPE \'\\\'';
$likeExpressions[] = $this->caseSensitive
? $field.' LIKE :'.$parameterName.' ESCAPE \'\\\''
: 'LOWER('.$field.') LIKE LOWER(:'.$parameterName.') ESCAPE \'\\\'';

$queryBuilder->setParameter($parameterName, $this->formatLikeValue($val));
}

Expand Down
6 changes: 5 additions & 1 deletion tests/Fixtures/TestBundle/Document/Chicken.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@
'name' => new QueryParameter(filter: new ExactFilter()),
'nameExactNoProperty' => new QueryParameter(filter: new ExactFilter()),
'namePartial' => new QueryParameter(
filter: new PartialSearchFilter(),
filter: new PartialSearchFilter(false),
property: 'name',
),
'namePartialNoProperty' => new QueryParameter(filter: new PartialSearchFilter()),
'namePartialSensitive' => new QueryParameter(
filter: new PartialSearchFilter(true),
property: 'name',
),
'autocomplete' => new QueryParameter(filter: new FreeTextQueryFilter(new OrFilter(new ExactFilter())), properties: ['name', 'ean']),
'q' => new QueryParameter(filter: new FreeTextQueryFilter(new PartialSearchFilter()), properties: ['name', 'ean']),
],
Expand Down
4 changes: 4 additions & 0 deletions tests/Fixtures/TestBundle/Entity/Chicken.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
property: 'name',
),
'namePartialNoProperty' => new QueryParameter(filter: new PartialSearchFilter()),
'namePartialSensitive' => new QueryParameter(
filter: new PartialSearchFilter(true),
property: 'name',
),
'autocomplete' => new QueryParameter(filter: new FreeTextQueryFilter(new OrFilter(new ExactFilter())), properties: ['name', 'ean']),
'q' => new QueryParameter(filter: new FreeTextQueryFilter(new PartialSearchFilter()), properties: ['name', 'ean']),
],
Expand Down
37 changes: 37 additions & 0 deletions tests/Functional/Parameters/PartialSearchFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,43 @@ public function testPartialSearchFilterThrowsExceptionWhenPropertyIsMissing(): v
);
}

#[DataProvider('partialSearchFilterCaseSensitiveProvider')]
public function testPartialSearchCaseSensitiveFilter(string $url, int $expectedCount, array $expectedNames): void
{
if ($this->isMysql() || $this->isSqlite()) {
$this->markTestSkipped('Mysql and sqlite use case insensitive LIKE.');
}

$this->testPartialSearchFilter($url, $expectedCount, $expectedNames);
}

public static function partialSearchFilterCaseSensitiveProvider(): \Generator
{
yield 'filter by partial name "tru"' => [
'/chickens?namePartial=tru',
1,
['Gertrude'],
];

yield 'filter by partial name "TRU"' => [
'/chickens?namePartial=TRU',
1,
['Gertrude'],
];

yield 'filter by case sensitive partial name "tru"' => [
'/chickens?namePartialSensitive=tru',
1,
['Gertrude'],
];

yield 'filter by case sensitive partial name "TRU"' => [
'/chickens?namePartialSensitive=TRU',
0,
[],
];
}

/**
* @throws \Throwable
* @throws MongoDBException
Expand Down
5 changes: 5 additions & 0 deletions tests/RecreateSchemaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ private function isMongoDB(): bool
return 'mongodb' === static::getContainer()->getParameter('kernel.environment');
}

private function isMysql(): bool
{
return 'mysql' === static::getContainer()->getParameter('kernel.environment');
}

private function isPostgres(): bool
{
return 'postgres' === static::getContainer()->getParameter('kernel.environment');
Expand Down
Loading