Skip to content
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

Add BeginsWithStrict filter #821

Merged
merged 6 commits into from
Nov 28, 2022
Merged
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: 8 additions & 0 deletions src/AllowedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Collection;
use Spatie\QueryBuilder\Filters\Filter;
use Spatie\QueryBuilder\Filters\FiltersBeginsWithStrict;
use Spatie\QueryBuilder\Filters\FiltersCallback;
use Spatie\QueryBuilder\Filters\FiltersExact;
use Spatie\QueryBuilder\Filters\FiltersPartial;
Expand Down Expand Up @@ -70,6 +71,13 @@ public static function partial(string $name, $internalName = null, bool $addRela
return new static($name, new FiltersPartial($addRelationConstraint), $internalName);
}

public static function beginsWithStrict(string $name, $internalName = null, bool $addRelationConstraint = true, string $arrayValueDelimiter = null): self
{
static::setFilterArrayValueDelimiter($arrayValueDelimiter);

return new static($name, new FiltersBeginsWithStrict($addRelationConstraint), $internalName);
}

public static function scope(string $name, $internalName = null, string $arrayValueDelimiter = null): self
{
static::setFilterArrayValueDelimiter($arrayValueDelimiter);
Expand Down
20 changes: 20 additions & 0 deletions src/Filters/FiltersBeginsWithStrict.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Spatie\QueryBuilder\Filters;

use Illuminate\Database\Eloquent\Builder;

/**
* @template TModelClass of \Illuminate\Database\Eloquent\Model
* @template-implements \Spatie\QueryBuilder\Filters\Filter<TModelClass>
*/
class FiltersBeginsWithStrict extends FiltersPartial implements Filter
{
protected function getWhereRawParameters($value, string $property): array
{
return [
"{$property} LIKE ?",
["{$value}%"],
];
}
}
20 changes: 13 additions & 7 deletions src/Filters/FiltersPartial.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,32 @@ public function __invoke(Builder $query, $value, string $property)

$wrappedProperty = $query->getQuery()->getGrammar()->wrap($query->qualifyColumn($property));

$sql = "LOWER({$wrappedProperty}) LIKE ?";

if (is_array($value)) {
if (count(array_filter($value, 'strlen')) === 0) {
return $query;
}

$query->where(function (Builder $query) use ($value, $sql) {
$query->where(function (Builder $query) use ($value, $wrappedProperty) {
foreach (array_filter($value, 'strlen') as $partialValue) {
$partialValue = mb_strtolower($partialValue, 'UTF8');

$query->orWhereRaw($sql, ["%{$partialValue}%"]);
[$sql, $bindings] = $this->getWhereRawParameters($partialValue, $wrappedProperty);
$query->orWhereRaw($sql, $bindings);
}
});

return;
}

[$sql, $bindings] = $this->getWhereRawParameters($value, $wrappedProperty);
$query->whereRaw($sql, $bindings);
}

protected function getWhereRawParameters($value, string $property): array
{
$value = mb_strtolower($value, 'UTF8');

$query->whereRaw($sql, ["%{$value}%"]);
return [
"LOWER({$property}) LIKE ?",
["%{$value}%"],
];
}
}
31 changes: 31 additions & 0 deletions tests/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,37 @@
$this->assertQueryLogContains("select * from `test_models` where (LOWER(`test_models`.`id`) LIKE ?)");
});

test('falsy values are not ignored when applying a begins with strict filter', function () {
DB::enableQueryLog();

createQueryFromFilterRequest([
'id' => [0],
])
->allowedFilters(AllowedFilter::beginsWithStrict('id'))
->get();

$this->assertQueryLogContains("select * from `test_models` where (`test_models`.`id` LIKE ?)");
});

it('can filter partial using begins with strict', function () {
TestModel::create([
'name' => 'John Doe',
]);

$models = createQueryFromFilterRequest(['name' => 'john'])
->allowedFilters([
AllowedFilter::beginsWithStrict('name'),
]);

$models2 = createQueryFromFilterRequest(['name' => 'doe'])
->allowedFilters([
AllowedFilter::beginsWithStrict('name'),
]);

expect($models->count())->toBe(1);
expect($models2->count())->toBe(0);
});

it('can filter and match results by exact property', function () {
$testModel = TestModel::first();

Expand Down