-
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.
Merge branch 'main' into serializer-support
- Loading branch information
Showing
4 changed files
with
103 additions
and
8 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
71 changes: 71 additions & 0 deletions
71
src/contracts/Input/Parser/Query/Criterion/BaseCriterionProcessor.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,71 @@ | ||
<?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\Criterion; | ||
|
||
use Ibexa\Contracts\Rest\Exceptions; | ||
use Ibexa\Contracts\Rest\Input\ParsingDispatcher; | ||
|
||
/** | ||
* @template TCriterion | ||
* | ||
* @internal | ||
*/ | ||
abstract class BaseCriterionProcessor implements CriterionProcessorInterface | ||
{ | ||
private const CRITERION_SUFFIX = 'Criterion'; | ||
private const LOGICAL_OPERATOR_CRITERION_MAP = [ | ||
'AND' => 'LogicalAnd', | ||
'OR' => 'LogicalOr', | ||
'NOT' => 'LogicalNot', | ||
]; | ||
|
||
private ParsingDispatcher $parsingDispatcher; | ||
|
||
public function __construct(ParsingDispatcher $parsingDispatcher) | ||
{ | ||
$this->parsingDispatcher = $parsingDispatcher; | ||
} | ||
|
||
final public function processCriteria(array $criteriaData): iterable | ||
{ | ||
if (empty($criteriaData)) { | ||
yield from []; | ||
} | ||
|
||
foreach ($criteriaData as $criterionName => $criterionData) { | ||
$mediaType = $this->getCriterionMediaType($criterionName); | ||
|
||
try { | ||
yield $this->parsingDispatcher->parse([$criterionName => $criterionData], $mediaType); | ||
} catch (Exceptions\Parser $e) { | ||
throw new Exceptions\Parser($this->getParserInvalidCriterionMessage($criterionName), 0, $e); | ||
} | ||
} | ||
} | ||
|
||
private function getCriterionMediaType(string $criterionName): string | ||
{ | ||
if (self::CRITERION_SUFFIX === substr($criterionName, -strlen(self::CRITERION_SUFFIX))) { | ||
$criterionName = substr($criterionName, 0, -strlen(self::CRITERION_SUFFIX)); | ||
} | ||
|
||
if (isset(self::LOGICAL_OPERATOR_CRITERION_MAP[$criterionName])) { | ||
$criterionName = self::LOGICAL_OPERATOR_CRITERION_MAP[$criterionName]; | ||
} | ||
|
||
$mediaTypePrefix = $this->getMediaTypePrefix(); | ||
if ('.' !== substr($mediaTypePrefix, strlen($mediaTypePrefix) - 1)) { | ||
$mediaTypePrefix .= '.'; | ||
} | ||
|
||
return $mediaTypePrefix . $criterionName; | ||
} | ||
|
||
abstract protected function getMediaTypePrefix(): string; | ||
|
||
abstract protected function getParserInvalidCriterionMessage(string $criterionName): string; | ||
} |
24 changes: 24 additions & 0 deletions
24
src/contracts/Input/Parser/Query/Criterion/CriterionProcessorInterface.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. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Contracts\Rest\Input\Parser\Query\Criterion; | ||
|
||
/** | ||
* @template TCriterion | ||
* | ||
* @internal | ||
*/ | ||
interface CriterionProcessorInterface | ||
{ | ||
/** | ||
* @param array<string, array<mixed>> $criteriaData | ||
* | ||
* @return iterable<TCriterion> | ||
*/ | ||
public function processCriteria(array $criteriaData): iterable; | ||
} |
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