Skip to content

Commit 2a89508

Browse files
committed
IBX-10527: Use BaseInputParserCollectionValidatorBuilder
1 parent 76bfa81 commit 2a89508

File tree

3 files changed

+67
-12
lines changed

3 files changed

+67
-12
lines changed

src/bundle/Resources/config/input_parsers.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ services:
6464

6565
Ibexa\Rest\Server\Input\Parser\ContentType\RestViewInput:
6666
parent: Ibexa\Rest\Server\Common\Parser
67+
arguments:
68+
$validator: '@validator'
6769
tags:
6870
- { name: ibexa.rest.input.parser, mediaType: application/vnd.ibexa.api.ContentTypeViewInput }
6971

src/lib/Server/Input/Parser/ContentType/RestViewInput.php

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,54 @@
88

99
namespace Ibexa\Rest\Server\Input\Parser\ContentType;
1010

11-
use Ibexa\Contracts\Rest\Exceptions;
1211
use Ibexa\Contracts\Rest\Input\ParsingDispatcher;
12+
use Ibexa\Rest\Server\Exceptions\ValidationFailedException;
1313
use Ibexa\Rest\Server\Input\Parser\Criterion as CriterionParser;
14+
use Ibexa\Rest\Server\Validation\Builder\Input\Parser\Criterion\ContentTypeRestViewInputValidatorBuilder;
1415
use Ibexa\Rest\Server\Values\ContentTypeRestViewInput;
16+
use Symfony\Component\Validator\Validator\ValidatorInterface;
1517

1618
final class RestViewInput extends CriterionParser
1719
{
18-
private const VIEW_INPUT_IDENTIFIER = 'ContentTypeQuery';
20+
public const VIEW_INPUT_IDENTIFIER = 'ContentTypeQuery';
21+
22+
public const IDENTIFIER = 'identifier';
23+
24+
private ValidatorInterface $validator;
25+
26+
public function __construct(ValidatorInterface $validator)
27+
{
28+
$this->validator = $validator;
29+
}
1930

2031
public function parse(array $data, ParsingDispatcher $parsingDispatcher): ContentTypeRestViewInput
2132
{
2233
$restViewInput = new ContentTypeRestViewInput();
2334
$restViewInput->languageCode = $data['languageCode'] ?? null;
2435

25-
$viewInputIdentifier = self::VIEW_INPUT_IDENTIFIER;
26-
if (!array_key_exists($viewInputIdentifier, $data)) {
27-
throw new Exceptions\Parser('Missing ' . $viewInputIdentifier . ' attribute for <ViewInput>.');
28-
}
29-
30-
if (!is_array($data[$viewInputIdentifier])) {
31-
throw new Exceptions\Parser($viewInputIdentifier . ' attribute for <ViewInput> contains invalid data.');
32-
}
36+
$this->validateInputArray($data);
3337

34-
$queryData = $data[$viewInputIdentifier];
35-
$queryMediaType = 'application/vnd.ibexa.api.internal.' . $viewInputIdentifier;
38+
$queryData = $data[self::VIEW_INPUT_IDENTIFIER];
39+
$queryMediaType = 'application/vnd.ibexa.api.internal.' . self::VIEW_INPUT_IDENTIFIER;
3640
$restViewInput->query = $parsingDispatcher->parse($queryData, $queryMediaType);
3741

3842
return $restViewInput;
3943
}
44+
45+
/**
46+
* @param array<mixed> $data
47+
*/
48+
private function validateInputArray(array $data): void
49+
{
50+
$validatorBuilder = new ContentTypeRestViewInputValidatorBuilder($this->validator);
51+
$validatorBuilder->validateInputArray($data);
52+
$violations = $validatorBuilder->build()->getViolations();
53+
54+
if ($violations->count() > 0) {
55+
throw new ValidationFailedException(
56+
self::VIEW_INPUT_IDENTIFIER,
57+
$violations
58+
);
59+
}
60+
}
4061
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Rest\Server\Validation\Builder\Input\Parser\Criterion;
10+
11+
use Ibexa\Rest\Server\Input\Parser\ContentType\RestViewInput;
12+
use Ibexa\Rest\Server\Validation\Builder\Input\Parser\BaseInputParserCollectionValidatorBuilder;
13+
use Symfony\Component\Validator\Constraints as Assert;
14+
15+
final class ContentTypeRestViewInputValidatorBuilder extends BaseInputParserCollectionValidatorBuilder
16+
{
17+
protected function getCollectionConstraints(): array
18+
{
19+
return [
20+
RestViewInput::VIEW_INPUT_IDENTIFIER => new Assert\Required(
21+
[
22+
new Assert\Type('array'),
23+
]
24+
),
25+
RestViewInput::IDENTIFIER => new Assert\Required(
26+
[
27+
new Assert\Type('string'),
28+
]
29+
),
30+
];
31+
}
32+
}

0 commit comments

Comments
 (0)