This package let you use an option (Thor\Maybe\Option
) type in PHP to handle cases when a value can or can not take a value.
The API of this module is intensively inspired by Rust's Option type.
With Thor\Maybe\Option
, you can wrap any value (including null
) and will never take back a null
.
use Thor\Maybe\Option;
use Thor\Maybe\Maybe;
$myOption = Option::from("data...");
// Or
$myOption = Option::some("data...");
if ($myOption->isNone()) {
// Never
}
// Or
if ($myOption->isA(Maybe::NONE)) {
// Never
}
// Unwrap the optional value
if ($myOption->is() === Maybe::SOME) {
// Here we know we can unwrap().
$myString = $myOption->unwrap();
}
// Echoes the string if it is not none, or an empty string if it is :
echo $myOption->matches(
fn(string $str) => $str,
fn() => '',
);
// Or
echo $myOption->unwrapOr('');
use Thor\Maybe\Option;
$myOption = Option::from(null);
$myOption = Option::none();
$value = $myOption->unwrap(); // Throws a RuntimeException
$value = $myOption->unwrapOrThrow(new Exception("Custom Exception"));
$value = $myOption->unwrapOrElse(fn() => 'default value from callable');
$value = $myOption->unwrapOr('default value');
- Case
SOME
to represent the case when an option contains some value, - Case
NONE
to represent the absence of value in an option.
Option::from(mixed $value)
: create a new option with some value or none if$value
is null,Option::some(mixed $value)
: create a new option with some value,Option::none()
: create a new option with none.
$myOption->is()
: returns aMaybe::SOME
or aMaybe::NONE
,$myOption->isNone()
: returnstrue
if the option is none,$myOption->isSome()
: returnstrue
if the option is some,$myOption->isA(Maybe $maybe)
: returnstrue
if the option is corresponding the $maybe case.
Do something with the value if the Option contains a value, or do something else if the value is none.
use Thor\Maybe\Option;
use Thor\Maybe\Maybe;
$myOption = Option::some("data...");
$myOption->matches(
fn(string $str) => "My Option is Some($str)",
fn() => 'My Option is None...',
);
$value = $myOption->unwrap()
: throws a RuntimeException if the value of the option is none,$value = $myOption->unwrapOrThrow(new Exception("Custom Exception"))
: throws the specifiedThrowable
if the value of the option is none,$value = $myOption->unwrapOrElse(fn() => 'default value from callable')
: executes the callable in parameter if the value of the option is none and returns its returned value,$value = $myOption->unwrapOr('default value')
: returns the specified value if the value of the option is none.
Copyright 2024 Sébastien GELDREICH
License MIT