Skip to content

Commit 6e1be7a

Browse files
committed
add callable and callable|null
1 parent f36051d commit 6e1be7a

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
@@ -80,6 +80,16 @@ public static function boolOrNull(mixed $array, int|string $key): ?bool
8080
return TypeAssert::boolOrNull(self::get($array, $key));
8181
}
8282

83+
public static function callable(mixed $array, int|string $key): callable
84+
{
85+
return TypeAssert::callable(self::get($array, $key));
86+
}
87+
88+
public static function callableOrNull(mixed $array, int|string $key): ?callable
89+
{
90+
return TypeAssert::callableOrNull(self::get($array, $key));
91+
}
92+
8393
public static function scalar(mixed $array, int|string $key): int|float|string|bool
8494
{
8595
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
@@ -136,6 +136,24 @@ public static function boolOrNull(mixed $value): ?bool
136136
return $value;
137137
}
138138

139+
public static function callable(mixed $value): callable
140+
{
141+
if (!is_callable($value)) {
142+
throw new AssertionFailedException(self::createErrorMessage($value, 'callable'));
143+
}
144+
145+
return $value;
146+
}
147+
148+
public static function callableOrNull(mixed $value): ?callable
149+
{
150+
if (!is_callable($value) && $value !== null) {
151+
throw new AssertionFailedException(self::createErrorMessage($value, 'callable|null'));
152+
}
153+
154+
return $value;
155+
}
156+
139157
public static function scalar(mixed $value): int|float|string|bool
140158
{
141159
if (!is_scalar($value)) {

0 commit comments

Comments
 (0)