11# Size
22
3- - ` Size(string $minSize) `
4- - ` Size(string $minSize, string $maxSize) `
5- - ` Size(null, string $maxSize) `
3+ - ` Size(string $unit, Rule $rule) `
64
75Validates whether the input is a file that is of a certain size or not.
86
97``` php
10- v::size('1KB') ->isValid($filename); // Must have at least 1KB size
11- v::size('1MB ', '2MB') ->isValid($filename); // Must have the size between 1MB and 2MB
12- v::size(null, '1GB') ->isValid($filename); // Must not be greater than 1GB
8+ v::size('KB', v::greaterThan(1)) ->isValid($filename);
9+ v::size('MB ', v::between(1, 2)) ->isValid($filename);
10+ v::size('GB', v::lessThan(1)) ->isValid($filename);
1311```
1412
15- Sizes are not case-sensitive and the accepted values are:
13+ Accepted data storage units are ` B ` , ` KB ` , ` MB ` , ` GB ` , ` TB ` , ` PB ` , ` EB ` , ` ZB ` , and ` YB ` .
1614
17- - B
18- - KB
19- - MB
20- - GB
21- - TB
22- - PB
23- - EB
24- - ZB
25- - YB
15+ This validator will accept:
2616
27- This validator will consider ` SplFileInfo ` instances, like:
17+ * ` string ` file paths
18+ * ` SplFileInfo ` objects (see [ SplFileInfo] [ ] )
19+ * ` Psr\Http\Message\UploadedFileInterface ` objects (see [ PSR-7] [ ] )
20+ * ` Psr\Http\Message\StreamInterface ` objects (see [ PSR-7] [ ] )
2821
29- ``` php
30- v::size('1.5mb')->isValid(new SplFileInfo($filename)); // Will return true or false
31- ```
22+ ## Templates
3223
33- Message template for this validator includes ` {{minSize}} ` and ` {{maxSize}} ` .
24+ ### ` Size::TEMPLATE_STANDARD `
3425
35- ## Templates
26+ | Mode | Template |
27+ | ------------| ------------------------------------|
28+ | ` default ` | The size in {{unit| ; trans}} of |
29+ | ` inverted ` | The size in {{unit| ; trans}} of |
3630
37- ### ` Size::TEMPLATE_BOTH `
31+ This template serve as message prefix:
3832
39- | Mode | Template |
40- | ------------| ----------------------------------------------------------|
41- | ` default ` | {{name}} must be between {{minSize}} and {{maxSize}} |
42- | ` inverted ` | {{name}} must not be between {{minSize}} and {{maxSize}} |
33+ ``` php
34+ v::size('MB', v::equals(2))->assert('filename.txt')
35+ // Message: The size in megabytes of "filename.txt" must be equal to 2
4336
44- ### ` Size::TEMPLATE_LOWER `
37+ v::size('KB', v::not(v::equals(56)))->assert('filename.txt')
38+ // Message: The size in kilobytes of "filename.txt" must not be equal to 56
39+ ```
4540
46- | Mode | Template |
47- | ------------| -----------------------------------------------|
48- | ` default ` | {{name}} must be greater than {{minSize}} |
49- | ` inverted ` | {{name}} must not be greater than {{minSize}} |
41+ ### ` Size::TEMPLATE_WRONG_TYPE `
5042
51- ### ` Size::TEMPLATE_GREATER `
43+ Used when the input is not a valid file path, a ` SplFileInfo ` object, or a PSR-7 interface.
5244
53- | Mode | Template |
54- | ------------| ---------------------------------------------|
55- | ` default ` | {{name}} must be lower than {{maxSize}} |
56- | ` inverted ` | {{name}} must not be lower than {{maxSize}} |
45+ | Mode | Template |
46+ | ------------| ------------------------------------------------------------------------------------ |
47+ | ` default ` | {{name}} must be a filename or an instance of SplFileInfo or a PSR-7 interface |
48+ | ` inverted ` | {{name}} must not be a filename or an instance of SplFileInfo or a PSR-7 interface |
5749
5850## Template placeholders
5951
6052| Placeholder | Description |
6153| -------------| ------------------------------------------------------------------|
62- | ` maxSize ` | |
63- | ` minSize ` | |
6454| ` name ` | The validated input or the custom validator name (if specified). |
55+ | ` unit ` | The name of the storage unit (bytes, kilobytes, etc.) |
6556
6657## Categorization
6758
6859- File system
60+ - Transformations
6961
7062## Changelog
7163
72- | Version | Description |
73- | --------:| -------------------|
74- | 2.1.0 | Add PSR-7 support |
75- | 1.0.0 | Created |
64+ | Version | Description |
65+ | --------:| -------------------------|
66+ | 3.0.0 | Became a transformation |
67+ | 2.1.0 | Add [ PSR-7] [ ] support |
68+ | 1.0.0 | Created |
7669
7770***
7871See also:
@@ -88,3 +81,6 @@ See also:
8881- [ SymbolicLink] ( SymbolicLink.md )
8982- [ Uploaded] ( Uploaded.md )
9083- [ Writable] ( Writable.md )
84+
85+ [ PSR-7 ] : https://www.php-fig.org/psr/psr-7/
86+ [ SplFileInfo ] : https://www.php.net/SplFileInfo
0 commit comments