-
-
Notifications
You must be signed in to change notification settings - Fork 887
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(laravel): boolean filter (#6806)
* feat: Adding boolean filter * refactor: return early if wrong values * tests: boolean filter test * refactor(BooleanFilter): cs fixer * add test --------- Co-authored-by: adriafigueres <[email protected]> Co-authored-by: soyuka <[email protected]>
- Loading branch information
1 parent
c59e275
commit c78ed0b
Showing
9 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Laravel\Eloquent\Filter; | ||
|
||
use ApiPlatform\Metadata\JsonSchemaFilterInterface; | ||
use ApiPlatform\Metadata\Parameter; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
final class BooleanFilter implements FilterInterface, JsonSchemaFilterInterface | ||
{ | ||
use QueryPropertyTrait; | ||
|
||
private const BOOLEAN_VALUES = [ | ||
'true' => true, | ||
'false' => false, | ||
'1' => true, | ||
'0' => false, | ||
]; | ||
|
||
/** | ||
* @param Builder<Model> $builder | ||
* @param array<string, mixed> $context | ||
*/ | ||
public function apply(Builder $builder, mixed $values, Parameter $parameter, array $context = []): Builder | ||
{ | ||
if (!\is_string($values) || !\array_key_exists($values, self::BOOLEAN_VALUES)) { | ||
return $builder; | ||
} | ||
|
||
return $builder->{$context['whereClause'] ?? 'where'}($this->getQueryProperty($parameter), $values); | ||
} | ||
|
||
public function getSchema(Parameter $parameter): array | ||
{ | ||
return ['type' => 'boolean']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Laravel\Tests\Eloquent\Filter; | ||
|
||
use ApiPlatform\Laravel\Eloquent\Filter\BooleanFilter; | ||
use ApiPlatform\Metadata\QueryParameter; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class BooleanFilterTest extends TestCase | ||
{ | ||
public function testOperator(): void | ||
{ | ||
$f = new BooleanFilter(); | ||
$builder = $this->createStub(Builder::class); | ||
$this->assertEquals($builder, $f->apply($builder, ['is_active' => 'true'], new QueryParameter(key: 'isActive', property: 'is_active'))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters