Skip to content

Commit 62b0b0e

Browse files
committed
add scalar and scalar|null
1 parent a8e3401 commit 62b0b0e

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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 scalar(mixed $array, int|string $key): int|float|string|bool
74+
{
75+
return TypeAssert::scalar(self::get($array, $key));
76+
}
77+
78+
public static function scalarOrNull(mixed $array, int|string $key): int|float|string|bool|null
79+
{
80+
return TypeAssert::scalarOrNull(self::get($array, $key));
81+
}
82+
7383
public static function numeric(mixed $array, int|string $key): float|int
7484
{
7585
return TypeAssert::numeric(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 scalar(mixed $value): int|float|string|bool
122+
{
123+
if (!is_scalar($value)) {
124+
throw new AssertionFailedException(self::createErrorMessage($value, 'scalar'));
125+
}
126+
127+
return $value;
128+
}
129+
130+
public static function scalarOrNull(mixed $value): int|float|string|bool|null
131+
{
132+
if (!is_scalar($value) && $value !== null) {
133+
throw new AssertionFailedException(self::createErrorMessage($value, 'scalar|null'));
134+
}
135+
136+
return $value;
137+
}
138+
121139
public static function numeric(mixed $value): float|int
122140
{
123141
if (is_string($value) && is_numeric($value)) {

0 commit comments

Comments
 (0)