Skip to content

Commit e2b32ed

Browse files
authored
IBX-8597: Added BaseSortClauseProcessor
#117
1 parent 397d3b5 commit e2b32ed

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

src/bundle/Resources/config/services.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,8 @@ services:
397397
abstract: true
398398
arguments:
399399
$parsingDispatcher: '@Ibexa\Contracts\Rest\Input\ParsingDispatcher'
400+
401+
Ibexa\Contracts\Rest\Input\Parser\Query\SortClause\BaseSortClauseProcessor:
402+
abstract: true
403+
arguments:
404+
$parsingDispatcher: '@Ibexa\Contracts\Rest\Input\ParsingDispatcher'
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Contracts\Rest\Input\Parser\Query\SortClause;
10+
11+
use Ibexa\Contracts\Rest\Exceptions;
12+
use Ibexa\Contracts\Rest\Input\ParsingDispatcher;
13+
14+
/**
15+
* @template TSortClause
16+
*
17+
* @internal
18+
*/
19+
abstract class BaseSortClauseProcessor implements SortClauseProcessorInterface
20+
{
21+
private ParsingDispatcher $parsingDispatcher;
22+
23+
public function __construct(ParsingDispatcher $parsingDispatcher)
24+
{
25+
$this->parsingDispatcher = $parsingDispatcher;
26+
}
27+
28+
final public function processSortClauses(array $sortClauseData): iterable
29+
{
30+
if (empty($sortClauseData)) {
31+
yield from [];
32+
}
33+
34+
foreach ($sortClauseData as $sortClauseName => $direction) {
35+
$mediaType = $this->getSortClauseMediaType($sortClauseName);
36+
37+
try {
38+
yield $this->parsingDispatcher->parse([$sortClauseName => $direction], $mediaType);
39+
} catch (Exceptions\Parser $e) {
40+
throw new Exceptions\Parser($this->getParserInvalidSortClauseMessage($sortClauseName), 0, $e);
41+
}
42+
}
43+
}
44+
45+
abstract protected function getMediaTypePrefix(): string;
46+
47+
abstract protected function getParserInvalidSortClauseMessage(string $sortClauseName): string;
48+
49+
private function getSortClauseMediaType(string $sortClauseName): string
50+
{
51+
$mediaTypePrefix = $this->getMediaTypePrefix();
52+
if ('.' !== substr($mediaTypePrefix, strlen($mediaTypePrefix) - 1)) {
53+
$mediaTypePrefix .= '.';
54+
}
55+
56+
return $mediaTypePrefix . $sortClauseName;
57+
}
58+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Contracts\Rest\Input\Parser\Query\SortClause;
10+
11+
/**
12+
* @template TSortClause
13+
*
14+
* @internal
15+
*/
16+
interface SortClauseProcessorInterface
17+
{
18+
/**
19+
* @param array<string, string> $sortClauseData
20+
*
21+
* @return iterable<TSortClause>
22+
*/
23+
public function processSortClauses(array $sortClauseData): iterable;
24+
}

0 commit comments

Comments
 (0)