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

feat: add conditionable allowed filter #987

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
14 changes: 14 additions & 0 deletions src/AllowedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\QueryBuilder;

use Closure;
use Illuminate\Support\Collection;
use Spatie\QueryBuilder\Enums\FilterOperator;
use Spatie\QueryBuilder\Filters\Filter;
Expand All @@ -27,6 +28,8 @@ class AllowedFilter

protected bool $nullable = false;

protected ?Closure $condition = null;

public function __construct(
protected string $name,
protected Filter $filterClass,
Expand All @@ -39,6 +42,10 @@ public function __construct(

public function filter(QueryBuilder $query, $value): void
{
if ($this->condition && ! value($this->condition, $this)) {
return;
}

$valueToFilter = $this->resolveValueForFiltering($value);

if (! $this->nullable && is_null($valueToFilter)) {
Expand Down Expand Up @@ -204,4 +211,11 @@ protected function resolveValueForFiltering($value)

return ! $this->ignored->contains($value) ? $value : null;
}

public function when(Closure $callback): static
{
$this->condition = $callback;

return $this;
}
}
30 changes: 30 additions & 0 deletions tests/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -894,3 +894,33 @@ public function __invoke(Builder $query, $value, string $property): Builder

expect($results)->toHaveCount(2);
});

it('can filter models by partial property when the condition return true', function () {
$models = createQueryFromFilterRequest([
'name' => $this->models->first()->name,
])
->allowedFilters(AllowedFilter::partial('name')->when(fn () => true))
->get();

expect($models)->toHaveCount(1);
});

it('cannot filter models by partial property when the condition return false', function () {
$models = createQueryFromFilterRequest([
'name' => $this->models->first()->name,
])
->allowedFilters(AllowedFilter::partial('name')->when(fn () => false))
->get();

expect($models)->toHaveCount(5);
});

it('can filter only trashed passing the class as a parameter to evaluate the condition', function () {
$models = createQueryFromFilterRequest([
'name' => $this->models->first()->name,
])
->allowedFilters(AllowedFilter::partial('name')->when(fn (AllowedFilter $filter) => $filter->getName() === 'name'))
->get();

expect($models)->toHaveCount(1);
});
30 changes: 30 additions & 0 deletions tests/FiltersTrashedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,33 @@

expect($models)->toHaveCount(3);
});

it('can filter only trashed when the condition return true', function () {
$models = createQueryFromFilterRequest([
'trashed' => 'only',
], SoftDeleteModel::class)
->allowedFilters(AllowedFilter::trashed()->when(fn () => true))
->get();

expect($models)->toHaveCount(1);
});

it('cannot filter only trashed when the condition return false', function () {
$models = createQueryFromFilterRequest([
'trashed' => 'only',
], SoftDeleteModel::class)
->allowedFilters(AllowedFilter::trashed()->when(fn () => false))
->get();

expect($models)->toHaveCount(2);
});

it('can filter only trashed passing the class as a parameter to evaluate the condition', function () {
$models = createQueryFromFilterRequest([
'trashed' => 'only',
], SoftDeleteModel::class)
->allowedFilters(AllowedFilter::trashed()->when(fn (AllowedFilter $filter) => $filter->getName() === 'trashed'))
->get();

expect($models)->toHaveCount(1);
});