-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
17 changed files
with
755 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?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\Exceptions; | ||
|
||
use Symfony\Component\Validator\ConstraintViolationListInterface; | ||
use Throwable; | ||
|
||
final class ValidationFailedException extends BadRequestException | ||
{ | ||
private ConstraintViolationListInterface $errors; | ||
|
||
public function __construct( | ||
string $contextName, | ||
ConstraintViolationListInterface $errors, | ||
?Throwable $previous = null | ||
) { | ||
$this->errors = $errors; | ||
|
||
parent::__construct("Input data validation failed for $contextName", 1, $previous); | ||
} | ||
|
||
public function getErrors(): ConstraintViolationListInterface | ||
{ | ||
return $this->errors; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?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\Image as ImageCriterion; | ||
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\ImageCriterionValidatorBuilder; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
final class Image extends BaseParser | ||
{ | ||
public const IMAGE_CRITERION = 'ImageCriterion'; | ||
public const FIELD_DEF_IDENTIFIER_KEY = 'fieldDefIdentifier'; | ||
public const MIME_TYPES_KEY = 'mimeTypes'; | ||
|
||
private ValidatorInterface $validator; | ||
|
||
public function __construct(ValidatorInterface $validator) | ||
{ | ||
$this->validator = $validator; | ||
} | ||
|
||
/** | ||
* @param array<mixed> $data | ||
* | ||
* @return \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Image | ||
* | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidCriterionArgumentException | ||
*/ | ||
public function parse(array $data, ParsingDispatcher $parsingDispatcher): ImageCriterion | ||
{ | ||
$this->validateInputArray($data); | ||
|
||
$criterionData = $data[self::IMAGE_CRITERION]; | ||
|
||
return new ImageCriterion( | ||
$criterionData[self::FIELD_DEF_IDENTIFIER_KEY], | ||
$this->extractImageCriteria($criterionData) | ||
); | ||
} | ||
|
||
/** | ||
* @param array<mixed> $data | ||
*/ | ||
private function validateInputArray(array $data): void | ||
{ | ||
$validatorBuilder = new ImageCriterionValidatorBuilder($this->validator); | ||
$validatorBuilder->validateInputArray($data); | ||
$violations = $validatorBuilder->build()->getViolations(); | ||
|
||
if ($violations->count() > 0) { | ||
throw new ValidationFailedException( | ||
self::IMAGE_CRITERION, | ||
$violations | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @param array<mixed> $data | ||
* | ||
* @return array{ | ||
* mimeTypes?: string|array<string>, | ||
* size?: array{min?: int|null, max?: int|null}, | ||
* width?: array{min?: int|null, max?: int|null}, | ||
* height?: array{min?: int|null, max?: int|null}, | ||
* orientation?: string|array<string>, | ||
* } | ||
*/ | ||
private function extractImageCriteria(array $data): array | ||
{ | ||
return array_filter( | ||
$data, | ||
static fn (string $criteria): bool => self::FIELD_DEF_IDENTIFIER_KEY !== $criteria, | ||
ARRAY_FILTER_USE_KEY | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?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\Image\Dimensions as ImageDimensionsCriterion; | ||
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\ImageDimensionsCriterionValidatorBuilder; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
final class ImageDimensions extends BaseParser | ||
{ | ||
public const IMAGE_DIMENSIONS_CRITERION = 'ImageDimensionsCriterion'; | ||
public const FIELD_DEF_IDENTIFIER_KEY = 'fieldDefIdentifier'; | ||
public const WIDTH_KEY = 'width'; | ||
public const HEIGHT_KEY = 'height'; | ||
|
||
private ValidatorInterface $validator; | ||
|
||
public function __construct(ValidatorInterface $validator) | ||
{ | ||
$this->validator = $validator; | ||
} | ||
|
||
/** | ||
* @param array<mixed> $data | ||
* | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException | ||
*/ | ||
public function parse(array $data, ParsingDispatcher $parsingDispatcher): ImageDimensionsCriterion | ||
{ | ||
$this->validateInputArray($data); | ||
|
||
$criterionData = $data[self::IMAGE_DIMENSIONS_CRITERION]; | ||
|
||
return new ImageDimensionsCriterion( | ||
$criterionData[self::FIELD_DEF_IDENTIFIER_KEY], | ||
$this->extractImageCriteria($criterionData) | ||
); | ||
} | ||
|
||
/** | ||
* @param array<mixed> $data | ||
*/ | ||
private function validateInputArray(array $data): void | ||
{ | ||
$validatorBuilder = new ImageDimensionsCriterionValidatorBuilder($this->validator); | ||
$validatorBuilder->validateInputArray($data); | ||
$violations = $validatorBuilder->build()->getViolations(); | ||
|
||
if ($violations->count() > 0) { | ||
throw new ValidationFailedException( | ||
self::IMAGE_DIMENSIONS_CRITERION, | ||
$violations | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @param array<mixed> $data | ||
* | ||
* @return array{ | ||
* width?: array{min?: int|null, max?: int|null}, | ||
* height?: array{min?: int|null, max?: int|null}, | ||
* } | ||
*/ | ||
private function extractImageCriteria(array $data): array | ||
{ | ||
return array_filter( | ||
$data, | ||
static fn (string $criteria): bool => self::FIELD_DEF_IDENTIFIER_KEY !== $criteria, | ||
ARRAY_FILTER_USE_KEY | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?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\Image\FileSize as ImageFileSizeCriterion; | ||
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\ImageFileSizeCriterionValidatorBuilder; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
final class ImageFileSize extends BaseParser | ||
{ | ||
public const IMAGE_FILE_SIZE_CRITERION = 'ImageFileSizeCriterion'; | ||
public const FIELD_DEF_IDENTIFIER_KEY = 'fieldDefIdentifier'; | ||
public const SIZE_KEY = 'size'; | ||
|
||
private ValidatorInterface $validator; | ||
|
||
public function __construct(ValidatorInterface $validator) | ||
{ | ||
$this->validator = $validator; | ||
} | ||
|
||
/** | ||
* @param array<mixed> $data | ||
* | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException | ||
*/ | ||
public function parse(array $data, ParsingDispatcher $parsingDispatcher): ImageFileSizeCriterion | ||
{ | ||
$this->validateInputArray($data); | ||
|
||
$sizeData = $data[self::IMAGE_FILE_SIZE_CRITERION][self::SIZE_KEY]; | ||
$minFileSize = isset($sizeData['min']) | ||
? (int) $sizeData['min'] | ||
: 0; | ||
|
||
$maxFileSize = isset($sizeData['max']) | ||
? (int) $sizeData['max'] | ||
: null; | ||
|
||
return new ImageFileSizeCriterion( | ||
$data[self::IMAGE_FILE_SIZE_CRITERION][self::FIELD_DEF_IDENTIFIER_KEY], | ||
$minFileSize, | ||
$maxFileSize, | ||
); | ||
} | ||
|
||
/** | ||
* @param array<mixed> $data | ||
*/ | ||
private function validateInputArray(array $data): void | ||
{ | ||
$validatorBuilder = new ImageFileSizeCriterionValidatorBuilder($this->validator); | ||
$validatorBuilder->validateInputArray($data); | ||
$violations = $validatorBuilder->build()->getViolations(); | ||
|
||
if ($violations->count() > 0) { | ||
throw new ValidationFailedException( | ||
self::IMAGE_FILE_SIZE_CRITERION, | ||
$violations | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.