Skip to content

Commit

Permalink
IBX-8597: Added BaseSortClauseProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
ciastektk committed Jul 19, 2024
1 parent f50ccb4 commit da66dfb
Show file tree
Hide file tree
Showing 3 changed files with 86 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 @@ -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'
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;
}
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;
}

0 comments on commit da66dfb

Please sign in to comment.