Skip to content
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-8469: Fixed image filtering by file size float value #414

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
ciastektk marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,33 @@ final class FileSize extends AbstractImageRangeCriterion
{
public function __construct(
string $fieldDefIdentifier,
$minValue = 0,
$minValue = null,
$maxValue = null
) {
if ($minValue > 0) {
$minValue *= 1024 * 1024;
}
ciastektk marked this conversation as resolved.
Show resolved Hide resolved

if ($maxValue > 0) {
$maxValue *= 1024 * 1024;
}
$minValue = $this->convertToBytes($minValue);
$maxValue = $this->convertToBytes($maxValue);

parent::__construct(
$fieldDefIdentifier,
$minValue,
$maxValue
);
}

/**
* @param numeric|null $value
*/
private function convertToBytes($value): ?int
{
if (
null === $value
|| 0 === $value
) {
return null;
}

$value *= 1024 * 1024;

return (int)$value;
}
}
41 changes: 35 additions & 6 deletions tests/integration/Core/Repository/SearchServiceImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down Expand Up @@ -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,
Expand Down
Loading