|
| 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\Odm\Filter; |
| 15 | + |
| 16 | +use ApiPlatform\Doctrine\Common\Filter\OpenApiFilterTrait; |
| 17 | +use ApiPlatform\Doctrine\Common\Filter\OrderFilterInterface; |
| 18 | +use ApiPlatform\Doctrine\Odm\NestedPropertyHelperTrait; |
| 19 | +use ApiPlatform\Metadata\BackwardCompatibleFilterDescriptionTrait; |
| 20 | +use ApiPlatform\Metadata\JsonSchemaFilterInterface; |
| 21 | +use ApiPlatform\Metadata\OpenApiParameterFilterInterface; |
| 22 | +use ApiPlatform\Metadata\Operation; |
| 23 | +use ApiPlatform\Metadata\Parameter; |
| 24 | +use Doctrine\ODM\MongoDB\Aggregation\Builder; |
| 25 | + |
| 26 | +/** |
| 27 | + * Parameter-based order filter for sorting a collection by a property. |
| 28 | + * |
| 29 | + * Unlike {@see OrderFilter}, this filter does not extend AbstractFilter and is designed |
| 30 | + * exclusively for use with Parameters (QueryParameter). |
| 31 | + * |
| 32 | + * Usage: `new QueryParameter(filter: new SortFilter(), property: 'department.name')`. |
| 33 | + * |
| 34 | + * @author Antoine Bluchet <soyuka@gmail.com> |
| 35 | + */ |
| 36 | +final class SortFilter implements FilterInterface, JsonSchemaFilterInterface, OpenApiParameterFilterInterface |
| 37 | +{ |
| 38 | + use BackwardCompatibleFilterDescriptionTrait; |
| 39 | + use NestedPropertyHelperTrait; |
| 40 | + use OpenApiFilterTrait; |
| 41 | + |
| 42 | + public function __construct( |
| 43 | + private readonly ?string $nullsComparison = null, |
| 44 | + ) { |
| 45 | + } |
| 46 | + |
| 47 | + public function apply(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void |
| 48 | + { |
| 49 | + $parameter = $context['parameter'] ?? null; |
| 50 | + if (null === $parameter) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + $value = $parameter->getValue(null); |
| 55 | + if (!\is_string($value)) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + $direction = strtoupper($value); |
| 60 | + if (!\in_array($direction, ['ASC', 'DESC'], true)) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + $property = $parameter->getProperty(); |
| 65 | + $matchField = $this->addNestedParameterLookups($property, $aggregationBuilder, $parameter, true, $context); |
| 66 | + |
| 67 | + $mongoDirection = 'ASC' === $direction ? 1 : -1; |
| 68 | + |
| 69 | + if (null !== $nullsComparison = $this->nullsComparison) { |
| 70 | + $nullsDirection = OrderFilterInterface::NULLS_DIRECTION_MAP[$nullsComparison][$direction] ?? null; |
| 71 | + if (null !== $nullsDirection) { |
| 72 | + $nullRankField = \sprintf('_null_rank_%s', str_replace('.', '_', $matchField)); |
| 73 | + $mongoNullsDirection = 'ASC' === $nullsDirection ? 1 : -1; |
| 74 | + |
| 75 | + $aggregationBuilder->addFields() |
| 76 | + ->field($nullRankField) |
| 77 | + ->cond( |
| 78 | + $aggregationBuilder->expr()->eq('$'.$matchField, null), |
| 79 | + 0, |
| 80 | + 1 |
| 81 | + ); |
| 82 | + |
| 83 | + $context['mongodb_odm_sort_fields'] = ($context['mongodb_odm_sort_fields'] ?? []) + [$nullRankField => $mongoNullsDirection]; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + $aggregationBuilder->sort( |
| 88 | + $context['mongodb_odm_sort_fields'] = ($context['mongodb_odm_sort_fields'] ?? []) + [$matchField => $mongoDirection] |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @return array<string, mixed> |
| 94 | + */ |
| 95 | + public function getSchema(Parameter $parameter): array |
| 96 | + { |
| 97 | + return ['type' => 'string', 'enum' => ['asc', 'desc', 'ASC', 'DESC']]; |
| 98 | + } |
| 99 | +} |
0 commit comments