Skip to content

Commit 657c08a

Browse files
committed
add bool and bool|null
1 parent 62b0b0e commit 657c08a

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

bin/methods.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ types:
99
assertions: '!is_object($value)'
1010
string:
1111
assertions: '!is_string($value)'
12+
bool:
13+
assertions: '!is_bool($value)'
1214
scalar:
1315
returns: [int, float, string, bool]
1416
assertions: '!is_scalar($value)'
@@ -53,6 +55,8 @@ generate:
5355
- float|null
5456
- int|float
5557
- int|float|null
58+
- bool
59+
- bool|null
5660
- scalar
5761
- scalar|null
5862
## special

src/Mixins/ArrayTypeAssertTrait.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ public static function intOrFloatOrNull(mixed $array, int|string $key): int|floa
7070
return TypeAssert::intOrFloatOrNull(self::get($array, $key));
7171
}
7272

73+
public static function bool(mixed $array, int|string $key): bool
74+
{
75+
return TypeAssert::bool(self::get($array, $key));
76+
}
77+
78+
public static function boolOrNull(mixed $array, int|string $key): ?bool
79+
{
80+
return TypeAssert::boolOrNull(self::get($array, $key));
81+
}
82+
7383
public static function scalar(mixed $array, int|string $key): int|float|string|bool
7484
{
7585
return TypeAssert::scalar(self::get($array, $key));

src/Mixins/TypeAssertTrait.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@ public static function intOrFloatOrNull(mixed $value): int|float|null
118118
return $value;
119119
}
120120

121+
public static function bool(mixed $value): bool
122+
{
123+
if (!is_bool($value)) {
124+
throw new AssertionFailedException(self::createErrorMessage($value, 'bool'));
125+
}
126+
127+
return $value;
128+
}
129+
130+
public static function boolOrNull(mixed $value): ?bool
131+
{
132+
if (!is_bool($value) && $value !== null) {
133+
throw new AssertionFailedException(self::createErrorMessage($value, 'bool|null'));
134+
}
135+
136+
return $value;
137+
}
138+
121139
public static function scalar(mixed $value): int|float|string|bool
122140
{
123141
if (!is_scalar($value)) {

0 commit comments

Comments
 (0)