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

Unset default of AllowedFilter after it was set once, which was possible with default(null) before nullable filters where implemented in 5.6.0 #902

Merged
merged 4 commits into from
Jan 8, 2024
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 @@ -176,6 +176,14 @@ public function nullable(bool $nullable = true): self
return $this;
}

public function unsetDefault(): self
{
$this->hasDefault = false;
unset($this->default);

return $this;
}

protected function resolveValueForFiltering($value)
{
if (is_array($value)) {
Expand Down
22 changes: 22 additions & 0 deletions tests/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,28 @@ public function __invoke(Builder $query, $value, string $property): Builder
expect($models->count())->toEqual(1);
});

it('should filter by query parameters if a default value is set and unset afterwards', function () {
TestModel::create(['name' => 'John Doe']);

$filterWithDefault = AllowedFilter::exact('name')->default('some default value');
$models = createQueryFromFilterRequest([
'name' => 'John Doe',
])
->allowedFilters($filterWithDefault->unsetDefault())
->get();

expect($models->count())->toEqual(1);
});

it('should not filter at all if a default value is set and unset afterwards', function () {
$filterWithDefault = AllowedFilter::exact('name')->default('some default value');
$models = createQueryFromFilterRequest([])
->allowedFilters($filterWithDefault->unsetDefault())
->get();

expect($models->count())->toEqual(5);
});

it('should apply a filter with a multi-dimensional array value', function () {
TestModel::create(['name' => 'John Doe']);

Expand Down
Loading