Skip to content

Commit

Permalink
Allowed null value for minValue
Browse files Browse the repository at this point in the history
  • Loading branch information
ciastektk committed Aug 9, 2024
1 parent 97e73e1 commit 3e7a7fa
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand All @@ -76,7 +85,7 @@ protected function validate($minValue, $maxValue): void

if (
null !== $maxValue
&& $maxValue <= 0
&& $maxValue < 0
) {
throw new InvalidArgumentException(
'$maxValue',
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
14 changes: 12 additions & 2 deletions tests/integration/Core/Repository/SearchServiceImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 3e7a7fa

Please sign in to comment.