<U, E is Throwable>Wrap a callable into one that transforms its result into an Option like Option\ify() does
but also return Option\None if an exception matching $exceptionClass was thrown.
Successful execution:
self::assertEq(Option\tryIfy(strtolower(...))("FRUITS"), Option\some("fruits"));
Conversion of null to Option\None:
self::assertEq(Option\tryIfy(fn() => null)(), Option\none());
Checked Exception:
self::assertEq(Option\tryIfy(fn () => new \DateTimeImmutable("nope"))(), Option\none());
Unchecked Exception:
self::assertEq(Option\tryIfy(fn () => 1 / 0)(), Option\none());
// @throws DivisionByZeroError Division by zero
| Parameters | |||
|---|---|---|---|
callable(mixed...): 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 ? Closure(mixed...): Option<U> : ($noneValue is null ? Closure(mixed...): Option<U> : never))
|
|
| Throws | |
|---|---|
InvalidArgumentException |
if both |