|
| 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\IriSearchFilterTrait; |
| 17 | +use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface; |
| 18 | +use ApiPlatform\Metadata\OpenApiParameterFilterInterface; |
| 19 | +use ApiPlatform\Metadata\Operation; |
| 20 | +use Doctrine\DBAL\Types\Types; |
| 21 | +use Doctrine\ORM\QueryBuilder; |
| 22 | +use Doctrine\Persistence\ManagerRegistry; |
| 23 | +use Psr\Log\LoggerInterface; |
| 24 | +use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
| 25 | + |
| 26 | +class IriSearchFilter extends AbstractFilter implements OpenApiParameterFilterInterface |
| 27 | +{ |
| 28 | + use IriSearchFilterTrait; |
| 29 | + |
| 30 | + public function __construct( |
| 31 | + ?ManagerRegistry $managerRegistry = null, |
| 32 | + ?LoggerInterface $logger = null, |
| 33 | + ?array $properties = null, |
| 34 | + ?NameConverterInterface $nameConverter = null, |
| 35 | + ) { |
| 36 | + parent::__construct($managerRegistry, $logger, $properties, $nameConverter); |
| 37 | + } |
| 38 | + |
| 39 | + protected function filterProperty(string $property, mixed $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void |
| 40 | + { |
| 41 | + if ( |
| 42 | + null === $value |
| 43 | + || !$this->isPropertyEnabled($property, $resourceClass) |
| 44 | + || !$this->isPropertyMapped($property, $resourceClass, true) |
| 45 | + ) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + $extraProperties = $operation?->getExtraProperties(); |
| 50 | + $resource = $extraProperties['_value'] ?? null; |
| 51 | + if (!$resource) { |
| 52 | + $this->logger->warning(\sprintf('No resource found for property "%s".', $property)); |
| 53 | + |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + $alias = $queryBuilder->getRootAliases()[0]; |
| 58 | + $parameterName = $queryNameGenerator->generateParameterName($property); |
| 59 | + |
| 60 | + $queryBuilder |
| 61 | + ->andWhere(\sprintf('%s.%s = :%s', $alias, $property, $parameterName)) |
| 62 | + ->setParameter($parameterName, $resource->getId()); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * {@inheritdoc} |
| 67 | + */ |
| 68 | + public function getType(string $doctrineType): string |
| 69 | + { |
| 70 | + // TODO: remove this test when doctrine/dbal:3 support is removed |
| 71 | + if (\defined(Types::class.'::ARRAY') && Types::ARRAY === $doctrineType) { |
| 72 | + return 'array'; |
| 73 | + } |
| 74 | + |
| 75 | + return match ($doctrineType) { |
| 76 | + Types::BIGINT, Types::INTEGER, Types::SMALLINT => 'int', |
| 77 | + Types::BOOLEAN => 'bool', |
| 78 | + 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, |
| 79 | + Types::FLOAT => 'float', |
| 80 | + default => 'string', |
| 81 | + }; |
| 82 | + } |
| 83 | +} |
0 commit comments