Skip to content

Commit 338c186

Browse files
committed
Invert the behaviour of NotBlank
Since we have the ability to use `not` as a prefix, having rules that start with not becomes a bit inflexible, verbose, and harder to understand. This commit will refactor the `NotBlank` rule by inverting its behaviour and renaming it to `Blank`. Although this is a breaking change, users will not feel it because "NotBlank" will still be available by using the `not` prefix followed by the `Blank` rule.
1 parent 8e77021 commit 338c186

28 files changed

+134
-150
lines changed

bin/create-mixin

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,12 @@ function addMethodToInterface(
107107

108108
$defaultValue = $reflectionParameter->getDefaultValue();
109109
if (is_object($defaultValue)) {
110+
$parameter->setDefaultValue(null);
111+
$parameter->setNullable(true);
110112
continue;
111113
}
112114

113-
$parameter->setDefaultValue($reflectionParameter->getDefaultValue());
115+
$parameter->setDefaultValue($defaultValue);
114116
$parameter->setNullable(false);
115117
}
116118
}
@@ -170,10 +172,10 @@ function overwriteFile(string $content, string $basename): void
170172
['Length', 'length', $numberRelatedRules, []],
171173
['Max', 'max', $numberRelatedRules, []],
172174
['Min', 'min', $numberRelatedRules, []],
173-
['Not', 'not', [], ['Not', 'NotEmpty', 'NotBlank', 'NotEmoji', 'NotUndef', 'NullOr', 'UndefOr', 'Attributes', 'Templated', 'Named']],
174-
['NullOr', 'nullOr', [], ['NullOr', 'NotUndef', 'UndefOr', 'Templated', 'Named']],
175+
['Not', 'not', [], ['Not', 'NotEmpty', 'NotEmoji', 'NotUndef', 'NullOr', 'UndefOr', 'Attributes', 'Templated', 'Named']],
176+
['NullOr', 'nullOr', [], ['NullOr', 'Blank', 'NotUndef', 'UndefOr', 'Templated', 'Named']],
175177
['Property', 'property', [], $structureRelatedRules],
176-
['UndefOr', 'undefOr', [], ['NullOr', 'NotUndef', 'UndefOr', 'Attributes', 'Templated', 'Named']],
178+
['UndefOr', 'undefOr', [], ['NullOr', 'Blank', 'NotUndef', 'UndefOr', 'Attributes', 'Templated', 'Named']],
177179
['', null, [], []],
178180
];
179181

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@
159159

160160
## Miscellaneous
161161

162+
- [Blank](rules/Blank.md)
162163
- [FilterVar](rules/FilterVar.md)
163164
- [Named](rules/Named.md)
164-
- [NotBlank](rules/NotBlank.md)
165165
- [NotEmpty](rules/NotEmpty.md)
166166
- [NotUndef](rules/NotUndef.md)
167167
- [Templated](rules/Templated.md)
@@ -315,6 +315,7 @@
315315
- [Base64](rules/Base64.md)
316316
- [Between](rules/Between.md)
317317
- [BetweenExclusive](rules/BetweenExclusive.md)
318+
- [Blank](rules/Blank.md)
318319
- [BoolType](rules/BoolType.md)
319320
- [BoolVal](rules/BoolVal.md)
320321
- [Bsn](rules/Bsn.md)
@@ -403,7 +404,6 @@
403404
- [No](rules/No.md)
404405
- [NoneOf](rules/NoneOf.md)
405406
- [Not](rules/Not.md)
406-
- [NotBlank](rules/NotBlank.md)
407407
- [NotEmoji](rules/NotEmoji.md)
408408
- [NotEmpty](rules/NotEmpty.md)
409409
- [NotUndef](rules/NotUndef.md)

