Skip to content

Commit b32edd0

Browse files
committed
feat: Adding boolean filter
1 parent fb47197 commit b32edd0

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Laravel\Eloquent\Filter;
15+
16+
use ApiPlatform\Metadata\Parameter;
17+
use Illuminate\Database\Eloquent\Builder;
18+
use Illuminate\Database\Eloquent\Model;
19+
20+
final class BooleanFilter implements FilterInterface
21+
{
22+
use QueryPropertyTrait;
23+
24+
/**
25+
* @param Builder<Model> $builder
26+
* @param array<string, mixed> $context
27+
*/
28+
public function apply(Builder $builder, mixed $values, Parameter $parameter, array $context = []): Builder
29+
{
30+
$booleanValues = [
31+
'true' => true,
32+
'false' => false,
33+
'1' => true,
34+
'0' => false,
35+
];
36+
37+
if (array_key_exists($values, $booleanValues)) {
38+
$values = $booleanValues[$values];
39+
}
40+
41+
return $builder->{$context['whereClause'] ?? 'where'}($this->getQueryProperty($parameter), $values);
42+
}
43+
}

0 commit comments

Comments
 (0)