-
-
Notifications
You must be signed in to change notification settings - Fork 874
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(doctrine): boolean filter like laravel filters
- Loading branch information
1 parent
1ac0c3d
commit cc689c3
Showing
12 changed files
with
443 additions
and
24 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
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 |
---|---|---|
|
@@ -15,7 +15,12 @@ | |
|
||
use ApiPlatform\Doctrine\Common\Filter\BooleanFilterTrait; | ||
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface; | ||
use ApiPlatform\Metadata\JsonSchemaFilterInterface; | ||
use ApiPlatform\Metadata\OpenApiParameterFilterInterface; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\Metadata\Parameter; | ||
use ApiPlatform\Metadata\QueryParameter; | ||
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter; | ||
use Doctrine\DBAL\Types\Types; | ||
use Doctrine\ORM\Query\Expr\Join; | ||
use Doctrine\ORM\QueryBuilder; | ||
|
@@ -106,7 +111,7 @@ | |
* @author Amrouche Hamza <[email protected]> | ||
* @author Teoh Han Hui <[email protected]> | ||
*/ | ||
final class BooleanFilter extends AbstractFilter | ||
final class BooleanFilter extends AbstractFilter implements OpenApiParameterFilterInterface, JsonSchemaFilterInterface | ||
{ | ||
use BooleanFilterTrait; | ||
|
||
|
@@ -145,4 +150,27 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB | |
->andWhere(\sprintf('%s.%s = :%s', $alias, $field, $valueParameter)) | ||
->setParameter($valueParameter, $value); | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function getSchema(Parameter $parameter): array | ||
{ | ||
return ['type' => 'boolean']; | ||
} | ||
|
||
public function getOpenApiParameters(Parameter $parameter): OpenApiParameter|array|null | ||
{ | ||
$in = $parameter instanceof QueryParameter ? 'query' : 'header'; | ||
$key = $parameter->getKey(); | ||
|
||
return [ | ||
new OpenApiParameter( | ||
name: $key, | ||
in: $in, | ||
required: false, | ||
schema: ['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
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
60 changes: 60 additions & 0 deletions
60
tests/Fixtures/TestBundle/Document/FilteredBooleanParameter.php
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,60 @@ | ||
<?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\Tests\Fixtures\TestBundle\Document; | ||
|
||
use ApiPlatform\Doctrine\Odm\Filter\BooleanFilter; | ||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\GetCollection; | ||
use ApiPlatform\Metadata\QueryParameter; | ||
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; | ||
|
||
#[ApiResource] | ||
#[GetCollection( | ||
parameters: [ | ||
'active' => new QueryParameter( | ||
filter: new BooleanFilter(), | ||
), | ||
'enabled' => new QueryParameter( | ||
filter: new BooleanFilter(), | ||
property: 'active', | ||
), | ||
], | ||
)] | ||
#[ODM\Document] | ||
class FilteredBooleanParameter | ||
{ | ||
public function __construct( | ||
#[ODM\Id(type: 'int', strategy: 'INCREMENT')] | ||
public ?int $id = null, | ||
|
||
#[ODM\Field(type: 'bool', nullable: true)] | ||
public ?bool $active = null, | ||
) { | ||
} | ||
|
||
public function getId(): ?int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function isActive(): bool | ||
{ | ||
return $this->active; | ||
} | ||
|
||
public function setActive(?bool $active): void | ||
{ | ||
$this->active = $active; | ||
} | ||
} |
Oops, something went wrong.