|
| 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\Common\Filter; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\Parameter; |
| 17 | +use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter; |
| 18 | +use Doctrine\DBAL\Types\Types; |
| 19 | + |
| 20 | +trait IriSearchFilterTrait |
| 21 | +{ |
| 22 | + public function getDescription(string $resourceClass): array |
| 23 | + { |
| 24 | + $description = []; |
| 25 | + |
| 26 | + $properties = $this->getProperties(); |
| 27 | + if (null === $properties) { |
| 28 | + $metadata = $this->getClassMetadata($resourceClass); |
| 29 | + $fieldNames = array_fill_keys($metadata->getFieldNames(), null); |
| 30 | + $associationNames = array_fill_keys($metadata->getAssociationNames(), null); |
| 31 | + |
| 32 | + $properties = array_merge($fieldNames, $associationNames); |
| 33 | + } |
| 34 | + |
| 35 | + foreach ($properties as $property => $strategy) { |
| 36 | + if (!$this->isPropertyMapped($property, $resourceClass, true)) { |
| 37 | + continue; |
| 38 | + } |
| 39 | + |
| 40 | + if ($this->isPropertyNested($property, $resourceClass)) { |
| 41 | + $propertyParts = $this->splitPropertyParts($property, $resourceClass); |
| 42 | + $field = $propertyParts['field']; |
| 43 | + $metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']); |
| 44 | + } else { |
| 45 | + $field = $property; |
| 46 | + $metadata = $this->getClassMetadata($resourceClass); |
| 47 | + } |
| 48 | + |
| 49 | + $propertyName = $this->normalizePropertyName($property); |
| 50 | + if ($metadata->hasField($field)) { |
| 51 | + $typeOfField = $this->getType($metadata->getTypeOfField($field)); |
| 52 | + $filterParameterNames = [$propertyName]; |
| 53 | + |
| 54 | + foreach ($filterParameterNames as $filterParameterName) { |
| 55 | + $description[$filterParameterName] = [ |
| 56 | + 'property' => $propertyName, |
| 57 | + 'type' => $typeOfField, |
| 58 | + 'required' => false, |
| 59 | + 'strategy' => $strategy, |
| 60 | + 'is_collection' => str_ends_with((string) $filterParameterName, '[]'), |
| 61 | + ]; |
| 62 | + } |
| 63 | + } elseif ($metadata->hasAssociation($field)) { |
| 64 | + $filterParameterNames = [ |
| 65 | + $propertyName, |
| 66 | + $propertyName.'[]', |
| 67 | + ]; |
| 68 | + |
| 69 | + foreach ($filterParameterNames as $filterParameterName) { |
| 70 | + $description[$filterParameterName] = [ |
| 71 | + 'property' => $propertyName, |
| 72 | + 'type' => 'string', |
| 73 | + 'required' => false, |
| 74 | + 'strategy' => 'exact', |
| 75 | + 'is_collection' => str_ends_with((string) $filterParameterName, '[]'), |
| 76 | + ]; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return $description; |
| 82 | + } |
| 83 | + |
| 84 | + public function getOpenApiParameters(Parameter $parameter): OpenApiParameter|array|null |
| 85 | + { |
| 86 | + return new OpenApiParameter(name: $parameter->getKey().'[]', in: 'query', style: 'deepObject', explode: true); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * {@inheritdoc} |
| 91 | + */ |
| 92 | + public function getType(string $doctrineType): string |
| 93 | + { |
| 94 | + // TODO: remove this test when doctrine/dbal:3 support is removed |
| 95 | + if (\defined(Types::class.'::ARRAY') && Types::ARRAY === $doctrineType) { |
| 96 | + return 'array'; |
| 97 | + } |
| 98 | + |
| 99 | + return match ($doctrineType) { |
| 100 | + Types::BIGINT, Types::INTEGER, Types::SMALLINT => 'int', |
| 101 | + Types::BOOLEAN => 'bool', |
| 102 | + 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, |
| 103 | + Types::FLOAT => 'float', |
| 104 | + default => 'string', |
| 105 | + }; |
| 106 | + } |
| 107 | +} |
0 commit comments