docs/rules/Blank.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Blank
2+
3+
- `Blank()`
4+
5+
Validates if the given input is a blank value (`null`, zeros, empty strings or empty arrays, recursively).
6+
7+
```php
8+
v::blank()->isValid(null); // true
9+
v::blank()->isValid(''); // true
10+
v::blank()->isValid([]); // true
11+
v::blank()->isValid(' '); // true
12+
v::blank()->isValid(0); // true
13+
v::blank()->isValid('0'); // true
14+
v::blank()->isValid(0); // true
15+
v::blank()->isValid('0.0'); // true
16+
v::blank()->isValid(false); // true
17+
v::blank()->isValid(['']); // true
18+
v::blank()->isValid([' ']); // true
19+
v::blank()->isValid([0]); // true
20+
v::blank()->isValid(['0']); // true
21+
v::blank()->isValid([false]); // true
22+
v::blank()->isValid([[''], [0]]); // true
23+
v::blank()->isValid(new stdClass()); // true
24+
```
25+
26+
It's similar to [NotEmpty](NotEmpty.md), but way stricter.
27+
28+
## Templates
29+
30+
### `Blank::TEMPLATE_STANDARD`
31+
32+
| Mode | Template |
33+
| ---------- | ----------------------------- |
34+
| `default` | {{subject}} must be blank |
35+
| `inverted` | {{subject}} must not be blank |
36+
37+
## Template placeholders
38+
39+
| Placeholder | Description |
40+
| ----------- | ---------------------------------------------------------------- |
41+
| `subject` | The validated input or the custom validator name (if specified). |
42+
43+
## Categorization
44+
45+
- Miscellaneous
46+
47+
## Changelog
48+
49+
| Version | Description |
50+
| ------: | ------------------------------------------- |
51+
| 3.0.0 | Renamed to `Blank` and changed the behavior |
52+
| 1.0.0 | Created as `NotBlank` |
53+
54+
---
55+
56+
See also:
57+
58+
- [NotEmpty](NotEmpty.md)
59+
- [NotUndef](NotUndef.md)
60+
- [NoWhitespace](NoWhitespace.md)
61+
- [NullType](NullType.md)
62+
- [Number](Number.md)
63+
- [UndefOr](UndefOr.md)

docs/rules/NoWhitespace.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ See also:
4242

4343
- [Alnum](Alnum.md)
4444
- [Alpha](Alpha.md)
45+
- [Blank](Blank.md)
4546
- [CreditCard](CreditCard.md)
46-
- [NotBlank](NotBlank.md)
4747
- [NotEmpty](NotEmpty.md)
4848
- [NotUndef](NotUndef.md)
4949
- [UndefOr](UndefOr.md)

docs/rules/NotBlank.md

Lines changed: 0 additions & 63 deletions
This file was deleted.

docs/rules/NotEmpty.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ v::stringType()->notEmpty()->isValid("\t \n \r"); //false
6464

6565
See also:
6666

67+
- [Blank](Blank.md)
6768
- [Each](Each.md)
6869
- [Max](Max.md)
6970
- [Min](Min.md)
70-
- [NotBlank](NotBlank.md)
7171
- [NotUndef](NotUndef.md)
7272
- [NoWhitespace](NoWhitespace.md)
7373
- [NullType](NullType.md)

docs/rules/NotUndef.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ v::notUndef()->isValid(new stdClass()); // true
5959

6060
See also:
6161

62-
- [NotBlank](NotBlank.md)
62+
- [Blank](Blank.md)
6363
- [NotEmpty](NotEmpty.md)
6464
- [NoWhitespace](NoWhitespace.md)
6565
- [NullType](NullType.md)

docs/rules/NullType.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ v::nullType()->isValid(null); // true
3939
See also:
4040

4141
- [ArrayType](ArrayType.md)
42+
- [Blank](Blank.md)
4243
- [BoolType](BoolType.md)
4344
- [BoolVal](BoolVal.md)
4445
- [CallableType](CallableType.md)
4546
- [FloatType](FloatType.md)
4647
- [IntType](IntType.md)
47-
- [NotBlank](NotBlank.md)
4848
- [NotEmpty](NotEmpty.md)
4949
- [NotUndef](NotUndef.md)
5050
- [NullOr](NullOr.md)

docs/rules/Number.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ v::number()->isValid(acos(8)); // false
4242

4343
See also:
4444

45+
- [Blank](Blank.md)
4546
- [BoolType](BoolType.md)
4647
- [CallableType](CallableType.md)
4748
- [FloatType](FloatType.md)
4849
- [IntType](IntType.md)
49-
- [NotBlank](NotBlank.md)
5050
- [NotEmpty](NotEmpty.md)
5151
- [NotUndef](NotUndef.md)
5252
- [NullType](NullType.md)

docs/rules/UndefOr.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ v::not(v::undefOr(v::alpha()))->assert("alpha");
6565

6666
See also:
6767

68-
- [NotBlank](NotBlank.md)
68+
- [Blank](Blank.md)
6969
- [NotEmpty](NotEmpty.md)
7070
- [NotUndef](NotUndef.md)
7171
- [NoWhitespace](NoWhitespace.md)

0 commit comments

Comments
 (0)