Skip to content

Commit a070c43

Browse files
committed
[Validator] Improve FileValidator sizes factorization edge case
1 parent 135f049 commit a070c43

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

Constraints/FileValidator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ private function factorizeSizes(int $size, int|float $limit, bool $binaryFormat)
225225
$coefFactor = self::KB_BYTES;
226226
}
227227

228+
// If $limit < $coef, $limitAsString could be < 1 with less than 3 decimals.
229+
// In this case, we would end up displaying an allowed size < 1 (eg: 0.1 MB).
230+
// It looks better to keep on factorizing (to display 100 kB for example).
231+
while ($limit < $coef) {
232+
$coef /= $coefFactor;
233+
}
234+
228235
$limitAsString = (string) ($limit / $coef);
229236

230237
// Restrict the limit to 2 decimals (without rounding! we

Tests/Constraints/FileValidatorTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ public function provideMaxSizeExceededTests()
153153
[1048577, '1Mi', '1048577', '1048576', 'bytes'],
154154
[1053818, '1Mi', '1029.12', '1024', 'KiB'],
155155
[1053819, '1Mi', '1.01', '1', 'MiB'],
156+
157+
// $limit < $coef, @see FileValidator::factorizeSizes()
158+
[169632, '100k', '169.63', '100', 'kB'],
159+
[1000001, '990k', '1000', '990', 'kB'],
160+
[123, '80', '123', '80', 'bytes'],
156161
];
157162
}
158163

@@ -508,8 +513,8 @@ public function uploadedFileErrorProvider()
508513
], '1000G'];
509514

510515
$tests[] = [(string) \UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', [
511-
'{{ limit }}' => '0.1',
512-
'{{ suffix }}' => 'MB',
516+
'{{ limit }}' => '100',
517+
'{{ suffix }}' => 'kB',
513518
], '100K'];
514519
}
515520

0 commit comments

Comments
 (0)