-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IBX-6620: Added image criterions rest input parsers #76
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note
Optional.
You can use
@phpstan-assert
to inform static analysis about the expected shape of data after validation.See https://phpstan.org/writing-php-code/narrowing-types#custom-type-checking-functions-and-methods for details.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we discussed with the dev team, tests coverage will be added later.