PHP Monad API Reference

pipeline.php

Table of Contents

Functions

map()  : callable(Result<T, mixed>): Result<U, mixed>
Pipeline function: Maps the Ok value using the callback.
mapErr()  : callable(Result<mixed, E>): Result<mixed, F>
Pipeline function: Maps the Err value using the callback.
andThen()  : callable(Result<T, mixed>): Result<U, mixed>
Pipeline function: Chains a Result-returning operation on Ok value.
orElse()  : callable(Result<mixed, E>): Result<mixed, F>
Pipeline function: Handles Err by calling a Result-returning function.
inspect()  : callable(Result<T, mixed>): Result<T, mixed>
Pipeline function: Performs a side-effect on Ok value, passing through the Result.
inspectErr()  : callable(Result<mixed, E>): Result<mixed, E>
Pipeline function: Performs a side-effect on Err value, passing through the Result.
unwrapOr()  : callable(Result<mixed, mixed>): mixed
Pipeline function: Unwraps the Ok value or returns the default.
unwrapOrElse()  : callable(Result<mixed, E>): mixed
Pipeline function: Unwraps the Ok value or computes a default from the Err.
expect()  : callable(Result<mixed, mixed>): mixed
Pipeline function: Unwraps the Ok value or throws RuntimeException with the message.

Functions

map()

Pipeline function: Maps the Ok value using the callback.

map(callable(T): U $callback) : callable(Result<T, mixed>): Result<U, mixed>

Usage with PHP 8.5 pipeline operator: $result |> Result\map(fn($x) => $x * 2)

Parameters
$callback : callable(T): U
Tags
template
template
Return values
callable(Result<T, mixed>): Result<U, mixed>

mapErr()

Pipeline function: Maps the Err value using the callback.

mapErr(callable(E): F $callback) : callable(Result<mixed, E>): Result<mixed, F>

Usage with PHP 8.5 pipeline operator: $result |> Result\mapErr(fn($e) => "Error: {$e}")

Parameters
$callback : callable(E): F
Tags
template
template
Return values
callable(Result<mixed, E>): Result<mixed, F>

andThen()

Pipeline function: Chains a Result-returning operation on Ok value.

andThen(callable(T): Result<U, F$callback) : callable(Result<T, mixed>): Result<U, mixed>

Usage with PHP 8.5 pipeline operator: $result |> Result\andThen(fn($x) => validate($x))

Parameters
$callback : callable(T): Result<U, F>
Tags
template
template
template
Return values
callable(Result<T, mixed>): Result<U, mixed>

orElse()

Pipeline function: Handles Err by calling a Result-returning function.

orElse(callable(E): Result<mixed, F$callback) : callable(Result<mixed, E>): Result<mixed, F>

Usage with PHP 8.5 pipeline operator: $result |> Result\orElse(fn($e) => recover($e))

Parameters
$callback : callable(E): Result<mixed, F>
Tags
template
template
Return values
callable(Result<mixed, E>): Result<mixed, F>

inspect()

Pipeline function: Performs a side-effect on Ok value, passing through the Result.

inspect(callable(T): mixed $callback) : callable(Result<T, mixed>): Result<T, mixed>

Usage with PHP 8.5 pipeline operator: $result |> Result\inspect(fn($x) => logger()->info("Got: {$x}"))

Parameters
$callback : callable(T): mixed
Tags
template
Return values
callable(Result<T, mixed>): Result<T, mixed>

inspectErr()

Pipeline function: Performs a side-effect on Err value, passing through the Result.

inspectErr(callable(E): mixed $callback) : callable(Result<mixed, E>): Result<mixed, E>

Usage with PHP 8.5 pipeline operator: $result |> Result\inspectErr(fn($e) => logger()->error($e))

Parameters
$callback : callable(E): mixed
Tags
template
Return values
callable(Result<mixed, E>): Result<mixed, E>

unwrapOr()

Pipeline function: Unwraps the Ok value or returns the default.

unwrapOr(U $default) : callable(Result<mixed, mixed>): mixed

Usage with PHP 8.5 pipeline operator: $result |> Result\unwrapOr(0)

Parameters
$default : U
Tags
template
Return values
callable(Result<mixed, mixed>): mixed

unwrapOrElse()

Pipeline function: Unwraps the Ok value or computes a default from the Err.

unwrapOrElse(callable(E): U $callback) : callable(Result<mixed, E>): mixed

Usage with PHP 8.5 pipeline operator: $result |> Result\unwrapOrElse(fn($e) => fallback($e))

Parameters
$callback : callable(E): U
Tags
template
template
Return values
callable(Result<mixed, E>): mixed

expect()

Pipeline function: Unwraps the Ok value or throws RuntimeException with the message.

expect(string $message) : callable(Result<mixed, mixed>): mixed

Usage with PHP 8.5 pipeline operator: $result |> Result\expect('Value must be present')

Parameters
$message : string
Return values
callable(Result<mixed, mixed>): mixed

        
On this page

Search results