|
13 | 13 |
|
14 | 14 | namespace ApiPlatform\Core\Bridge\Elasticsearch\DataProvider\Extension;
|
15 | 15 |
|
16 |
| -class_exists(\ApiPlatform\Elasticsearch\Extension\SortExtension::class); |
| 16 | +use ApiPlatform\Core\Api\ResourceClassResolverInterface; |
| 17 | +use ApiPlatform\Core\Bridge\Elasticsearch\Api\IdentifierExtractorInterface; |
| 18 | +use ApiPlatform\Core\Bridge\Elasticsearch\Util\FieldDatatypeTrait; |
| 19 | +use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
| 20 | +use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
| 21 | +use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
17 | 22 |
|
18 |
| -if (false) { |
19 |
| - final class SortExtension extends \ApiPlatform\Elasticsearch\Extension\SortExtension |
| 23 | +/** |
| 24 | + * Applies selected sorting while querying resource collection. |
| 25 | + * |
| 26 | + * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html |
| 27 | + * |
| 28 | + * @experimental |
| 29 | + * |
| 30 | + * @author Baptiste Meyer <[email protected]> |
| 31 | + */ |
| 32 | +final class SortExtension implements RequestBodySearchCollectionExtensionInterface |
| 33 | +{ |
| 34 | + use FieldDatatypeTrait; |
| 35 | + |
| 36 | + private $defaultDirection; |
| 37 | + private $identifierExtractor; |
| 38 | + private $resourceMetadataFactory; |
| 39 | + private $nameConverter; |
| 40 | + |
| 41 | + public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, IdentifierExtractorInterface $identifierExtractor, PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, ?NameConverterInterface $nameConverter = null, ?string $defaultDirection = null) |
| 42 | + { |
| 43 | + $this->resourceMetadataFactory = $resourceMetadataFactory; |
| 44 | + $this->identifierExtractor = $identifierExtractor; |
| 45 | + $this->propertyMetadataFactory = $propertyMetadataFactory; |
| 46 | + $this->resourceClassResolver = $resourceClassResolver; |
| 47 | + $this->nameConverter = $nameConverter; |
| 48 | + $this->defaultDirection = $defaultDirection; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * {@inheritdoc} |
| 53 | + */ |
| 54 | + public function applyToCollection(array $requestBody, string $resourceClass, ?string $operationName = null, array $context = []): array |
| 55 | + { |
| 56 | + $orders = []; |
| 57 | + |
| 58 | + if ( |
| 59 | + null !== ($defaultOrder = $this->resourceMetadataFactory->create($resourceClass)->getAttribute('order')) |
| 60 | + && \is_array($defaultOrder) |
| 61 | + ) { |
| 62 | + foreach ($defaultOrder as $property => $direction) { |
| 63 | + if (\is_int($property)) { |
| 64 | + $property = $direction; |
| 65 | + $direction = 'asc'; |
| 66 | + } |
| 67 | + |
| 68 | + $orders[] = $this->getOrder($resourceClass, $property, $direction); |
| 69 | + } |
| 70 | + } elseif (null !== $this->defaultDirection) { |
| 71 | + $orders[] = $this->getOrder( |
| 72 | + $resourceClass, |
| 73 | + $this->identifierExtractor->getIdentifierFromResourceClass($resourceClass), |
| 74 | + $this->defaultDirection |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + if (!$orders) { |
| 79 | + return $requestBody; |
| 80 | + } |
| 81 | + |
| 82 | + $requestBody['sort'] = array_merge_recursive($requestBody['sort'] ?? [], $orders); |
| 83 | + |
| 84 | + return $requestBody; |
| 85 | + } |
| 86 | + |
| 87 | + private function getOrder(string $resourceClass, string $property, string $direction): array |
20 | 88 | {
|
| 89 | + $order = ['order' => strtolower($direction)]; |
| 90 | + |
| 91 | + if (null !== $nestedPath = $this->getNestedFieldPath($resourceClass, $property)) { |
| 92 | + $nestedPath = null === $this->nameConverter ? $nestedPath : $this->nameConverter->normalize($nestedPath, $resourceClass); |
| 93 | + $order['nested'] = ['path' => $nestedPath]; |
| 94 | + } |
| 95 | + |
| 96 | + $property = null === $this->nameConverter ? $property : $this->nameConverter->normalize($property, $resourceClass); |
| 97 | + |
| 98 | + return [$property => $order]; |
21 | 99 | }
|
22 | 100 | }
|
0 commit comments