|
8 | 8 |
|
9 | 9 | namespace Ibexa\Rest\Server\Input\Parser\ContentType; |
10 | 10 |
|
11 | | -use Ibexa\Contracts\Rest\Exceptions; |
12 | 11 | use Ibexa\Contracts\Rest\Input\ParsingDispatcher; |
| 12 | +use Ibexa\Rest\Server\Exceptions\ValidationFailedException; |
13 | 13 | use Ibexa\Rest\Server\Input\Parser\Criterion as CriterionParser; |
| 14 | +use Ibexa\Rest\Server\Validation\Builder\Input\Parser\Criterion\ContentTypeRestViewInputValidatorBuilder; |
14 | 15 | use Ibexa\Rest\Server\Values\ContentTypeRestViewInput; |
| 16 | +use Symfony\Component\Validator\Validator\ValidatorInterface; |
15 | 17 |
|
16 | 18 | final class RestViewInput extends CriterionParser |
17 | 19 | { |
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 | + } |
19 | 30 |
|
20 | 31 | public function parse(array $data, ParsingDispatcher $parsingDispatcher): ContentTypeRestViewInput |
21 | 32 | { |
22 | 33 | $restViewInput = new ContentTypeRestViewInput(); |
23 | 34 | $restViewInput->languageCode = $data['languageCode'] ?? null; |
24 | 35 |
|
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); |
33 | 37 |
|
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; |
36 | 40 | $restViewInput->query = $parsingDispatcher->parse($queryData, $queryMediaType); |
37 | 41 |
|
38 | 42 | return $restViewInput; |
39 | 43 | } |
| 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 | + } |
40 | 61 | } |
0 commit comments