|
| 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\Doctrine\Orm\Filter; |
| 15 | + |
| 16 | +use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface; |
| 17 | +use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface; |
| 18 | +use ApiPlatform\Metadata\OpenApiParameterFilterInterface; |
| 19 | +use ApiPlatform\Metadata\Operation; |
| 20 | +use ApiPlatform\Metadata\Parameter; |
| 21 | +use ApiPlatform\Metadata\ParameterProviderFilterInterface; |
| 22 | +use ApiPlatform\Metadata\PropertiesAwareInterface; |
| 23 | +use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter; |
| 24 | +use ApiPlatform\State\Provider\IriConverterParameterProvider; |
| 25 | +use Doctrine\DBAL\Types\Types; |
| 26 | +use Doctrine\ORM\QueryBuilder; |
| 27 | +use Doctrine\Persistence\ManagerRegistry; |
| 28 | +use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
| 29 | + |
| 30 | +final class IriSearchFilter implements FilterInterface, ManagerRegistryAwareInterface, OpenApiParameterFilterInterface, PropertiesAwareInterface, ParameterProviderFilterInterface |
| 31 | +{ |
| 32 | + use FilterInterfaceTrait; |
| 33 | + |
| 34 | + public function __construct( |
| 35 | + private ?ManagerRegistry $managerRegistry = null, |
| 36 | + private readonly ?array $properties = null, |
| 37 | + private readonly ?NameConverterInterface $nameConverter = null, |
| 38 | + ) { |
| 39 | + } |
| 40 | + |
| 41 | + protected function filterProperty(string $property, mixed $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void |
| 42 | + { |
| 43 | + if ( |
| 44 | + null === $value |
| 45 | + || !$this->isPropertyEnabled($property, $resourceClass) |
| 46 | + || !$this->isPropertyMapped($property, $resourceClass, true) |
| 47 | + ) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + $value = $context['parameter']->getValue(); |
| 52 | + |
| 53 | + $alias = $queryBuilder->getRootAliases()[0]; |
| 54 | + $parameterName = $queryNameGenerator->generateParameterName($property); |
| 55 | + |
| 56 | + $queryBuilder |
| 57 | + ->andWhere(\sprintf('%s.%s = :%s', $alias, $property, $parameterName)) |
| 58 | + ->setParameter($parameterName, $value); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * {@inheritdoc} |
| 63 | + */ |
| 64 | + public function getType(string $doctrineType): string |
| 65 | + { |
| 66 | + // TODO: remove this test when doctrine/dbal:3 support is removed |
| 67 | + if (\defined(Types::class.'::ARRAY') && Types::ARRAY === $doctrineType) { |
| 68 | + return 'array'; |
| 69 | + } |
| 70 | + |
| 71 | + return match ($doctrineType) { |
| 72 | + Types::BIGINT, Types::INTEGER, Types::SMALLINT => 'int', |
| 73 | + Types::BOOLEAN => 'bool', |
| 74 | + Types::DATE_MUTABLE, Types::TIME_MUTABLE, Types::DATETIME_MUTABLE, Types::DATETIMETZ_MUTABLE, Types::DATE_IMMUTABLE, Types::TIME_IMMUTABLE, Types::DATETIME_IMMUTABLE, Types::DATETIMETZ_IMMUTABLE => \DateTimeInterface::class, |
| 75 | + Types::FLOAT => 'float', |
| 76 | + default => 'string', |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + public static function getParameterProvider(): string |
| 81 | + { |
| 82 | + return IriConverterParameterProvider::class; |
| 83 | + } |
| 84 | + |
| 85 | + public function getOpenApiParameters(Parameter $parameter): OpenApiParameter|array|null |
| 86 | + { |
| 87 | + return new OpenApiParameter(name: $parameter->getKey().'[]', in: 'query', style: 'deepObject', explode: true); |
| 88 | + } |
| 89 | +} |
0 commit comments