|
| 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\LoggerAwareInterface; |
| 17 | +use ApiPlatform\Doctrine\Common\Filter\LoggerAwareTrait; |
| 18 | +use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface; |
| 19 | +use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareTrait; |
| 20 | +use ApiPlatform\Doctrine\Common\PropertyHelperTrait; |
| 21 | +use ApiPlatform\Doctrine\Orm\PropertyHelperTrait as OrmPropertyHelperTrait; |
| 22 | +use ApiPlatform\Doctrine\Orm\Util\QueryBuilderHelper; |
| 23 | +use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface; |
| 24 | +use ApiPlatform\Metadata\BackwardCompatibleFilterDescriptionTrait; |
| 25 | +use ApiPlatform\Metadata\Exception\InvalidArgumentException; |
| 26 | +use ApiPlatform\Metadata\JsonSchemaFilterInterface; |
| 27 | +use ApiPlatform\Metadata\OpenApiParameterFilterInterface; |
| 28 | +use ApiPlatform\Metadata\Operation; |
| 29 | +use ApiPlatform\Metadata\Parameter; |
| 30 | +use ApiPlatform\Metadata\QueryParameter; |
| 31 | +use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter; |
| 32 | +use Doctrine\DBAL\ArrayParameterType; |
| 33 | +use Doctrine\DBAL\ParameterType; |
| 34 | +use Doctrine\DBAL\Types\ConversionException; |
| 35 | +use Doctrine\DBAL\Types\Type; |
| 36 | +use Doctrine\ORM\Query\Expr\Join; |
| 37 | +use Doctrine\ORM\QueryBuilder; |
| 38 | + |
| 39 | +/** |
| 40 | + * @internal |
| 41 | + */ |
| 42 | +class AbstractUuidFilter implements FilterInterface, ManagerRegistryAwareInterface, JsonSchemaFilterInterface, OpenApiParameterFilterInterface, LoggerAwareInterface |
| 43 | +{ |
| 44 | + use BackwardCompatibleFilterDescriptionTrait; |
| 45 | + use LoggerAwareTrait; |
| 46 | + use ManagerRegistryAwareTrait; |
| 47 | + use OrmPropertyHelperTrait; |
| 48 | + use PropertyHelperTrait; |
| 49 | + |
| 50 | + private const UUID_SCHEMA = [ |
| 51 | + 'type' => 'string', |
| 52 | + 'format' => 'uuid', |
| 53 | + ]; |
| 54 | + |
| 55 | + public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void |
| 56 | + { |
| 57 | + $parameter = $context['parameter'] ?? null; |
| 58 | + if (!$parameter) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + $this->filterProperty($parameter->getProperty(), $parameter->getValue(), $queryBuilder, $queryNameGenerator, $resourceClass, $operation, $context); |
| 63 | + } |
| 64 | + |
| 65 | + private function filterProperty(string $property, mixed $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void |
| 66 | + { |
| 67 | + $alias = $queryBuilder->getRootAliases()[0]; |
| 68 | + $field = $property; |
| 69 | + |
| 70 | + $associations = []; |
| 71 | + if ($this->isPropertyNested($property, $resourceClass)) { |
| 72 | + [$alias, $field, $associations] = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass, Join::INNER_JOIN); |
| 73 | + } |
| 74 | + |
| 75 | + $metadata = $this->getNestedMetadata($resourceClass, $associations); |
| 76 | + |
| 77 | + if ($metadata->hasField($field)) { |
| 78 | + $value = $this->convertValuesToTheDatabaseRepresentationIfNecessary($queryBuilder, $this->getDoctrineFieldType($property, $resourceClass), $value); |
| 79 | + $this->addWhere($queryBuilder, $queryNameGenerator, $alias, $field, $value); |
| 80 | + |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + // metadata doesn't have the field, nor an association on the field |
| 85 | + if (!$metadata->hasAssociation($field)) { |
| 86 | + $this->logger->notice('Tried to filter on a non-existent field or association', [ |
| 87 | + 'field' => $field, |
| 88 | + 'resource_class' => $resourceClass, |
| 89 | + 'exception' => new InvalidArgumentException(\sprintf('Property "%s" does not exist in resource "%s".', $field, $resourceClass)), |
| 90 | + ]); |
| 91 | + |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + // association, let's fetch the entity (or reference to it) if we can so we can make sure we get its orm id |
| 96 | + $associationResourceClass = $metadata->getAssociationTargetClass($field); |
| 97 | + $associationMetadata = $this->getClassMetadata($associationResourceClass); |
| 98 | + $associationFieldIdentifier = $associationMetadata->getIdentifierFieldNames()[0]; |
| 99 | + $doctrineTypeField = $this->getDoctrineFieldType($associationFieldIdentifier, $associationResourceClass); |
| 100 | + |
| 101 | + $associationAlias = $alias; |
| 102 | + $associationField = $field; |
| 103 | + |
| 104 | + if ($metadata->isCollectionValuedAssociation($associationField) || $metadata->isAssociationInverseSide($field)) { |
| 105 | + $associationAlias = QueryBuilderHelper::addJoinOnce($queryBuilder, $queryNameGenerator, $alias, $associationField); |
| 106 | + $associationField = $associationFieldIdentifier; |
| 107 | + } |
| 108 | + |
| 109 | + $value = $this->convertValuesToTheDatabaseRepresentationIfNecessary($queryBuilder, $doctrineTypeField, $value); |
| 110 | + $this->addWhere($queryBuilder, $queryNameGenerator, $associationAlias, $associationField, $value); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Converts values to their database representation. |
| 115 | + */ |
| 116 | + private function convertValuesToTheDatabaseRepresentationIfNecessary(QueryBuilder $queryBuilder, ?string $doctrineFieldType, mixed $value): mixed |
| 117 | + { |
| 118 | + if (null === $doctrineFieldType || !Type::hasType($doctrineFieldType)) { |
| 119 | + throw new InvalidArgumentException(\sprintf('The Doctrine type "%s" is not valid or not registered.', $doctrineFieldType)); |
| 120 | + } |
| 121 | + |
| 122 | + $doctrineType = Type::getType($doctrineFieldType); |
| 123 | + $platform = $queryBuilder->getEntityManager()->getConnection()->getDatabasePlatform(); |
| 124 | + |
| 125 | + if (\is_array($value)) { |
| 126 | + $databaseValues = []; |
| 127 | + foreach ($value as $val) { |
| 128 | + try { |
| 129 | + $databaseValues[] = $doctrineType->convertToDatabaseValue($val, $platform); |
| 130 | + } catch (ConversionException $e) { |
| 131 | + $this->logger->notice('Invalid value conversion to database representation', [ |
| 132 | + 'exception' => $e, |
| 133 | + ]); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return $databaseValues; |
| 138 | + } |
| 139 | + |
| 140 | + try { |
| 141 | + return $doctrineType->convertToDatabaseValue($value, $platform); |
| 142 | + } catch (ConversionException $e) { |
| 143 | + $this->logger->notice('Invalid value conversion to database representation', [ |
| 144 | + 'exception' => $e, |
| 145 | + ]); |
| 146 | + |
| 147 | + return null; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Adds where clause. |
| 153 | + */ |
| 154 | + private function addWhere(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, mixed $value): void |
| 155 | + { |
| 156 | + $valueParameter = ':'.$queryNameGenerator->generateParameterName($field); |
| 157 | + $aliasedField = \sprintf('%s.%s', $alias, $field); |
| 158 | + |
| 159 | + if (!\is_array($value)) { |
| 160 | + $queryBuilder |
| 161 | + ->andWhere($queryBuilder->expr()->eq($aliasedField, $valueParameter)) |
| 162 | + ->setParameter($valueParameter, $value, $this->getDoctrineParameterType()); |
| 163 | + |
| 164 | + return; |
| 165 | + } |
| 166 | + |
| 167 | + $queryBuilder |
| 168 | + ->andWhere($queryBuilder->expr()->in($aliasedField, $valueParameter)) |
| 169 | + ->setParameter($valueParameter, $value, $this->getDoctrineArrayParameterType()); |
| 170 | + } |
| 171 | + |
| 172 | + protected function getDoctrineParameterType(): ?ParameterType |
| 173 | + { |
| 174 | + return null; |
| 175 | + } |
| 176 | + |
| 177 | + protected function getDoctrineArrayParameterType(): ?ArrayParameterType |
| 178 | + { |
| 179 | + return null; |
| 180 | + } |
| 181 | + |
| 182 | + public function getOpenApiParameters(Parameter $parameter): array |
| 183 | + { |
| 184 | + $in = $parameter instanceof QueryParameter ? 'query' : 'header'; |
| 185 | + $key = $parameter->getKey(); |
| 186 | + |
| 187 | + return [ |
| 188 | + new OpenApiParameter( |
| 189 | + name: $key, |
| 190 | + in: $in, |
| 191 | + schema: self::UUID_SCHEMA, |
| 192 | + style: 'form', |
| 193 | + explode: false |
| 194 | + ), |
| 195 | + new OpenApiParameter( |
| 196 | + name: $key.'[]', |
| 197 | + in: $in, |
| 198 | + description: 'One or more Uuids', |
| 199 | + schema: [ |
| 200 | + 'type' => 'array', |
| 201 | + 'items' => self::UUID_SCHEMA, |
| 202 | + ], |
| 203 | + style: 'deepObject', |
| 204 | + explode: true |
| 205 | + ), |
| 206 | + ]; |
| 207 | + } |
| 208 | + |
| 209 | + public function getSchema(Parameter $parameter): array |
| 210 | + { |
| 211 | + return self::UUID_SCHEMA; |
| 212 | + } |
| 213 | +} |
0 commit comments