azjezz/psl
PSL is a modern, well-typed standard library for PHP 8.4+, inspired by HHVM’s HSL. It offers safer, predictable APIs for async, collections, networking, I/O, crypto, terminal UI, and robust data validation—replacing brittle built-ins with consistent alternatives.
The Either component represents a value that is one of two possible types: Left or Right. Unlike Result (which specifically models success/failure with exceptions), Either is a general-purpose disjoint union -- both sides carry arbitrary values.
This is useful when an operation has two equally valid outcomes that aren't naturally "success" or "error", such as returning cached data vs. fresh data, or routing a value down one of two processing paths.
By convention, Left represents the secondary/error case and Right represents the primary/success case, but this is only a convention -- both sides are first-class values.
Left<TLeft> -- Holds a value of the left typeRight<TRight> -- Holds a value of the right typeEither<TLeft, TRight> -- The common interface both implement@example('types/either-creating.php')
@example('types/either-extracting.php')
mapRight() transforms the right value, leaving a left untouched. mapLeft() does the opposite. map() transforms whichever side is present:
@example('types/either-transforming.php')
When a transformation itself returns an Either, use flatMapRight() or flatMapLeft() to avoid nested Either values:
@example('types/either-flatmap.php')
Handle both cases and return a unified result. The right (happy-path) closure comes first:
@example('types/either-proceed.php')
swap() flips a Left into a Right and vice versa:
@example('types/either-swap.php')
Run a closure on the contained value without changing the Either:
@example('types/either-apply.php')
See src/Psl/Either/ for the full API.
How can I help you explore Laravel packages today?