This package provides a Rust-aligned Option
, Result
, and Either
types providing expressive, type-safe error handling and ergonomic controller helpers for Laravel applications.
Requires PHP 8.4+
composer require cline/monad
- Option Guide - Complete guide with real-world examples for null-safe operations
- Result Guide - Comprehensive error handling patterns with Ok/Err semantics
- Either Guide - Advanced Left/Right branching for complex scenarios
- Transpose Guide - Swapping nested monads (Option ↔ Result)
- Option: Null-safety with
fromNullable()
, LaravelfirstOption()
macros, controllerunwrapOrAbort()
helpers - Result:
Ok
/Err
semantics with full combinators and interop with Option - Either:
Left
/Right
branching for complex multi-path scenarios - Full Rust-aligned API naming for unwrapping and chaining operations
Create: fromNullable()
fromValue()
ensure()
fromReturn()
| Query: isSome()
isNone()
contains()
| Unwrap: unwrap()
unwrapOr()
unwrapOrAbort()
| Transform: map()
andThen()
filter()
| Match: match(someFn, noneFn)
Create: Ok($v)
Err($e)
| Query: isOk()
isErr()
ok()
err()
| Unwrap: unwrap()
unwrapOr()
expect()
| Transform: map()
mapErr()
andThen()
| Match: match(okFn, errFn)
Create: left($v)
right($v)
| Query: isLeft()
isRight()
left()
right()
| Unwrap: unwrapLeft()
unwrapRight()
| Transform: mapLeft()
mapRight()
bimap()
| Match: match(leftFn, rightFn)
See individual cookbook guides for comprehensive API documentation.
Keep a modern codebase with PHP CS Fixer:
composer lint
Run refactors using Rector
composer refactor
Run static analysis using PHPStan:
composer test:types
Run unit tests using PEST
composer test:unit
Run the entire test suite:
composer test
Monad was created by Brian Faust under the MIT license.