TH\Maybe\Option\fromValue

<U>

Transform a value into an Option. It will be a Some option containing $value if $value is different from $noneValue (default null), or, when $isSome is given, if $isSome($value) returns true ($strict is then ignored). $noneValue and $isSome cannot be used together: an \InvalidArgumentException is thrown if both are given.

Examples

self::assertEq(Option\fromValue("fruits"), Option\some("fruits"));
self::assertEq(Option\fromValue(null), Option\none());

Using a predicate:

self::assertEq(Option\fromValue(3, isSome: is_int(...)), Option\some(3));
self::assertEq(Option\fromValue(false, isSome: is_int(...)), Option\none());

$noneValue and $isSome are mutually exclusive:

Option\fromValue(3, noneValue: 0, isSome: is_int(...));
// @throws InvalidArgumentException $noneValue and $isSome cannot be used together
Parameters
U $value #
mixed $noneValue = null #
bool $strict = true #
(callable(U): bool)|null $isSome = null #
Returns
($isSome is null ? Option<U> : ($noneValue is null ? Option<U> : never))
Throws
InvalidArgumentException

if both $noneValue and $isSome are given