Skip to content

Commit b8530f9

Browse files
committed
Update maxSize rule
1 parent 8042ebc commit b8530f9

8 files changed

Lines changed: 39 additions & 20 deletions

File tree

‎guide/index.rst‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -602,15 +602,19 @@ The rule can take one parameter: ``$maxLength``.
602602
maxSize
603603
#######
604604

605-
The field requires a file that does not exceed the maximum size of ``{0}`` kilobytes.
605+
The field requires a file that does not exceed the maximum size of ``{0}``,
606+
using ``{1}`` as the unit of measurement.
606607

607608
.. code-block:: php
608609
609-
maxSize:$kilobytes
610+
maxSize:$size,$unit
610611
611-
The rule can take one parameter: ``$kilobytes``.
612+
The rule can take two parameters: ``$size`` and ``$unit``.
612613

613-
``$kilobytes`` is the maximum number of kilobytes that the field file can receive.
614+
``$size`` is the maximum number of ``$unit`` that the field file can receive.
615+
616+
``$unit`` can be ``B`` (bytes), ``KB`` (kilobytes), ``MB`` (megabytes), ``GB``
617+
(gigabytes) or ``TB`` (terabytes).
614618

615619
md5
616620
###

‎src/FilesValidator.php‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Framework\Validation;
1111

1212
use Framework\Helpers\ArraySimple;
13+
use InvalidArgumentException;
1314

1415
/**
1516
* Class FilesValidator.
@@ -66,19 +67,28 @@ public static function uploaded(string $field, array $data = []) : bool
6667
*
6768
* @param string $field
6869
* @param array<string,mixed> $data
69-
* @param int $kilobytes
70+
* @param int|string $size
71+
* @param string $unit
7072
*
7173
* @return bool
7274
*/
73-
public static function maxSize(string $field, array $data, int | string $kilobytes) : bool
75+
public static function maxSize(string $field, array $data, int | string $size, string $unit) : bool
7476
{
7577
$uploaded = static::uploaded($field);
7678
if (!$uploaded) {
7779
return false;
7880
}
7981
$file = static::getFile($field);
80-
$kilobytes = (int) $kilobytes;
81-
return $file['size'] <= ($kilobytes * 1024);
82+
$size = (int) $size;
83+
$size = match(\strtolower($unit)) {
84+
'b', 'byte', 'bytes' => $size,
85+
'kb', 'kilobyte', 'kilobytes' => $size * 1024,
86+
'mb', 'megabyte', 'megabytes' => $size * 1024 * 1024,
87+
'gb', 'gigabyte', 'gigabytes' => $size * 1024 * 1024 * 1024,
88+
'tb', 'terabyte', 'terabytes' => $size * 1024 * 1024 * 1024 * 1024,
89+
default => throw new InvalidArgumentException('Invalid unit of measurement: ' . $unit)
90+
};
91+
return $file['size'] <= $size;
8292
}
8393

