From db281fa9568be29b937f3b1f0095e2ae75b5753a Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Fri, 19 Jul 2024 09:07:09 +0200 Subject: [PATCH 1/4] IBX-8597: Added BaseSortClauseProcessor https://issues.ibexa.co/browse/IBX-8597 --- src/bundle/Resources/config/services.yml | 5 ++ .../SortClause/BaseSortClauseProcessor.php | 57 +++++++++++++++++++ .../SortClauseProcessorInterface.php | 24 ++++++++ 3 files changed, 86 insertions(+) create mode 100644 src/contracts/Input/Parser/Query/SortClause/BaseSortClauseProcessor.php create mode 100644 src/contracts/Input/Parser/Query/SortClause/SortClauseProcessorInterface.php diff --git a/src/bundle/Resources/config/services.yml b/src/bundle/Resources/config/services.yml index 7cf7853eb..b07d3e764 100644 --- a/src/bundle/Resources/config/services.yml +++ b/src/bundle/Resources/config/services.yml @@ -395,3 +395,8 @@ services: abstract: true arguments: $parsingDispatcher: '@Ibexa\Contracts\Rest\Input\ParsingDispatcher' + + Ibexa\Contracts\Rest\Input\Parser\Query\SortClause\BaseSortClauseProcessor: + abstract: true + arguments: + $parsingDispatcher: '@Ibexa\Contracts\Rest\Input\ParsingDispatcher' diff --git a/src/contracts/Input/Parser/Query/SortClause/BaseSortClauseProcessor.php b/src/contracts/Input/Parser/Query/SortClause/BaseSortClauseProcessor.php new file mode 100644 index 000000000..e3e086a51 --- /dev/null +++ b/src/contracts/Input/Parser/Query/SortClause/BaseSortClauseProcessor.php @@ -0,0 +1,57 @@ +parsingDispatcher = $parsingDispatcher; + } + + public function processSortClauses(array $sortClauseData): Traversable + { + if (empty($sortClauseData)) { + yield from []; + } + + foreach ($sortClauseData as $sortClauseName => $direction) { + $mediaType = $this->getSortClauseMediaType($sortClauseName); + + try { + yield $this->parsingDispatcher->parse([$sortClauseName => $direction], $mediaType); + } catch (Exceptions\Parser $e) { + throw new Exceptions\Parser($this->getParserInvalidSortClauseMessage($sortClauseName), 0, $e); + } + } + } + + abstract protected function getMediaTypePrefix(): string; + + abstract protected function getParserInvalidSortClauseMessage(string $sortClauseName): string; + + private function getSortClauseMediaType(string $sortClauseName): string + { + $mediaTypePrefix = $this->getMediaTypePrefix(); + if ('.' !== substr($mediaTypePrefix, strlen($mediaTypePrefix) - 1)) { + $mediaTypePrefix .= '.'; + } + + return $mediaTypePrefix . $sortClauseName; + } +} diff --git a/src/contracts/Input/Parser/Query/SortClause/SortClauseProcessorInterface.php b/src/contracts/Input/Parser/Query/SortClause/SortClauseProcessorInterface.php new file mode 100644 index 000000000..1ba7cc874 --- /dev/null +++ b/src/contracts/Input/Parser/Query/SortClause/SortClauseProcessorInterface.php @@ -0,0 +1,24 @@ + $sortClauseData + * + * @return \Traversable + */ + public function processSortClauses(array $sortClauseData): Traversable; +} From 824a13fa21fb18fe0bbdf7390529fe12abee0fa4 Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Wed, 7 Aug 2024 09:00:20 +0200 Subject: [PATCH 2/4] Changed return type of SortClauseProcessorInterface::processSortClauses --- .../Parser/Query/SortClause/BaseSortClauseProcessor.php | 3 +-- .../Query/SortClause/SortClauseProcessorInterface.php | 6 ++---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/contracts/Input/Parser/Query/SortClause/BaseSortClauseProcessor.php b/src/contracts/Input/Parser/Query/SortClause/BaseSortClauseProcessor.php index e3e086a51..68a13d791 100644 --- a/src/contracts/Input/Parser/Query/SortClause/BaseSortClauseProcessor.php +++ b/src/contracts/Input/Parser/Query/SortClause/BaseSortClauseProcessor.php @@ -8,7 +8,6 @@ use Ibexa\Contracts\Rest\Exceptions; use Ibexa\Contracts\Rest\Input\ParsingDispatcher; -use Traversable; /** * @template TSortClause @@ -24,7 +23,7 @@ public function __construct(ParsingDispatcher $parsingDispatcher) $this->parsingDispatcher = $parsingDispatcher; } - public function processSortClauses(array $sortClauseData): Traversable + public function processSortClauses(array $sortClauseData): iterable { if (empty($sortClauseData)) { yield from []; diff --git a/src/contracts/Input/Parser/Query/SortClause/SortClauseProcessorInterface.php b/src/contracts/Input/Parser/Query/SortClause/SortClauseProcessorInterface.php index 1ba7cc874..628e125d4 100644 --- a/src/contracts/Input/Parser/Query/SortClause/SortClauseProcessorInterface.php +++ b/src/contracts/Input/Parser/Query/SortClause/SortClauseProcessorInterface.php @@ -6,8 +6,6 @@ */ namespace Ibexa\Contracts\Rest\Input\Parser\Query\SortClause; -use Traversable; - /** * @template TSortClause * @@ -18,7 +16,7 @@ interface SortClauseProcessorInterface /** * @param array $sortClauseData * - * @return \Traversable + * @return iterable */ - public function processSortClauses(array $sortClauseData): Traversable; + public function processSortClauses(array $sortClauseData): iterable; } From 177ab674460f8a0a51bb36a8203b17919013e4cc Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Wed, 7 Aug 2024 09:02:56 +0200 Subject: [PATCH 3/4] Marked BaseSortClauseProcessor::processSortClauses as final --- .../Input/Parser/Query/SortClause/BaseSortClauseProcessor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/contracts/Input/Parser/Query/SortClause/BaseSortClauseProcessor.php b/src/contracts/Input/Parser/Query/SortClause/BaseSortClauseProcessor.php index 68a13d791..0fe8ce0cd 100644 --- a/src/contracts/Input/Parser/Query/SortClause/BaseSortClauseProcessor.php +++ b/src/contracts/Input/Parser/Query/SortClause/BaseSortClauseProcessor.php @@ -23,7 +23,7 @@ public function __construct(ParsingDispatcher $parsingDispatcher) $this->parsingDispatcher = $parsingDispatcher; } - public function processSortClauses(array $sortClauseData): iterable + final public function processSortClauses(array $sortClauseData): iterable { if (empty($sortClauseData)) { yield from []; From 97e7cd30b216054fa998b956afc38a8028b30d21 Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Fri, 30 Aug 2024 09:00:15 +0200 Subject: [PATCH 4/4] Fixed cs --- .../Input/Parser/Query/SortClause/BaseSortClauseProcessor.php | 4 +++- .../Parser/Query/SortClause/SortClauseProcessorInterface.php | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/contracts/Input/Parser/Query/SortClause/BaseSortClauseProcessor.php b/src/contracts/Input/Parser/Query/SortClause/BaseSortClauseProcessor.php index 0fe8ce0cd..4405b1396 100644 --- a/src/contracts/Input/Parser/Query/SortClause/BaseSortClauseProcessor.php +++ b/src/contracts/Input/Parser/Query/SortClause/BaseSortClauseProcessor.php @@ -4,6 +4,8 @@ * @copyright Copyright (C) Ibexa AS. All rights reserved. * @license For full copyright and license information view LICENSE file distributed with this source code. */ +declare(strict_types=1); + namespace Ibexa\Contracts\Rest\Input\Parser\Query\SortClause; use Ibexa\Contracts\Rest\Exceptions; @@ -43,7 +45,7 @@ final public function processSortClauses(array $sortClauseData): iterable abstract protected function getMediaTypePrefix(): string; abstract protected function getParserInvalidSortClauseMessage(string $sortClauseName): string; - + private function getSortClauseMediaType(string $sortClauseName): string { $mediaTypePrefix = $this->getMediaTypePrefix(); diff --git a/src/contracts/Input/Parser/Query/SortClause/SortClauseProcessorInterface.php b/src/contracts/Input/Parser/Query/SortClause/SortClauseProcessorInterface.php index 628e125d4..ac8ef713b 100644 --- a/src/contracts/Input/Parser/Query/SortClause/SortClauseProcessorInterface.php +++ b/src/contracts/Input/Parser/Query/SortClause/SortClauseProcessorInterface.php @@ -4,6 +4,8 @@ * @copyright Copyright (C) Ibexa AS. All rights reserved. * @license For full copyright and license information view LICENSE file distributed with this source code. */ +declare(strict_types=1); + namespace Ibexa\Contracts\Rest\Input\Parser\Query\SortClause; /**