Skip to content

Commit 6e3ed79

Browse files
committed
Refactor Size rule
Currently, the Size rule does multiple things, yet it's limited. Because it does many things, it's also confusing. Turning the Size rule into a transformation allows for way more flexibility and clarity. The syntax becomes more verbose, but the rule becomes more reusable.
1 parent 94daa8d commit 6e3ed79

23 files changed

+514
-266
lines changed

docs/09-list-of-rules-by-category.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@
265265
- [Length](rules/Length.md)
266266
- [Max](rules/Max.md)
267267
- [Min](rules/Min.md)
268+
- [Size](rules/Size.md)
268269

269270
## Types
270271

docs/rules/Size.md

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,71 @@
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

75
Validates 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
***
7871
See 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

library/Factory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Respect\Validation\Transformers\DeprecatedKeyValue;
2222
use Respect\Validation\Transformers\DeprecatedLength;
2323
use Respect\Validation\Transformers\DeprecatedMinAndMax;
24+
use Respect\Validation\Transformers\DeprecatedSize;
2425
use Respect\Validation\Transformers\DeprecatedType;
2526
use Respect\Validation\Transformers\Prefix;
2627
use Respect\Validation\Transformers\RuleSpec;
@@ -44,7 +45,9 @@ public function __construct(
4445
new DeprecatedKeyValue(
4546
new DeprecatedMinAndMax(
4647
new DeprecatedAge(
47-
new DeprecatedKeyNested(new DeprecatedLength(new DeprecatedType(new Aliases(new Prefix()))))
48+
new DeprecatedKeyNested(new DeprecatedLength(new DeprecatedType(new DeprecatedSize(
49+
new Aliases(new Prefix())
50+
))))
4851
)
4952
)
5053
)

library/Mixins/ChainedKey.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,10 @@ public function keyRoman(int|string $key): ChainedValidator;
293293

294294
public function keyScalarVal(int|string $key): ChainedValidator;
295295

296-
public function keySize(
297-
int|string $key,
298-
string|int|null $minSize = null,
299-
string|int|null $maxSize = null,
300-
): ChainedValidator;
296+
/**
297+
* @param "B"|"KB"|"MB"|"GB"|"TB"|"PB"|"EB"|"ZB"|"YB" $unit
298+
*/
299+
public function keySize(int|string $key, string $unit, Rule $rule): ChainedValidator;
301300

302301
public function keySlug(int|string $key): ChainedValidator;
303302

library/Mixins/ChainedNot.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,10 @@ public function notRoman(): ChainedValidator;
295295

296296
public function notScalarVal(): ChainedValidator;
297297

298-
public function notSize(string|int|null $minSize = null, string|int|null $maxSize = null): ChainedValidator;
298+
/**
299+
* @param "B"|"KB"|"MB"|"GB"|"TB"|"PB"|"EB"|"ZB"|"YB" $unit
300+
*/
301+
public function notSize(string $unit, Rule $rule): ChainedValidator;
299302

300303
public function notSlug(): ChainedValidator;
301304

library/Mixins/ChainedNullOr.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,10 @@ public function nullOrRoman(): ChainedValidator;
305305

306306
public function nullOrScalarVal(): ChainedValidator;
307307

308-
public function nullOrSize(string|int|null $minSize = null, string|int|null $maxSize = null): ChainedValidator;
308+
/**
309+
* @param "B"|"KB"|"MB"|"GB"|"TB"|"PB"|"EB"|"ZB"|"YB" $unit
310+
*/
311+
public function nullOrSize(string $unit, Rule $rule): ChainedValidator;
309312

310313
public function nullOrSlug(): ChainedValidator;
311314

library/Mixins/ChainedProperty.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,10 @@ public function propertyRoman(string $propertyName): ChainedValidator;
314314

315315
public function propertyScalarVal(string $propertyName): ChainedValidator;
316316

317-
public function propertySize(
318-
string $propertyName,
319-
string|int|null $minSize = null,
320-
string|int|null $maxSize = null,
321-
): ChainedValidator;
317+
/**
318+
* @param "B"|"KB"|"MB"|"GB"|"TB"|"PB"|"EB"|"ZB"|"YB" $unit
319+
*/
320+
public function propertySize(string $propertyName, string $unit, Rule $rule): ChainedValidator;
322321

323322
public function propertySlug(string $propertyName): ChainedValidator;
324323

library/Mixins/ChainedUndefOr.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,10 @@ public function undefOrRoman(): ChainedValidator;
303303

304304
public function undefOrScalarVal(): ChainedValidator;
305305

306-
public function undefOrSize(string|int|null $minSize = null, string|int|null $maxSize = null): ChainedValidator;
306+
/**
307+
* @param "B"|"KB"|"MB"|"GB"|"TB"|"PB"|"EB"|"ZB"|"YB" $unit
308+
*/
309+
public function undefOrSize(string $unit, Rule $rule): ChainedValidator;
307310

308311
public function undefOrSlug(): ChainedValidator;
309312

library/Mixins/ChainedValidator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,10 @@ public function roman(): ChainedValidator;
349349

350350
public function scalarVal(): ChainedValidator;
351351

352-
public function size(string|int|null $minSize = null, string|int|null $maxSize = null): ChainedValidator;
352+
/**
353+
* @param "B"|"KB"|"MB"|"GB"|"TB"|"PB"|"EB"|"ZB"|"YB" $unit
354+
*/
355+
public function size(string $unit, Rule $rule): ChainedValidator;
353356

354357
public function slug(): ChainedValidator;
355358

library/Mixins/StaticKey.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,10 @@ public static function keyRoman(int|string $key): ChainedValidator;
301301

302302
public static function keyScalarVal(int|string $key): ChainedValidator;
303303

304-
public static function keySize(
305-
int|string $key,
306-
string|int|null $minSize = null,
307-
string|int|null $maxSize = null,
308-
): ChainedValidator;
304+
/**
305+
* @param "B"|"KB"|"MB"|"GB"|"TB"|"PB"|"EB"|"ZB"|"YB" $unit
306+
*/
307+
public static function keySize(int|string $key, string $unit, Rule $rule): ChainedValidator;
309308

310309
public static function keySlug(int|string $key): ChainedValidator;
311310

0 commit comments

Comments
 (0)