Skip to content

Commit e8e81c8

Browse files
authored
IBX-8469: Fixed image filtering by file size float value (#414)
For more details see https://issues.ibexa.co/browse/IBX-8469 and #414 Key changes: * Fixed image filtering by image size float value * [Tests] Added tests coverage
1 parent 078d9d0 commit e8e81c8

File tree

3 files changed

+84
-22
lines changed

3 files changed

+84
-22
lines changed

src/contracts/Repository/Values/Content/Query/Criterion/Image/AbstractImageRangeCriterion.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,25 @@
1616
abstract class AbstractImageRangeCriterion extends Criterion
1717
{
1818
/**
19-
* @param numeric $minValue
19+
* @param numeric|null $minValue
2020
* @param numeric|null $maxValue
2121
*
2222
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
2323
*/
2424
public function __construct(
2525
string $fieldDefIdentifier,
26-
$minValue = 0,
26+
$minValue = null,
2727
$maxValue = null
2828
) {
2929
$this->validate($minValue, $maxValue);
30-
31-
$value[] = $minValue;
32-
$operator = Operator::GTE;
30+
$value[] = $minValue ?? 0;
3331

3432
if ($maxValue > 0) {
35-
$operator = Operator::BETWEEN;
3633
$value[] = $maxValue;
3734
}
3835

36+
$operator = $this->getOperator($value);
37+
3938
parent::__construct(
4039
$fieldDefIdentifier,
4140
$operator,
@@ -60,13 +59,23 @@ public function getSpecifications(): array
6059
}
6160

6261
/**
63-
* @param numeric $minValue
62+
* @param numeric|null $minValue
6463
* @param numeric|null $maxValue
6564
*
6665
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
6766
*/
6867
protected function validate($minValue, $maxValue): void
6968
{
69+
if (
70+
null === $minValue
71+
&& null === $maxValue
72+
) {
73+
throw new InvalidArgumentException(
74+
implode(', ', ['$minValue', '$maxValue']),
75+
'At least one value must be specified.'
76+
);
77+
}
78+
7079
if ($minValue < 0) {
7180
throw new InvalidArgumentException(
7281
'$minValue',
@@ -76,7 +85,7 @@ protected function validate($minValue, $maxValue): void
7685

7786
if (
7887
null !== $maxValue
79-
&& $maxValue <= 0
88+
&& $maxValue < 0
8089
) {
8190
throw new InvalidArgumentException(
8291
'$maxValue',
@@ -94,4 +103,16 @@ protected function validate($minValue, $maxValue): void
94103
);
95104
}
96105
}
106+
107+
/**
108+
* @param array{0: numeric, 1?: numeric|null} $value
109+
*/
110+
private function getOperator(array $value): string
111+
{
112+
if (count($value) === 2) {
113+
return Operator::BETWEEN;
114+
}
115+
116+
return Operator::GTE;
117+
}
97118
}

src/contracts/Repository/Values/Content/Query/Criterion/Image/FileSize.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,33 @@ final class FileSize extends AbstractImageRangeCriterion
1212
{
1313
public function __construct(
1414
string $fieldDefIdentifier,
15-
$minValue = 0,
15+
$minValue = null,
1616
$maxValue = null
1717
) {
18-
if ($minValue > 0) {
19-
$minValue *= 1024 * 1024;
20-
}
21-
22-
if ($maxValue > 0) {
23-
$maxValue *= 1024 * 1024;
24-
}
18+
$minValue = $this->convertToBytes($minValue);
19+
$maxValue = $this->convertToBytes($maxValue);
2520

2621
parent::__construct(
2722
$fieldDefIdentifier,
2823
$minValue,
2924
$maxValue
3025
);
3126
}
27+
28+
/**
29+
* @param numeric|null $value
30+
*/
31+
private function convertToBytes($value): ?int
32+
{
33+
if (
34+
null === $value
35+
|| 0 === $value
36+
) {
37+
return null;
38+
}
39+
40+
$value *= 1024 * 1024;
41+
42+
return (int)$value;
43+
}
3244
}

tests/integration/Core/Repository/SearchServiceImageTest.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,39 @@ public function provideDataForTestCriterion(): iterable
8484
),
8585
];
8686

87-
yield 'FileSize - default values min 0 and max 1' => [
87+
yield 'FileSize - with numeric values from 0 to 1' => [
8888
3,
89-
$this->createFileSizeCriterion(),
89+
$this->createFileSizeCriterion(0, 1),
9090
];
9191

92-
yield 'FileSize' => [
92+
yield 'FileSize - with numeric string values from 0.0 to 2.5' => [
9393
3,
94-
$this->createFileSizeCriterion(0, 2),
94+
$this->createFileSizeCriterion('0.0', '2.5'),
95+
];
96+
97+
yield 'FileSize - with numeric values from 0.0 to 2.5' => [
98+
3,
99+
$this->createFileSizeCriterion(0.0, 2.5),
100+
];
101+
102+
yield 'FileSize - with numeric values 0.0001 to 0.004' => [
103+
2,
104+
$this->createFileSizeCriterion(0.001, 0.004),
105+
];
106+
107+
yield 'FileSize - with values numeric string 0.0003 and numeric 0.3' => [
108+
1,
109+
$this->createFileSizeCriterion('0.003', 0.3),
110+
];
111+
112+
yield 'FileSize - min value' => [
113+
2,
114+
$this->createFileSizeCriterion('0.0002'),
115+
];
116+
117+
yield 'FileSize - max value' => [
118+
1,
119+
$this->createFileSizeCriterion(null, '0.0003'),
95120
];
96121

97122
yield 'Width' => [
@@ -238,9 +263,13 @@ private function createMimeTypeCriterion($value): Query\Criterion\Image\MimeType
238263
);
239264
}
240265

266+
/**
267+
* @param numeric|null $min
268+
* @param numeric|null $max
269+
*/
241270
private function createFileSizeCriterion(
242-
int $min = 0,
243-
?int $max = null
271+
$min = 0,
272+
$max = null
244273
): Query\Criterion\Image\FileSize {
245274
return new Query\Criterion\Image\FileSize(
246275
self::IMAGE_FIELD_DEF_IDENTIFIER,

0 commit comments

Comments
 (0)