Skip to content

Commit 2f751f1

Browse files
committed
Validators: added support for ?nullable types
1 parent 7a9587b commit 2f751f1

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
@@ -124,6 +124,11 @@ public static function is($value, string $expected): bool
124124
return true;
125125
}
126126
continue;
127+
} elseif (substr($item, 0, 1) === '?') {
128+
$item = substr($item, 1);
129+
if ($value === null) {
130+
return true;
131+
}
127132
}
128133

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

tests/Utils/Validators.is().phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,11 @@ test(function () {
373373
Assert::true(Validators::is([['ABCD', 'EFGH'], ['IJKL']], 'string:4[][]'));
374374
Assert::false(Validators::is([['ABCD', 'EFGH'], ['IJKLM']], 'string:4[][]'));
375375
});
376+
377+
378+
test(function () {
379+
Assert::true(Validators::is(null, '?string'));
380+
Assert::true(Validators::is('1', '?string'));
381+
Assert::false(Validators::is(true, '?int'));
382+
Assert::false(Validators::is(0, '?string'));
383+
});

0 commit comments

Comments
 (0)