Skip to content

Commit c31c82a

Browse files
committed
Validators: added support for ?nullable types
1 parent 9f2b45b commit c31c82a

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/Utils/Validators.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ public static function is($value, string $expected): bool
119119
return true;
120120
}
121121
continue;
122+
} elseif (substr($item, 0, 1) === '?') {
123+
$item = substr($item, 1);
124+
if ($value === null) {
125+
return true;
126+
}
122127
}
123128

124129
[$type] = $item = explode(':', $item, 2);

tests/Utils/Validators.is().phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,11 @@ test(function () {
399399
Assert::true(Validators::is([['ABCD', 'EFGH'], ['IJKL']], 'string:4[][]'));
400400
Assert::false(Validators::is([['ABCD', 'EFGH'], ['IJKLM']], 'string:4[][]'));
401401
});
402+
403+
404+
test(function () {
405+
Assert::true(Validators::is(null, '?string'));
406+
Assert::true(Validators::is('1', '?string'));
407+
Assert::false(Validators::is(true, '?int'));
408+
Assert::false(Validators::is(0, '?string'));
409+
});

0 commit comments

Comments
 (0)