|
15 | 15 |
|
16 | 16 | use ApiPlatform\Doctrine\Common\Filter\DateFilterInterface; |
17 | 17 | use ApiPlatform\Doctrine\Common\Filter\DateFilterTrait; |
| 18 | +use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface; |
| 19 | +use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareTrait; |
| 20 | +use ApiPlatform\Doctrine\Common\Filter\NameConverterAwareInterface; |
| 21 | +use ApiPlatform\Doctrine\Common\Filter\NameConverterAwareTrait; |
| 22 | +use ApiPlatform\Doctrine\Common\Filter\PropertyAwareFilterInterface; |
| 23 | +use ApiPlatform\Doctrine\Common\Filter\PropertyAwareFilterTrait; |
| 24 | +use ApiPlatform\Doctrine\Odm\PropertyHelperTrait as MongoDbOdmPropertyHelperTrait; |
18 | 25 | use ApiPlatform\Metadata\Exception\InvalidArgumentException; |
19 | 26 | use ApiPlatform\Metadata\JsonSchemaFilterInterface; |
20 | 27 | use ApiPlatform\Metadata\OpenApiParameterFilterInterface; |
|
23 | 30 | use ApiPlatform\Metadata\QueryParameter; |
24 | 31 | use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter; |
25 | 32 | use Doctrine\ODM\MongoDB\Aggregation\Builder; |
| 33 | +use Doctrine\Persistence\ManagerRegistry; |
| 34 | +use Psr\Log\LoggerInterface; |
| 35 | +use Psr\Log\NullLogger; |
| 36 | +use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
26 | 37 |
|
27 | 38 | /** |
28 | 39 | * The date filter allows to filter a collection by date intervals. |
|
120 | 131 | * @author Kévin Dunglas <dunglas@gmail.com> |
121 | 132 | * @author Théo FIDRY <theo.fidry@gmail.com> |
122 | 133 | * @author Alan Poulain <contact@alanpoulain.eu> |
123 | | - * |
124 | | - * @deprecated since API Platform 4.4: extending {@see AbstractFilter} is deprecated. In 5.0 this filter is rewritten as a standalone overlay over {@see ComparisonFilter} (translating the `[before]`/`[strictly_before]`/`[after]`/`[strictly_after]` syntax) — same class name, same URL syntax, drop-in. Declare it through a QueryParameter to migrate. |
125 | 134 | */ |
126 | | -final class DateFilter extends AbstractFilter implements DateFilterInterface, JsonSchemaFilterInterface, OpenApiParameterFilterInterface |
| 135 | +final class DateFilter implements DateFilterInterface, FilterInterface, JsonSchemaFilterInterface, ManagerRegistryAwareInterface, NameConverterAwareInterface, OpenApiParameterFilterInterface, PropertyAwareFilterInterface |
127 | 136 | { |
128 | 137 | use DateFilterTrait; |
| 138 | + use ManagerRegistryAwareTrait; |
| 139 | + use MongoDbOdmPropertyHelperTrait; |
| 140 | + use NameConverterAwareTrait; |
| 141 | + use PropertyAwareFilterTrait; |
129 | 142 |
|
130 | 143 | public const DOCTRINE_DATE_TYPES = [ |
131 | 144 | 'date' => true, |
132 | 145 | 'date_immutable' => true, |
133 | 146 | ]; |
134 | 147 |
|
| 148 | + private LoggerInterface $logger; |
| 149 | + |
| 150 | + /** |
| 151 | + * @param array<string, mixed>|null $properties |
| 152 | + */ |
| 153 | + public function __construct(?ManagerRegistry $managerRegistry = null, ?LoggerInterface $logger = null, ?array $properties = null, ?NameConverterInterface $nameConverter = null) |
| 154 | + { |
| 155 | + $this->managerRegistry = $managerRegistry; |
| 156 | + $this->logger = $logger ?? new NullLogger(); |
| 157 | + $this->properties = $properties; |
| 158 | + $this->nameConverter = $nameConverter; |
| 159 | + } |
| 160 | + |
| 161 | + public function apply(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void |
| 162 | + { |
| 163 | + foreach ($context['filters'] ?? [] as $property => $value) { |
| 164 | + $this->filterProperty($this->denormalizePropertyName($property), $value, $aggregationBuilder, $resourceClass, $operation, $context); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + protected function getLogger(): LoggerInterface |
| 169 | + { |
| 170 | + return $this->logger; |
| 171 | + } |
| 172 | + |
| 173 | + protected function isPropertyEnabled(string $property, string $resourceClass): bool |
| 174 | + { |
| 175 | + if (null === $this->properties) { |
| 176 | + // to ensure sanity, nested properties must still be explicitly enabled |
| 177 | + return !$this->isPropertyNested($property, $resourceClass); |
| 178 | + } |
| 179 | + |
| 180 | + return \array_key_exists($property, $this->properties); |
| 181 | + } |
| 182 | + |
135 | 183 | /** |
136 | | - * {@inheritdoc} |
| 184 | + * @param array<string, mixed> $context |
| 185 | + * |
| 186 | + * @param-out array<string, mixed> $context |
137 | 187 | */ |
138 | 188 | protected function filterProperty(string $property, mixed $value, Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void |
139 | 189 | { |
|
0 commit comments