Skip to content

Commit

Permalink
IBX-8597: Added BaseSortClauseProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
ciastektk authored Sep 4, 2024
1 parent 397d3b5 commit e2b32ed
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,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'
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* @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;
use Ibexa\Contracts\Rest\Input\ParsingDispatcher;

/**
* @template TSortClause
*
* @internal
*/
abstract class BaseSortClauseProcessor implements SortClauseProcessorInterface
{
private ParsingDispatcher $parsingDispatcher;

public function __construct(ParsingDispatcher $parsingDispatcher)
{
$this->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;
}
}
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.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Rest\Input\Parser\Query\SortClause;

/**
* @template TSortClause
*
* @internal
*/
interface SortClauseProcessorInterface
{
/**
* @param array<string, string> $sortClauseData
*
* @return iterable<TSortClause>
*/
public function processSortClauses(array $sortClauseData): iterable;
}

0 comments on commit e2b32ed

Please sign in to comment.