Skip to content

Commit

Permalink
Added input parser for ContentName criterion
Browse files Browse the repository at this point in the history
  • Loading branch information
ciastektk committed Dec 20, 2023
1 parent 4315d42 commit 385f17c
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/bundle/Resources/config/input_parsers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,13 @@ services:
tags:
- { name: ibexa.rest.input.parser, mediaType: application/vnd.ibexa.api.internal.criterion.Sibling }

Ibexa\Rest\Server\Input\Parser\Criterion\ContentName:
parent: Ibexa\Rest\Server\Common\Parser
arguments:
$validator: '@validator'
tags:
- { name: ibexa.rest.input.parser, mediaType: application/vnd.ibexa.api.internal.criterion.ContentName }

ibexa.rest.input.parser.internal.sortclause.content_id:
parent: Ibexa\Rest\Server\Common\Parser
class: Ibexa\Rest\Server\Input\Parser\SortClause\DataKeyValueObjectClass
Expand Down
57 changes: 57 additions & 0 deletions src/lib/Server/Input/Parser/Criterion/ContentName.php
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.
*/
declare(strict_types=1);

namespace Ibexa\Rest\Server\Input\Parser\Criterion;

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\ContentName as ContentNameCriterion;
use Ibexa\Contracts\Rest\Input\ParsingDispatcher;
use Ibexa\Rest\Input\BaseParser;
use Ibexa\Rest\Server\Exceptions\ValidationFailedException;
use Ibexa\Rest\Server\Validation\Builder\Input\Parser\Criterion\ContentNameValidatorBuilder;
use Symfony\Component\Validator\Validator\ValidatorInterface;

final class ContentName extends BaseParser
{
public const CONTENT_NAME_CRITERION = 'ContentNameCriterion';

private ValidatorInterface $validator;

public function __construct(ValidatorInterface $validator)
{
$this->validator = $validator;
}

/**
* @param array<mixed> $data
*/
public function parse(array $data, ParsingDispatcher $parsingDispatcher): ContentNameCriterion
{
$this->validateInputArray($data);

$criterionData = $data[self::CONTENT_NAME_CRITERION];

return new ContentNameCriterion($criterionData);
}

/**
* @param array<mixed> $data
*/
private function validateInputArray(array $data): void
{
$validatorBuilder = new ContentNameValidatorBuilder($this->validator);
$validatorBuilder->validateInputArray($data);
$violations = $validatorBuilder->build()->getViolations();

if ($violations->count() > 0) {
throw new ValidationFailedException(
self::CONTENT_NAME_CRITERION,
$violations
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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\Rest\Server\Validation\Builder\Input\Parser\Criterion;

use Ibexa\Rest\Server\Input\Parser\Criterion\ContentName;
use Ibexa\Rest\Server\Validation\Builder\Input\Parser\BaseInputParserCollectionValidatorBuilder;
use Symfony\Component\Validator\Constraints as Assert;

final class ContentNameValidatorBuilder extends BaseInputParserCollectionValidatorBuilder
{
protected function getCollectionConstraints(): array
{
return [
ContentName::CONTENT_NAME_CRITERION => new Assert\Required(
[
new Assert\Type('string'),
new Assert\NotBlank(),
]
),
];
}
}

0 comments on commit 385f17c

Please sign in to comment.