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 9ca824e6e4..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,16 +12,11 @@ final class FileSize extends AbstractImageRangeCriterion { public function __construct( string $fieldDefIdentifier, - $minValue = 0, + $minValue = null, $maxValue = null ) { - if ($minValue > 0) { - $minValue *= 1024 * 1024; - } - - if ($maxValue > 0) { - $maxValue *= 1024 * 1024; - } + $minValue = $this->convertToBytes($minValue); + $maxValue = $this->convertToBytes($maxValue); parent::__construct( $fieldDefIdentifier, @@ -29,4 +24,21 @@ public function __construct( $maxValue ); } + + /** + * @param numeric|null $value + */ + 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 0d6c0f6731..8c8de7c906 100644 --- a/tests/integration/Core/Repository/SearchServiceImageTest.php +++ b/tests/integration/Core/Repository/SearchServiceImageTest.php @@ -84,14 +84,39 @@ public function provideDataForTestCriterion(): iterable ), ]; - yield 'FileSize - default values min 0 and max 1' => [ + yield 'FileSize - with numeric values from 0 to 1' => [ 3, - $this->createFileSizeCriterion(), + $this->createFileSizeCriterion(0, 1), ]; - yield 'FileSize' => [ + yield 'FileSize - with numeric string values from 0.0 to 2.5' => [ 3, - $this->createFileSizeCriterion(0, 2), + $this->createFileSizeCriterion('0.0', '2.5'), + ]; + + yield 'FileSize - with numeric values from 0.0 to 2.5' => [ + 3, + $this->createFileSizeCriterion(0.0, 2.5), + ]; + + yield 'FileSize - with numeric values 0.0001 to 0.004' => [ + 2, + $this->createFileSizeCriterion(0.001, 0.004), + ]; + + yield 'FileSize - with values numeric string 0.0003 and numeric 0.3' => [ + 1, + $this->createFileSizeCriterion('0.003', 0.3), + ]; + + yield 'FileSize - min value' => [ + 2, + $this->createFileSizeCriterion('0.0002'), + ]; + + yield 'FileSize - max value' => [ + 1, + $this->createFileSizeCriterion(null, '0.0003'), ]; yield 'Width' => [ @@ -238,9 +263,13 @@ private function createMimeTypeCriterion($value): Query\Criterion\Image\MimeType ); } + /** + * @param numeric|null $min + * @param numeric|null $max + */ private function createFileSizeCriterion( - int $min = 0, - ?int $max = null + $min = 0, + $max = null ): Query\Criterion\Image\FileSize { return new Query\Criterion\Image\FileSize( self::IMAGE_FIELD_DEF_IDENTIFIER,