Download the PHP package emilianobovetti/php-option without Composer
On this page you can find all versions of the php package emilianobovetti/php-option. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download emilianobovetti/php-option
More information about emilianobovetti/php-option
Files in emilianobovetti/php-option
Package php-option
Short Description Lightweight option in PHP
License GPL-3.0
Informations about the package php-option
PhpOption
Tired of Trying to get property of non-object
?
This is a porting of the option type in PHP.
Installation
composer require emilianobovetti/php-option
Creating Options
You can create options through constructors (e.g. new Some(0)
, new None
) or using facade methods of the Option class.
Option::none()
returns an instance of the None class. Since this class is stateless there is no need to create a new object every time that a None is needed.
Option::create($value, $empty = null)
returns an Option object. By default it returns a None if the $value
is null
, or a new Some($value)
otherwise.
You can change this behavior passing the $empty
parameter.
E.g. Option::create(0, 0) === Option::none()
If another option is passed to the create method, the option itself is returned.
If the $value
is callable then the value that it yields is used.
If $empty
is callable then it's applied to $value
and a None is returned if it yields a falsy value.
Using Options
With a convenient syntax you can use PhpOption to get properties from objects and keys from arrays while handling null
s.
Option
s also offer a lot of flexibility.
Wait, but I have an array!
Okay, what if I have some resource intensive functions?
If $user->some->property
exists and is not null, no methods get called.
If the first callback gets called and returns a non-null value, the following functions aren't executed, and so on...
How to output Option
s?
Option methods
filter(Closure $function)
If the Option is a Some and the given $function
returns false on its value, filter method returns None.
Returns the Option itself otherwise.
map(Closure $function)
If the Option is a Some returns another Option wrapping the result of $function
applied to Option's value.
Returns a None otherwise.
each(Closure $function)
Applies given $function
to the Option's value if is non-empty, does nothing otherwise.
Returns Option itself.
get()
Gets the value of the Option or throws NoneValueException
if the Option is a None.
getOrElse(mixed $default)
If the Option is a Some returns its value, returns $default
otherwise.
$default
can be:
- a callback - which is called and its result returned if the Option is a None.
- any other PHP value - which is returned in case the Option is a None.
getOrThrow(Exception $e)
Gets the value of the Option or throws $e
if the Option is a None.
getOrCall(Closure $function)
If the Option is a Some returns its value, returns the value that yields $function
otherwise.
orElse(mixed $default)
Acts like getOrElse
, but returns an Option instead of its value.
isDefined()
and isEmpty()
The first one returns true if the Option is not a None, the second one returns true if it is.
Dynamic methods
__toString()
A None gets casted to an empty string, while a non-empty Option returns the (string)
cast of its value.
__get(string $name)
If the Option is a Some which contains an array or an object, it looks for the given key or property.
If it exists and is not null
, returns an Option wrapping this value.
In any other case returns a None.
__isset(string $name)
Check if the given key or property exists in the Option's value.
E.g.
__invoke(mixed $default)
Makes the Option callable.
This is a shortcut for get()
and getOrElse()
methods.
E.g.
Like getOrElse method $default
can be a callback or any other PHP value.