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..4405b1396 --- /dev/null +++ b/src/contracts/Input/Parser/Query/SortClause/BaseSortClauseProcessor.php @@ -0,0 +1,58 @@ +parsingDispatcher = $parsingDispatcher; + } + + final public function processSortClauses(array $sortClauseData): iterable + { + 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..ac8ef713b --- /dev/null +++ b/src/contracts/Input/Parser/Query/SortClause/SortClauseProcessorInterface.php @@ -0,0 +1,24 @@ + $sortClauseData + * + * @return iterable + */ + public function processSortClauses(array $sortClauseData): iterable; +}