8494
/**

‎src/Languages/en/validation.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
'isset' => 'The {field} field must be sent.',
4242
'latin' => 'The {field} field requires only latin characters.',
4343
'uploaded' => 'The {field} field requires a file to be uploaded.',
44-
'maxSize' => 'The {field} field requires a file that does not exceed the maximum size of {0} kilobytes.',
44+
'maxSize' => 'The {field} field requires a file that does not exceed the maximum size of {0} {1, select, b{B} kb{KB} mb{MB} gb{GB} tb{TB} other{{1}}}.',
4545
'ext' => 'The {field} field requires a file with an accepted extension: {args}.',
4646
'mimes' => 'The {field} field requires a file with an accepted MIME type: {args}.',
4747
'image' => 'The {field} field requires an image.',

‎src/Languages/es/validation.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
'isset' => 'El campo {field} debe enviarse.',
4242
'latin' => 'El campo {field} sólo requiere caracteres latinos.',
4343
'uploaded' => 'El campo {field} requiere que se cargue un archivo.',
44-
'maxSize' => 'El campo {field} requiere un archivo que no exceda el tamaño máximo de {0} kilobytes.',
44+
'maxSize' => 'El campo {field} requiere un archivo que no exceda el tamaño máximo de {0} {1, select, b{B} kb{KB} mb{MB} gb{GB} tb{TB} other{{1}}}.',
4545
'ext' => 'El campo {field} requiere un archivo con una extensión aceptada: {args}.',
4646
'mimes' => 'El campo {field} requiere un archivo con un tipo MIME aceptado: {args}.',
4747
'image' => 'El campo {field} requiere una imagen.',

‎src/Languages/pt-br/validation.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
'isset' => 'O campo {field} deve ser enviado.',
4242
'latin' => 'O campo {field} requer apenas caracteres latinos.',
4343
'uploaded' => 'O campo {field} requer que um arquivo seja carregado.',
44-
'maxSize' => 'O campo {field} requer um arquivo que não exceda o tamanho máximo de {0} kilobytes.',
44+
'maxSize' => 'O campo {field} requer um arquivo que não exceda o tamanho máximo de {0} {1, select, b{B} kb{KB} mb{MB} gb{GB} tb{TB} other{{1}}}.',
4545
'ext' => 'O campo {field} requer um arquivo com uma extensão aceita: {args}.',
4646
'mimes' => 'O campo {field} requer um arquivo com um tipo MIME aceito: {args}.',
4747
'image' => 'O campo {field} requer uma imagem.',

‎src/Traits/FilesValidator.php‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ public function uploaded() : static
3030
/**
3131
* Validates file size.
3232
*
33-
* @param int $kilobytes
33+
* @param int $size
34+
* @param string $unit
3435
*
3536
* @return static
3637
*/
37-
public function maxSize(int $kilobytes) : static
38+
public function maxSize(int $size, string $unit) : static
3839
{
39-
$this->rules[] = 'maxSize:' . $kilobytes;
40+
$this->rules[] = 'maxSize:' . $size . ',' . $unit;
4041
return $this;
4142
}
4243

‎tests/FilesValidatorTest.php‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,15 @@ public function testUploaded() : void
103103

104104
public function testMaxSize() : void
105105
{
106-
self::assertFalse(FilesValidatorMock::maxSize('foo[a]', [], 10));
107-
self::assertFalse(FilesValidatorMock::maxSize('foo[a]', [], '10'));
108-
self::assertTrue(FilesValidatorMock::maxSize('foo[a]', [], 40));
109-
self::assertTrue(FilesValidatorMock::maxSize('foo[a]', [], '40'));
110-
self::assertFalse(FilesValidator::maxSize('foo[a]', [], 40));
106+
self::assertFalse(FilesValidator::maxSize('foo[a]', [], 10, 'b'));
107+
self::assertFalse(FilesValidatorMock::maxSize('foo[a]', [], 10, 'b'));
108+
self::assertFalse(FilesValidatorMock::maxSize('foo[a]', [], 10, 'kb'));
109+
self::assertTrue(FilesValidatorMock::maxSize('foo[a]', [], 10, 'mb'));
110+
self::assertTrue(FilesValidatorMock::maxSize('foo[a]', [], 10, 'gb'));
111+
self::assertTrue(FilesValidatorMock::maxSize('foo[a]', [], 10, 'tb'));
112+
$this->expectException(\InvalidArgumentException::class);
113+
$this->expectExceptionMessage('Invalid unit of measurement: foo');
114+
FilesValidatorMock::maxSize('foo[a]', [], 10, 'foo');
111115
}
112116

113117
public function testMimeTypes() : void

‎tests/RulesTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public function testRuleUploaded() : void
262262

263263
public function testRuleMaxSize() : void
264264
{
265-
self::assertRule('maxSize:10', $this->rules->maxSize(10));
265+
self::assertRule('maxSize:10,kb', $this->rules->maxSize(10, 'kb'));
266266
}
267267

268268
public function testRuleMimes() : void

0 commit comments

Comments
 (0)