-
-
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 a5fe140
Showing
16 changed files
with
493 additions
and
41 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 |
---|---|---|
|
@@ -17,8 +17,8 @@ | |
use ApiPlatform\Doctrine\Common\PropertyHelperTrait; | ||
use ApiPlatform\Doctrine\Odm\PropertyHelperTrait as MongoDbOdmPropertyHelperTrait; | ||
use ApiPlatform\Metadata\Operation; | ||
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry; | ||
use Doctrine\ODM\MongoDB\Aggregation\Builder; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\NullLogger; | ||
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; | ||
|
@@ -30,14 +30,18 @@ | |
* | ||
* @author Alan Poulain <[email protected]> | ||
*/ | ||
abstract class AbstractFilter implements FilterInterface, PropertyAwareFilterInterface | ||
abstract class AbstractFilter implements FilterInterface, PropertyAwareFilterInterface, ManagerRegistryConfigurableInterface | ||
{ | ||
use MongoDbOdmPropertyHelperTrait; | ||
use PropertyHelperTrait; | ||
protected LoggerInterface $logger; | ||
|
||
public function __construct(protected ManagerRegistry $managerRegistry, ?LoggerInterface $logger = null, protected ?array $properties = null, protected ?NameConverterInterface $nameConverter = null) | ||
{ | ||
public function __construct( | ||
protected ?ManagerRegistry $managerRegistry = null, | ||
?LoggerInterface $logger = null, | ||
protected ?array $properties = null, | ||
protected ?NameConverterInterface $nameConverter = null, | ||
) { | ||
$this->logger = $logger ?? new NullLogger(); | ||
} | ||
|
||
|
@@ -56,18 +60,35 @@ public function apply(Builder $aggregationBuilder, string $resourceClass, ?Opera | |
*/ | ||
abstract protected function filterProperty(string $property, $value, Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void; | ||
|
||
public function hasManagerRegistry(): bool | ||
{ | ||
return $this->managerRegistry instanceof ManagerRegistry; | ||
} | ||
|
||
protected function getManagerRegistry(): ManagerRegistry | ||
{ | ||
if (!$this->hasManagerRegistry()) { | ||
throw new \RuntimeException('ManagerRegistry must be initialized before accessing it.'); | ||
} | ||
|
||
return $this->managerRegistry; | ||
} | ||
|
||
protected function getProperties(): ?array | ||
public function setManagerRegistry(?ManagerRegistry $managerRegistry): ?ManagerRegistry | ||
{ | ||
return $this->managerRegistry = $managerRegistry; | ||
} | ||
|
||
/** | ||
* @return array<string, mixed>|null | ||
*/ | ||
public function getProperties(): ?array | ||
{ | ||
return $this->properties; | ||
} | ||
|
||
/** | ||
* @param string[] $properties | ||
* @param array<string, mixed> $properties | ||
*/ | ||
public function setProperties(array $properties): void | ||
{ | ||
|
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 |
---|---|---|
|
@@ -14,7 +14,9 @@ | |
namespace ApiPlatform\Doctrine\Odm\Filter; | ||
|
||
use ApiPlatform\Doctrine\Common\Filter\BooleanFilterTrait; | ||
use ApiPlatform\Metadata\JsonSchemaFilterInterface; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\Metadata\Parameter; | ||
use Doctrine\ODM\MongoDB\Aggregation\Builder; | ||
use Doctrine\ODM\MongoDB\Types\Type as MongoDbType; | ||
|
||
|
@@ -104,7 +106,7 @@ | |
* @author Teoh Han Hui <[email protected]> | ||
* @author Alan Poulain <[email protected]> | ||
*/ | ||
final class BooleanFilter extends AbstractFilter | ||
final class BooleanFilter extends AbstractFilter implements JsonSchemaFilterInterface | ||
{ | ||
use BooleanFilterTrait; | ||
|
||
|
@@ -139,4 +141,12 @@ protected function filterProperty(string $property, $value, Builder $aggregation | |
|
||
$aggregationBuilder->match()->field($matchField)->equals($value); | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
*/ | ||
public function getSchema(Parameter $parameter): array | ||
{ | ||
return $parameter->getSchema() ?? ['type' => 'boolean']; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Doctrine/Odm/Filter/ManagerRegistryConfigurableInterface.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,21 @@ | ||
<?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\Doctrine\Odm\Filter; | ||
|
||
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry; | ||
|
||
interface ManagerRegistryConfigurableInterface | ||
{ | ||
public function setManagerRegistry(?ManagerRegistry $managerRegistry): ?ManagerRegistry; | ||
} |
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
Oops, something went wrong.