Skip to content

Commit 21bedd7

Browse files
fix(Matches): Correctly match Booleans: GET converts them to 0 and 1 (#632)
* fix(Matches): Correctly match Booleans: GET converts them to 0 and 1 * Fix styling --------- Co-authored-by: arthurkirkosa <arthurkirkosa@users.noreply.github.com>
1 parent 37f8098 commit 21bedd7

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/Filters/MatchFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function filter(RestifyRequest $request, Builder|Relation $query, $value)
4646
break;
4747
case RestifySearchable::MATCH_BOOL:
4848
case 'boolean':
49-
if ($value === 'false') {
49+
if ($value === 'false' || $value === '0') {
5050
$query->where(function ($query) use ($field) {
5151
if ($this->negation) {
5252
return $query->where($field, true);

tests/Feature/Filters/MatchFilterTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Binaryk\LaravelRestify\Contracts\RestifySearchable;
66
use Binaryk\LaravelRestify\Filters\MatchFilter;
7+
use Binaryk\LaravelRestify\Tests\Fixtures\Post\Post;
78
use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostRepository;
89
use Binaryk\LaravelRestify\Tests\Fixtures\User\User;
910
use Binaryk\LaravelRestify\Tests\Fixtures\User\UserRepository;
@@ -175,6 +176,26 @@ public function test_can_match_date(): void
175176
$this->getJson(UserRepository::route(query: ['created_at' => '2020-12-01']))->assertJsonCount(3, 'data');
176177
}
177178

179+
public function test_can_match_boolean(): void
180+
{
181+
PostRepository::$match = [
182+
'is_active' => 'boolean',
183+
];
184+
185+
Post::factory(2)->sequence(
186+
['is_active' => true, 'title' => 'Active post 1'],
187+
['is_active' => false, 'title' => 'Inactive post 2'],
188+
)->create();
189+
190+
$this->getJson(PostRepository::route())->assertJsonCount(2, 'data');
191+
192+
$this->getJson(PostRepository::route(query: ['is_active' => true]))
193+
->assertJsonCount(1, 'data');
194+
195+
$this->getJson(PostRepository::route(query: ['is_active' => false]))
196+
->assertJsonCount(1, 'data');
197+
}
198+
178199
public function test_can_match_datetime_interval(): void
179200
{
180201
User::factory()->state([

tests/Fixtures/Post/PostRepository.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class PostRepository extends Repository
2727

2828
public static array $match = [
2929
'title' => RestifySearchable::MATCH_TEXT,
30+
'is_active' => RestifySearchable::MATCH_BOOL,
3031
];
3132

3233
public static array $middleware = [];
@@ -50,6 +51,8 @@ public function fields(RestifyRequest $request): array
5051
field('description')->storingRules('required')->messages([
5152
'required' => 'Description field is required',
5253
]),
54+
55+
field('is_active'),
5356
];
5457
}
5558

0 commit comments

Comments
 (0)