-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IBX-8597: Added BaseSortClauseProcessor
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/contracts/Input/Parser/Query/SortClause/BaseSortClauseProcessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
namespace Ibexa\Contracts\Rest\Input\Parser\Query\SortClause; | ||
|
||
use Ibexa\Contracts\Rest\Exceptions; | ||
use Ibexa\Contracts\Rest\Input\ParsingDispatcher; | ||
use Traversable; | ||
|
||
/** | ||
* @template TSortClause | ||
* | ||
* @internal | ||
*/ | ||
abstract class BaseSortClauseProcessor implements SortClauseProcessorInterface | ||
{ | ||
private ParsingDispatcher $parsingDispatcher; | ||
|
||
public function __construct(ParsingDispatcher $parsingDispatcher) | ||
{ | ||
$this->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); | ||
} | ||
} | ||
} | ||
|
||
private function getSortClauseMediaType(string $sortClauseName): string | ||
{ | ||
$mediaTypePrefix = $this->getMediaTypePrefix(); | ||
if ('.' !== substr($mediaTypePrefix, strlen($mediaTypePrefix) - 1)) { | ||
$mediaTypePrefix .= '.'; | ||
} | ||
|
||
return $mediaTypePrefix . $sortClauseName; | ||
} | ||
|
||
abstract protected function getMediaTypePrefix(): string; | ||
|
||
abstract protected function getParserInvalidSortClauseMessage(string $sortClauseName): string; | ||
} |
24 changes: 24 additions & 0 deletions
24
src/contracts/Input/Parser/Query/SortClause/SortClauseProcessorInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
namespace Ibexa\Contracts\Rest\Input\Parser\Query\SortClause; | ||
|
||
use Traversable; | ||
|
||
/** | ||
* @template TSortClause | ||
* | ||
* @internal | ||
*/ | ||
interface SortClauseProcessorInterface | ||
{ | ||
/** | ||
* @param array<string, string> $sortClauseData | ||
* | ||
* @return \Traversable<TSortClause> | ||
*/ | ||
public function processSortClauses(array $sortClauseData): Traversable; | ||
} |