|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 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\Orm\Util\QueryNameGeneratorInterface; |
| 17 | +use ApiPlatform\Metadata\OpenApiParameterFilterInterface; |
| 18 | +use ApiPlatform\Metadata\Operation; |
| 19 | +use ApiPlatform\Metadata\Parameter; |
| 20 | +use ApiPlatform\Metadata\ParameterProviderFilterInterface; |
| 21 | +use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter; |
| 22 | +use ApiPlatform\State\Provider\IriConverterParameterProvider; |
| 23 | +use Doctrine\ORM\QueryBuilder; |
| 24 | + |
| 25 | +class IriFilter implements FilterInterface, OpenApiParameterFilterInterface, ParameterProviderFilterInterface |
| 26 | +{ |
| 27 | + public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void |
| 28 | + { |
| 29 | + if (!$parameter = $context['parameter'] ?? null) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + $value = $parameter->getValue(); |
| 34 | + if (!\is_array($value)) { |
| 35 | + $value = [$value]; |
| 36 | + } |
| 37 | + |
| 38 | + $property = $parameter->getProperty(); |
| 39 | + $alias = $queryBuilder->getRootAliases()[0]; |
| 40 | + $parameterName = $queryNameGenerator->generateParameterName($property); |
| 41 | + |
| 42 | + $queryBuilder |
| 43 | + ->join(\sprintf('%s.%s', $alias, $property), $parameterName) |
| 44 | + ->andWhere(\sprintf('%s IN(:%s)', $parameterName, $parameterName)) |
| 45 | + ->setParameter($parameterName, $value); |
| 46 | + } |
| 47 | + |
| 48 | + public static function getParameterProvider(): string |
| 49 | + { |
| 50 | + return IriConverterParameterProvider::class; |
| 51 | + } |
| 52 | + |
| 53 | + public function getOpenApiParameters(Parameter $parameter): OpenApiParameter|array|null |
| 54 | + { |
| 55 | + return new OpenApiParameter(name: $parameter->getKey().'[]', in: 'query', style: 'deepObject', explode: true); |
| 56 | + } |
| 57 | + |
| 58 | + public function getDescription(string $resourceClass): array |
| 59 | + { |
| 60 | + return []; |
| 61 | + } |
| 62 | +} |
0 commit comments