From 3e7a7fa57838c70debff04cc13da285062c38988 Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Fri, 9 Aug 2024 12:01:15 +0200 Subject: [PATCH] Allowed null value for minValue --- .../Image/AbstractImageRangeCriterion.php | 37 +++++++++++++++---- .../Query/Criterion/Image/FileSize.php | 18 +++++---- .../Repository/SearchServiceImageTest.php | 14 ++++++- 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/src/contracts/Repository/Values/Content/Query/Criterion/Image/AbstractImageRangeCriterion.php b/src/contracts/Repository/Values/Content/Query/Criterion/Image/AbstractImageRangeCriterion.php index 7540c6a3d5..3a3fc4c331 100644 --- a/src/contracts/Repository/Values/Content/Query/Criterion/Image/AbstractImageRangeCriterion.php +++ b/src/contracts/Repository/Values/Content/Query/Criterion/Image/AbstractImageRangeCriterion.php @@ -16,26 +16,25 @@ abstract class AbstractImageRangeCriterion extends Criterion { /** - * @param numeric $minValue + * @param numeric|null $minValue * @param numeric|null $maxValue * * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException */ public function __construct( string $fieldDefIdentifier, - $minValue = 0, + $minValue = null, $maxValue = null ) { $this->validate($minValue, $maxValue); - - $value[] = $minValue; - $operator = Operator::GTE; + $value[] = $minValue ?? 0; if ($maxValue > 0) { - $operator = Operator::BETWEEN; $value[] = $maxValue; } + $operator = $this->getOperator($value); + parent::__construct( $fieldDefIdentifier, $operator, @@ -60,13 +59,23 @@ public function getSpecifications(): array } /** - * @param numeric $minValue + * @param numeric|null $minValue * @param numeric|null $maxValue * * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException */ protected function validate($minValue, $maxValue): void { + if ( + null === $minValue + && null === $maxValue + ) { + throw new InvalidArgumentException( + implode(', ', ['$minValue', '$maxValue']), + 'At least one value must be specified.' + ); + } + if ($minValue < 0) { throw new InvalidArgumentException( '$minValue', @@ -76,7 +85,7 @@ protected function validate($minValue, $maxValue): void if ( null !== $maxValue - && $maxValue <= 0 + && $maxValue < 0 ) { throw new InvalidArgumentException( '$maxValue', @@ -94,4 +103,16 @@ protected function validate($minValue, $maxValue): void ); } } + + /** + * @param array{0: numeric, 1?: numeric|null} $value + */ + private function getOperator(array $value): string + { + if (count($value) === 2) { + return Operator::BETWEEN; + } + + return Operator::GTE; + } } diff --git a/src/contracts/Repository/Values/Content/Query/Criterion/Image/FileSize.php b/src/contracts/Repository/Values/Content/Query/Criterion/Image/FileSize.php index 4be526959d..e14044df4e 100644 --- a/src/contracts/Repository/Values/Content/Query/Criterion/Image/FileSize.php +++ b/src/contracts/Repository/Values/Content/Query/Criterion/Image/FileSize.php @@ -12,14 +12,11 @@ final class FileSize extends AbstractImageRangeCriterion { public function __construct( string $fieldDefIdentifier, - $minValue = 0, + $minValue = null, $maxValue = null ) { $minValue = $this->convertToBytes($minValue); - - if ($maxValue > 0) { - $maxValue = $this->convertToBytes($maxValue); - } + $maxValue = $this->convertToBytes($maxValue); parent::__construct( $fieldDefIdentifier, @@ -29,10 +26,17 @@ public function __construct( } /** - * @param numeric $value + * @param numeric|null $value */ - private function convertToBytes($value): int + private function convertToBytes($value): ?int { + if ( + null === $value + || 0 === $value + ) { + return null; + } + $value *= 1024 * 1024; return (int)$value; diff --git a/tests/integration/Core/Repository/SearchServiceImageTest.php b/tests/integration/Core/Repository/SearchServiceImageTest.php index 97502cfab6..4882e649e1 100644 --- a/tests/integration/Core/Repository/SearchServiceImageTest.php +++ b/tests/integration/Core/Repository/SearchServiceImageTest.php @@ -86,7 +86,7 @@ public function provideDataForTestCriterion(): iterable yield 'FileSize - default values min 0 and max 1' => [ 3, - $this->createFileSizeCriterion(), + $this->createFileSizeCriterion(0, 1), ]; yield 'FileSize' => [ @@ -114,6 +114,16 @@ public function provideDataForTestCriterion(): iterable $this->createFileSizeCriterion('0.00001', 0.3), ]; + yield 'FileSize - min value' => [ + 3, + $this->createFileSizeCriterion('0.00001'), + ]; + + yield 'FileSize - max value' => [ + 3, + $this->createFileSizeCriterion(null, '1.2'), + ]; + yield 'Width' => [ 3, $this->createWidthCriterion(0, 100), @@ -259,7 +269,7 @@ private function createMimeTypeCriterion($value): Query\Criterion\Image\MimeType } /** - * @param numeric $min + * @param numeric|null $min * @param numeric|null $max */ private function createFileSizeCriterion(