Download the PHP package trehinos/thor-option without Composer
On this page you can find all versions of the php package trehinos/thor-option. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Table of contents
Download trehinos/thor-option
More information about trehinos/thor-option
Files in trehinos/thor-option
Download trehinos/thor-option
More information about trehinos/thor-option
Files in trehinos/thor-option
Vendor trehinos
Package thor-option
Short Description An option-type for PHP 8.1 and above.
License MIT
Package thor-option
Short Description An option-type for PHP 8.1 and above.
License MIT
Please rate this library. Is it a good library?
Informations about the package thor-option
Option type in PHP 8.1+
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.
Say goodbye to null
values
With Thor\Maybe\Option
, you can wrap any value (including null
) and will never take back a null
.
Examples
Playing with some data
Handling NONE
values
Reference
Maybe enumeration
- Case
SOME
to represent the case when an option contains some value, - Case
NONE
to represent the absence of value in an option.
Option
Constructors
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.
Informational methods
$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.
Match
Do something with the value if the Option contains a value, or do something else if the value is none.
Unwrap methods
$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.
License
Copyright 2024 Sébastien GELDREICH
License MIT
All versions of thor-option with dependencies
PHP Build Version
Package Version
Requires
php Version
^8.1
The package trehinos/thor-option contains the following files
Loading the files please wait ....