PHP Monad API Reference

PhpMonad

Table of Contents

Interfaces

Monad
Monad interface.
Option
Option monad as a `Maybe monad`.
Result
Result monad as a `Either monad`.

Classes

Some
Err
Ok

Enums

None

Functions

some()  : Some<string|int, T>
Return a `Option\Some` option containing `$value`.
none()  : None
Return a `Option\None` option containing no values.
fromValue()  : NoneValue>)
Transform a value into an `Option`.
of()  : NoneValue>)
Execute a callable and transform the result into an `Option`.
tryOf()  : NoneValue>)
Execute a callable and transform the result into an `Option` as `Option\of()` does but also return `Option\None` if it an exception matching $exceptionClass was thrown.
flatten()  : Option<string|int, U>
Converts from `Option<Option<T>>` to `Option<T>`.
transpose()  : Result<Option<string|int, U>, E>
Transposes an `Option` of a `Result` into a `Result` of an `Option`.
ok()  : Ok<string|int, U>
Return a `Result\Ok` Result containing `$value`.
err()  : Err<string|int, F>
Return a `Result\Err` result.
fromThrowable()  : Result<T, E>
Creates a Result from a Closure that may throw an exception.
flatten()  : Result<T, E>
Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.
transpose()  : Option<string|int, Result<U, F>>
Transposes a `Result` of an `Option` into an `Option` of a `Result`.
combine()  : Result<bool, array<int, E>>

Functions

some()

Return a `Option\Some` option containing `$value`.

some(T $value) : Some<string|int, T>
Parameters
$value : T
Tags
template
Return values
Some<string|int, T>

none()

Return a `Option\None` option containing no values.

none() : None
Return values
None

fromValue()

Transform a value into an `Option`.

fromValue(U $value[, NoneValue|null $noneValue = null ]) : NoneValue>)

It will be a Some option containing $value if $value is different from $noneValue (default null)

Parameters
$value : U
$noneValue : NoneValue|null = null
Tags
template
template
Return values
NoneValue>)

of()

Execute a callable and transform the result into an `Option`.

of(callable(): U $callback[, NoneValue|null $noneValue = null ]) : NoneValue>)

It will be a Some option containing the result if it is different from $noneValue (default null).

Parameters
$callback : callable(): U
$noneValue : NoneValue|null = null
Tags
template
template
Return values
NoneValue>)

tryOf()

Execute a callable and transform the result into an `Option` as `Option\of()` does but also return `Option\None` if it an exception matching $exceptionClass was thrown.

tryOf(callable(): U $callback[, NoneValue|null $noneValue = null ][, E> $exceptionClass = Exception::class ]) : NoneValue>)
Parameters
$callback : callable(): U
$noneValue : NoneValue|null = null
$exceptionClass : E> = Exception::class
Tags
template
template
template
throws
Throwable
Return values
NoneValue>)

flatten()

Converts from `Option<Option<T>>` to `Option<T>`.

flatten(Option<string|int, Option<string|int, U>> $option) : Option<string|int, U>
Parameters
$option : Option<string|int, Option<string|int, U>>
Tags
template
Return values
Option<string|int, U>

transpose()

Transposes an `Option` of a `Result` into a `Result` of an `Option`.

transpose(Option<string|int, Result<U, E>> $option) : Result<Option<string|int, U>, E>

None will be mapped to Ok(None). Some(Ok(_)) and Some(Err(_)) will be mapped to Ok(Some(_)) and Err(_).

Parameters
$option : Option<string|int, Result<U, E>>
Tags
template
template
Return values
Result<Option<string|int, U>, E>

ok()

Return a `Result\Ok` Result containing `$value`.

ok([U $value = true ]) : Ok<string|int, U>
Parameters
$value : U = true
Tags
template
Return values
Ok<string|int, U>

err()

Return a `Result\Err` result.

err(F $value) : Err<string|int, F>
Parameters
$value : F
Tags
template
Return values
Err<string|int, F>

fromThrowable()

Creates a Result from a Closure that may throw an exception.

fromThrowable(callable(): T $closure, callable(Throwable): E $errorHandler) : Result<T, E>
Parameters
$closure : callable(): T
$errorHandler : callable(Throwable): E
Tags
template
template
Return values
Result<T, E>

flatten()

Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.

flatten(Result<Result<T, E1>, E2$result) : Result<T, E>
Parameters
$result : Result<Result<T, E1>, E2>
Tags
template
template
template
template
Return values
Result<T, E>

transpose()

Transposes a `Result` of an `Option` into an `Option` of a `Result`.

transpose(Result<Option<string|int, U>, F$result) : Option<string|int, Result<U, F>>

Ok(None) will be mapped to None. Ok(Some(_)) and Err(_) will be mapped to Some(Ok(_)) and Some(Err(_)).

Parameters
$result : Result<Option<string|int, U>, F>
Tags
template
template
Return values
Option<string|int, Result<U, F>>

combine()

combine(Result<T, E...$results) : Result<bool, array<int, E>>
Parameters
$results : Result<T, E>
Tags
template
template
Return values
Result<bool, array<int, E>>

        
On this page

Search results