From a1fa4ecd8f1021bafe8bbb34523a17c15d95f66e Mon Sep 17 00:00:00 2001 From: Tomas Hermanek Date: Wed, 14 Aug 2024 13:07:07 +0200 Subject: [PATCH] Add custom ordering functionality. --- src/ApiFilter/ApiQuery.php | 6 ++++++ src/ApiFilter/CustomOrderInterface.php | 12 ++++++++++++ src/Repository/AbstractAnzuRepository.php | 9 ++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/ApiFilter/CustomOrderInterface.php diff --git a/src/ApiFilter/ApiQuery.php b/src/ApiFilter/ApiQuery.php index 4d67297..90662a0 100644 --- a/src/ApiFilter/ApiQuery.php +++ b/src/ApiFilter/ApiQuery.php @@ -22,6 +22,7 @@ class ApiQuery /** * @param list $customFilters + * @param list $customOrders * * @throws ORMException */ @@ -32,6 +33,7 @@ public function __construct( #[Deprecated] protected ?CustomFilterInterface $customFilter = null, protected bool $fetchOneAdditionalRecord = false, protected array $customFilters = [], + protected array $customOrders = [], ) { if ($this->customFilter instanceof CustomFilterInterface && empty($this->customFilters)) { $this->customFilters = [$this->customFilter]; @@ -84,6 +86,10 @@ private function applyOrders(): void foreach ($this->apiParams->getOrder() as $field => $direction) { $this->dqb->addOrderBy('t.' . $field, $direction); } + + foreach ($this->customOrders as $customOrder) { + $customOrder->apply($this->dqb, $this->apiParams); + } } /** diff --git a/src/ApiFilter/CustomOrderInterface.php b/src/ApiFilter/CustomOrderInterface.php new file mode 100644 index 0000000..ef61db3 --- /dev/null +++ b/src/ApiFilter/CustomOrderInterface.php @@ -0,0 +1,12 @@ + $customFilters + * @param list $customOrders * * @throws ORMException */ @@ -71,6 +73,7 @@ public function findByApiParams( ApiParams $apiParams, #[Deprecated] ?CustomFilterInterface $customFilter = null, array $customFilters = [], + array $customOrders = [], ?Closure $mapDataFn = null, ): ApiResponseList { if ($customFilter instanceof CustomFilterInterface && empty($customFilters)) { @@ -81,7 +84,8 @@ public function findByApiParams( entityManager: $this->getEntityManager(), metadata: $this->getClassMetadata(), apiParams: $apiParams, - customFilters: $customFilters + customFilters: $customFilters, + customOrders: $customOrders, ); $data = $apiQuery->getData(); @@ -104,6 +108,7 @@ public function findByApiParams( /** * @param list $customFilters + * @param list $customOrders * @param ?CustomFilterInterface $customFilter Deprecated * * @throws ORMException @@ -112,6 +117,7 @@ public function findByApiParamsWithInfiniteListing( ApiParams $apiParams, #[Deprecated] ?CustomFilterInterface $customFilter = null, array $customFilters = [], + array $customOrders = [], ?Closure $mapDataFn = null, ): ApiInfiniteResponseList { if ($customFilter instanceof CustomFilterInterface && empty($customFilters)) { @@ -124,6 +130,7 @@ public function findByApiParamsWithInfiniteListing( apiParams: $apiParams, fetchOneAdditionalRecord: true, customFilters: $customFilters, + customOrders: $customOrders, ); $data = $apiQuery->getData(); $totalCount = $apiParams->getLimit() + $apiParams->getOffset() + 1;