Download the PHP package hoa/option without Composer
On this page you can find all versions of the php package hoa/option. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package option
Short Description The Hoa\Option library.
License BSD-3-Clause
Homepage https://hoa-project.net/
Informations about the package option
Hoa is a modular, extensible and
structured set of PHP libraries.
Moreover, Hoa aims at being a bridge between industrial and research worlds.
Hoa\Option
This library is an implementation of the
famous
Option
polymorphic type
(also called Maybe
). An Option
represents an optional value, either
there is Some
value, or there is None
which is the equivalent of
null
. This is a convenient and safe way to manipulate an optional
value.
Installation
With Composer, to include this library into
your dependencies, you need to
require hoa/option
:
For more installation procedures, please read the Source page.
Testing
Before running the test suites, the development dependencies must be installed:
Then, to run all the test suites:
For more information, please read the contributor guide.
Quick usage
The following examples illustrate how to use the Hoa\Option\Option
class.
Build an optional value
There are two static methods to allocate an optional value:
Hoa\Option\Option::some($value)
for some value,Hoa\Option\Option::none()
for no value.
Two functional aliases exist, respectively:
Hoa\Option\Some($value)
, andHoa\Option\None()
.
In the next examples, it is assumed that use function Hoa\Option\{Some, None}
is declared, so that we can use Some
and
None
directly.
Basic operations
The isSome
and isNone
methods respectively return true
if the
option contains some value or none:
The unwrap
method returns the contained value if there is some, or
throws a RuntimeException
if there is none:
In general, because of the unexpected exception, its use is
discouraged. Prefer to use either: expect
, unwrapOr
, isSome
, or
isNone
for instance.
One common mistake is to think that it is required to extract/unwrap
the value from the option. Actually, the Hoa\Option\Option
API is
designed to always manipulate an option without having the need to
extract its contained value. New options can be constructed or mapped,
see next sections.
The unwrap
method can throw a RuntimeException
with a default
message. To have a custom message, please use the expect
method:
The unwrapOr
method returns the contained value if there is some, or
a default value if none:
The unwrapOrElse
method returns the contained value if there is
some, or computes a default value from the given callable if none:
Mappers
Mappers transform an option into another option by applying a callable to the contained value if some.
The mapOr
mapper transform an option into another option but use a
default value if there is no contained value:
The result of mapOr
is always an option with some value.
The mapOrElse
mapper is similar to mapOr
but computes the default
value from a callable:
Boolean operations
It is possible to apply boolean operations on options. The and
method returns a none option if the current option has no value, otherwise it
returns the right option:
The andThen
method returns a none option if the current option has no value,
otherwise it returns a new option computed by a callable. Some
languages call this operation flatmap
.
The or
method returns the current option if it has some value, otherwise it
returns the right option:
Finally the orElse
option returns the option if it has some value,
otherwise it returns a new option computed by a callable:
Documentation
The
hack book of Hoa\Option
contains detailed information about how to use this library and how it works.
To generate the documentation locally, execute the following commands:
More documentation can be found on the project's website: hoa-project.net.
Getting help
There are mainly two ways to get help:
- On the
#hoaproject
IRC channel, - On the forum at users.hoa-project.net.
Contribution
Do you want to contribute? Thanks! A detailed contributor guide explains everything you need to know.
License
Hoa is under the New BSD License (BSD-3-Clause). Please, see
LICENSE
for details.