Skip to content

Commit

Permalink
Merge branch 'main' into serializer-support
Browse files Browse the repository at this point in the history
  • Loading branch information
barw4 authored Aug 22, 2024
2 parents a95be13 + 6fa825f commit 6eb69b2
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 8 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 @@ -409,6 +409,11 @@ services:

Ibexa\Contracts\Rest\Input\MediaTypeParserInterface: '@Ibexa\Contracts\Rest\Input\MediaTypeParser'

Ibexa\Contracts\Rest\Input\Parser\Query\Criterion\BaseCriterionProcessor:
abstract: true
arguments:
$parsingDispatcher: '@Ibexa\Contracts\Rest\Input\ParsingDispatcher'

ibexa.rest.serializer.encoder.json:
class: Symfony\Component\Serializer\Encoder\JsonEncoder
tags:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

Check warning on line 1 in src/contracts/Input/Parser/Query/Criterion/BaseCriterionProcessor.php

View workflow job for this annotation

GitHub Actions / Run code style check

Found violation(s) of type: blank_lines_before_namespace

/**
* @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;
}
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;
}
11 changes: 3 additions & 8 deletions src/lib/Server/Input/Parser/Criterion/UserMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@
class UserMetadata extends BaseParser
{
/**
* Parses input structure to a Criterion object.
*
* @param array $data
* @param \Ibexa\Contracts\Rest\Input\ParsingDispatcher $parsingDispatcher
* @phpstan-param array{UserMetadataCriterion: array{Target: string, Value: int|string|array}} $data
*
* @throws \Ibexa\Contracts\Rest\Exceptions\Parser
*
* @return \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\UserMetadata
*/
public function parse(array $data, ParsingDispatcher $parsingDispatcher)
public function parse(array $data, ParsingDispatcher $parsingDispatcher): UserMetadataCriterion
{
if (!isset($data['UserMetadataCriterion'])) {
throw new Exceptions\Parser('Invalid <UserMetadataCriterion> format');
Expand All @@ -49,7 +44,7 @@ public function parse(array $data, ParsingDispatcher $parsingDispatcher)

$value = is_array($data['UserMetadataCriterion']['Value'])
? $data['UserMetadataCriterion']['Value']
: explode(',', $data['UserMetadataCriterion']['Value']);
: explode(',', (string)$data['UserMetadataCriterion']['Value']);

return new UserMetadataCriterion($target, null, $value);
}
Expand Down

0 comments on commit 6eb69b2

Please sign in to comment.