php-standard-library/either-or-both
A three-variant disjoint union type for PHP. EitherOrBoth models values that may be present on the left, the right, or both sides at once, giving you a type-safe way to express partial successes, asymmetric results, and paired data without nullable workarounds.
match expressions in PHP 8.1+) without reinventing union types. Ideal for projects adopting domain-driven design (DDD) or event sourcing.if-else chains for multi-step validation (e.g., "validate user input and permissions").try-catch spaghetti for dual-error scenarios (e.g., "API call failed but fallback succeeded").Left (pending), Right (success), or Both (partial success).Adopt if:
match expressions) and needs type-safe disjoint unions without heavyweight libraries (e.g., spatie/array-to-object).null checks or array hacks.Look elsewhere if:
stdClass or bool flags).match support).League\Pipeline (for validation chains).ReactPHP (for async workflows).enum classes (if Both isn’t critical).For Executives: "This package lets us model complex, dual-outcome workflows—like validating both user input and permissions—with clean, type-safe code. It’s a lightweight alternative to custom solutions, reducing technical debt in systems where operations may succeed, fail, or partially succeed. Think of it as ‘either/or/both’ for PHP, enabling safer, more maintainable logic in validation, async tasks, and state machines."
For Engineers:
*"EitherOrBoth gives us a three-state union type (Left, Right, Both) to replace verbose if-else or array hacks. Key benefits:
match expressions catch edge cases.Both::new($user, $permissions)).Example use case: Replace this:
if ($user && $permissions) { ... }
with this:
match ($result) {
Left::class => handleLeft(),
Right::class => handleRight(),
Both::class => handleBoth($result->left(), $result->right()),
};
```*
*Best for teams using PHP 8.1+ and needing to model non-binary outcomes."*
How can I help you explore Laravel packages today?