<U, E is Throwable>Execute a callable and transform the result into an Option as Option\of() does
but also return Option\None if an exception matching $exceptionClass was thrown.
Successful execution:
self::assertEq(Option\tryOf(fn () => strtolower("FRUITS")), Option\some("fruits"));
Conversion of null to Option\None:
self::assertEq(Option\tryOf(fn() => null), Option\none());
Checked Exception:
self::assertEq(Option\tryOf(fn () => new \DateTimeImmutable("nope")), Option\none());
Unchecked Exception:
self::assertEq(Option\tryOf(fn () => 1 / 0), Option\none());
// @throws DivisionByZeroError Division by zero
| Parameters | |||
|---|---|---|---|
callable(): U
|
$callback |
# | |
mixed
|
$noneValue = null |
# | |
bool
|
$strict = true |
# | |
list<class-string<E>>|class-string<E>
|
$exceptionClass = Exception::class |
# | |
(callable(U): bool)|null
|
$isSome = null |
# | |
| Returns | |
|---|---|
($isSome is null ? Option<U> : ($noneValue is null ? Option<U> : never))
|
|
| Throws | |
|---|---|
InvalidArgumentException |
if both |
Throwable |
|