|
| 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\Input\Parser\Criterion; |
| 10 | + |
| 11 | +use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Image as ImageCriterion; |
| 12 | +use Ibexa\Contracts\Rest\Input\ParsingDispatcher; |
| 13 | +use Ibexa\Rest\Input\BaseParser; |
| 14 | +use Ibexa\Rest\Server\Exceptions\ValidationFailedException; |
| 15 | +use Ibexa\Rest\Server\Validation\Builder\Input\Parser\Criterion\ImageCriterionValidatorBuilder; |
| 16 | +use Symfony\Component\Validator\Validator\ValidatorInterface; |
| 17 | + |
| 18 | +final class Image extends BaseParser |
| 19 | +{ |
| 20 | + public const IMAGE_CRITERION = 'ImageCriterion'; |
| 21 | + public const FIELD_DEF_IDENTIFIER_KEY = 'fieldDefIdentifier'; |
| 22 | + public const MIME_TYPES_KEY = 'mimeTypes'; |
| 23 | + |
| 24 | + private ValidatorInterface $validator; |
| 25 | + |
| 26 | + public function __construct(ValidatorInterface $validator) |
| 27 | + { |
| 28 | + $this->validator = $validator; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @param array<mixed> $data |
| 33 | + * |
| 34 | + * @return \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Image |
| 35 | + * |
| 36 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException |
| 37 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidCriterionArgumentException |
| 38 | + */ |
| 39 | + public function parse(array $data, ParsingDispatcher $parsingDispatcher): ImageCriterion |
| 40 | + { |
| 41 | + $this->validateInputArray($data); |
| 42 | + |
| 43 | + $criterionData = $data[self::IMAGE_CRITERION]; |
| 44 | + |
| 45 | + return new ImageCriterion( |
| 46 | + $criterionData[self::FIELD_DEF_IDENTIFIER_KEY], |
| 47 | + $this->extractImageCriteria($criterionData) |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @param array<mixed> $data |
| 53 | + */ |
| 54 | + private function validateInputArray(array $data): void |
| 55 | + { |
| 56 | + $validatorBuilder = new ImageCriterionValidatorBuilder($this->validator); |
| 57 | + $validatorBuilder->validateInputArray($data); |
| 58 | + $violations = $validatorBuilder->build()->getViolations(); |
| 59 | + |
| 60 | + if ($violations->count() > 0) { |
| 61 | + throw new ValidationFailedException( |
| 62 | + self::IMAGE_CRITERION, |
| 63 | + $violations |
| 64 | + ); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param array<mixed> $data |
| 70 | + * |
| 71 | + * @return array{ |
| 72 | + * mimeTypes?: string|array<string>, |
| 73 | + * size?: array{min?: int|null, max?: int|null}, |
| 74 | + * width?: array{min?: int|null, max?: int|null}, |
| 75 | + * height?: array{min?: int|null, max?: int|null}, |
| 76 | + * orientation?: string|array<string>, |
| 77 | + * } |
| 78 | + */ |
| 79 | + private function extractImageCriteria(array $data): array |
| 80 | + { |
| 81 | + return array_filter( |
| 82 | + $data, |
| 83 | + static fn (string $criteria): bool => self::FIELD_DEF_IDENTIFIER_KEY !== $criteria, |
| 84 | + ARRAY_FILTER_USE_KEY |
| 85 | + ); |
| 86 | + } |
| 87 | +} |
0 commit comments