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.
EitherOrBoth type is a three-state disjoint union (Left/Right/Both), which is ideal for modeling scenarios where a value can exist in one, the other, or both states simultaneously. This is particularly useful in:
Illuminate\Support\Collection, Illuminate\Validation\Validator) often deals with partial data states (e.g., validation errors mixed with successful fields). This package could reduce boilerplate for handling such cases.php-standard-library/either-or-both).EitherOrBoth types, serialization/deserialization).| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| Learning Curve | Developers unfamiliar with disjoint unions may need training. | Write internal docs with Laravel-specific examples (e.g., validation, API responses). |
| Performance Overhead | Immutable objects may have slight memory overhead vs. primitive checks. | Benchmark in critical paths; prefer for complex logic, not simple flags. |
| Type System Quirks | PHP’s dynamic nature may lead to runtime mismatches if not used strictly. | Enforce strict typing in CI (e.g., phpstan). |
| Lack of Adoption | Low stars/dependents may indicate niche use or instability. | Pilot in a non-critical module; monitor for updates. |
| Serialization | Custom serialization may be needed for APIs/database storage. | Extend with JsonSerializable or Laravel’s Serializable trait. |
array/null checks, or solving a new problem (e.g., dual-write consistency)?stdClass hacks)?Validator::errors() + Validator::passes() checks with EitherOrBoth<ValidationErrors, ValidData>.Both for mixed success/error responses).Both if a job affects two databases).authorize() + validate() logic into a single EitherOrBoth return.HttpFoundation responses).{ "status": "both", "left": {...}, "right": {...} }).array/null logic with EitherOrBoth.if-else chains with match expressions (PHP 8.2+).EitherOrBoth response shape.Both states, consider JSON columns or separate tables for left/right values.| Component | Compatibility Notes |
|---|---|
| PHP 8.1+ | Required for enums and named arguments. |
| Laravel 10.x+ | Full support; older versions may need polyfills. |
| Composer | Install via composer require php-standard-library/either-or-both. |
| IDE Support | Modern IDEs (PHPStorm, VSCode) will autocomplete Left/Right/Both variants. |
| Legacy Code | May require adapter classes to bridge old array logic to EitherOrBoth. |
composer.json.ValidationEitherOrBoth).EitherOrBoth-based responses.EitherOrBoth makes code intent clearer than nested if checks.EitherOrBoth types may require deeper inspection.match expressions.match vs. if-else).Both: Ensure teams don’t overuse it for unrelated data.JsonSerializable).| Failure Scenario | Impact | Mitigation |
|---|---|---|
Incorrect EitherOrBoth usage |
Runtime errors or silent bugs. | PHPStan + CI checks for invalid variants. |
| Serialization issues | API/database corruption. | Enforce JsonSerializable or custom serializers. |
| Over-engineering | Unnecessary complexity. | Limit scope to clear pain points (e.g., validation, async jobs). |
| Lack of adoption | Package becomes dead code. | Tie to critical features (e.g., new validation system). |
| PHP Version Downgrade | Breaks if PHP <8.1 is required. | Document version constraints; avoid in legacy projects. |
How can I help you explore Laravel packages today?