|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Api\Filter\ElasticSearch; |
| 4 | + |
| 5 | +use ApiPlatform\Elasticsearch\Filter\AbstractFilter; |
| 6 | +use ApiPlatform\Metadata\Operation; |
| 7 | +use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
| 8 | +use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; |
| 9 | +use ApiPlatform\Metadata\ResourceClassResolverInterface; |
| 10 | +use App\Model\DateFilterConfig; |
| 11 | +use App\Model\DateLimit; |
| 12 | +use Symfony\Component\PropertyInfo\Type; |
| 13 | +use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
| 14 | + |
| 15 | +/** |
| 16 | + * DateRangeFilter allows for defining filters on datetime fields with operators e.g. |
| 17 | + * - startDate[gt]=2004-02-12T15:19:21+00:00 |
| 18 | + * - startDate[between]=2004-02-12T15:19:21+00:00..2004-03-12T15:19:21+00:00. |
| 19 | + * |
| 20 | + * @see ApiPlatform\Doctrine\Orm\Filter\RangeFilter |
| 21 | + */ |
| 22 | +final class DateRangeFilter extends AbstractFilter |
| 23 | +{ |
| 24 | + /** @var DateFilterConfig[] */ |
| 25 | + private array $config; |
| 26 | + |
| 27 | + public function __construct( |
| 28 | + protected PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, |
| 29 | + PropertyMetadataFactoryInterface $propertyMetadataFactory, |
| 30 | + ResourceClassResolverInterface $resourceClassResolver, |
| 31 | + protected ?NameConverterInterface $nameConverter = null, |
| 32 | + protected ?array $properties = null, |
| 33 | + array $config = [], |
| 34 | + ) { |
| 35 | + parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $resourceClassResolver, $nameConverter, $properties); |
| 36 | + |
| 37 | + // Convert config to DateFilterConfig objects for better type safety. |
| 38 | + foreach ($config as $field => $values) { |
| 39 | + $this->config[$field] = new DateFilterConfig( |
| 40 | + $values['limit'], |
| 41 | + $values['throwOnInvalid'] |
| 42 | + ); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public function apply(array $clauseBody, string $resourceClass, ?Operation $operation = null, array $context = []): array |
| 47 | + { |
| 48 | + $ranges = []; |
| 49 | + |
| 50 | + if (!$this->properties) { |
| 51 | + return $ranges; |
| 52 | + } |
| 53 | + |
| 54 | + foreach ($this->properties as $property => $value) { |
| 55 | + if (!empty($context['filters'][$property])) { |
| 56 | + $ranges[] = $this->getElasticSearchQueryRanges($property, $context['filters'][$property]); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return isset($ranges[1]) ? $ranges : $ranges[0] ?? $ranges; |
| 61 | + } |
| 62 | + |
| 63 | + public function getDescription(string $resourceClass): array |
| 64 | + { |
| 65 | + if (!$this->properties) { |
| 66 | + return []; |
| 67 | + } |
| 68 | + |
| 69 | + $description = []; |
| 70 | + |
| 71 | + foreach ($this->properties as $property => $value) { |
| 72 | + $description += $this->getFilterDescription($property, $this->config[$value]->limit, true); |
| 73 | + $description += $this->getFilterDescription($property, DateLimit::between); |
| 74 | + $description += $this->getFilterDescription($property, DateLimit::gt); |
| 75 | + $description += $this->getFilterDescription($property, DateLimit::gte); |
| 76 | + $description += $this->getFilterDescription($property, DateLimit::lt); |
| 77 | + $description += $this->getFilterDescription($property, DateLimit::lte); |
| 78 | + } |
| 79 | + |
| 80 | + return $description; |
| 81 | + } |
| 82 | + |
| 83 | + private function getElasticSearchQueryRanges($property, $filter): array |
| 84 | + { |
| 85 | + if (!\is_array($filter)) { |
| 86 | + $operator = $this->config[$property]->limit; |
| 87 | + $value = $filter; |
| 88 | + } else { |
| 89 | + $operator = DateLimit::{array_key_first($filter)}; |
| 90 | + $value = array_shift($filter); |
| 91 | + } |
| 92 | + |
| 93 | + switch ($operator) { |
| 94 | + case DateLimit::between: |
| 95 | + $values = explode('..', $value); |
| 96 | + |
| 97 | + if (count($values) !== 2) { |
| 98 | + throw new \InvalidArgumentException('Invalid date range'); |
| 99 | + } |
| 100 | + |
| 101 | + return [ |
| 102 | + 'range' => [ |
| 103 | + $property => [ |
| 104 | + DateLimit::gt->name => $values[0], |
| 105 | + DateLimit::lt->name => $values[1], |
| 106 | + ], |
| 107 | + ], |
| 108 | + ]; |
| 109 | + case DateLimit::gt: |
| 110 | + case DateLimit::gte: |
| 111 | + case DateLimit::lt: |
| 112 | + case DateLimit::lte: |
| 113 | + return [ |
| 114 | + 'range' => [ |
| 115 | + $property => [ |
| 116 | + $operator->name => $value, |
| 117 | + ], |
| 118 | + ], |
| 119 | + ]; |
| 120 | + default: |
| 121 | + return []; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private function getFilterDescription(string $fieldName, DateLimit $operator, bool $isDefault = false): array |
| 126 | + { |
| 127 | + $propertyName = $this->normalizePropertyName($fieldName); |
| 128 | + $key = $this->getFilterDescriptionKey($propertyName, $operator, $isDefault); |
| 129 | + |
| 130 | + return [ |
| 131 | + $key => [ |
| 132 | + 'property' => $propertyName, |
| 133 | + 'type' => 'string', |
| 134 | + 'required' => false, |
| 135 | + 'description' => $this->getFilterDescriptionBody($propertyName, $operator, $isDefault), |
| 136 | + ], |
| 137 | + ]; |
| 138 | + } |
| 139 | + |
| 140 | + private function getFilterDescriptionKey(string $propertyName, DateLimit $operator, bool $isDefault = false): string |
| 141 | + { |
| 142 | + return $isDefault ? $propertyName : $propertyName.'['.$operator->name.']'; |
| 143 | + } |
| 144 | + |
| 145 | + private function getFilterDescriptionBody(string $propertyName, DateLimit $operator, bool $isDeprecated = false): string |
| 146 | + { |
| 147 | + $deprecatedBody = $isDeprecated ? sprintf(' (DEPRECATED - please use a filter with an explicit operator, e.g. %s[gt]=2004-02-12T15:19:21+00:00) ', $propertyName) : ''; |
| 148 | + |
| 149 | + return match ($operator) { |
| 150 | + DateLimit::between => sprintf('Filter based on %s %s two ISO 8601 datetime (yyyy-MM-dd\'T\'HH:mm:ssz) seperated by \'..\', e.g. "2004-02-12T15:19:21+00:00..2004-02-13T16:20:22+00:00"', $propertyName, $operator->value), |
| 151 | + default => sprintf('Filter based on %s %s ISO 8601 datetime (yyyy-MM-dd\'T\'HH:mm:ssz), e.g. "2004-02-12T15:19:21+00:00"%s', $propertyName, $operator->value, $deprecatedBody), |
| 152 | + }; |
| 153 | + } |
| 154 | + |
| 155 | + private function normalizePropertyName(string $property): string |
| 156 | + { |
| 157 | + if (!$this->nameConverter instanceof NameConverterInterface) { |
| 158 | + return $property; |
| 159 | + } |
| 160 | + |
| 161 | + return implode('.', array_map($this->nameConverter->normalize(...), explode('.', $property))); |
| 162 | + } |
| 163 | +} |
0 commit comments