Download the PHP package fab2s/enumerate without Composer
On this page you can find all versions of the php package fab2s/enumerate. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fab2s/enumerate
More information about fab2s/enumerate
Files in fab2s/enumerate
Package enumerate
Short Description Enumerate, a nice boost to your enums
License MIT
Homepage https://github.com/fab2s/Enumerate
Informations about the package enumerate
Enumerate
A lightweight PHP library that makes native enums practical for real-world applications.
The Problem
PHP 8.1 enums are powerful but have limitations that create friction in production code:
- No
Stringablesupport - Can't use enums directly in string contexts - Strict type restrictions - Can't safely instantiate from mixed input (HTTP requests, databases)
- No inheritance - Can't share behavior across related enums
- Type mismatch pain -
IntBackedEnum::tryFrom('1')throws instead of returning null
The Solution
Enumerate provides a unified API to work with any enum type consistently:
Installation
Requirements: PHP 8.1+
Quick Start
Add the trait to your enum:
Now you can:
API Reference
Instantiation Methods
| Method | Returns | Throws | Description |
|---|---|---|---|
tryFromAny($value, $strict = true) |
?static |
- | Safe instantiation from any type |
fromAny($value, $strict = true) |
static |
InvalidArgumentException |
Strict instantiation |
tryFromName($name) |
?static |
- | Match by case name |
fromName($name) |
static |
InvalidArgumentException |
Strict match by case name |
Value Methods
| Method | Returns | Description |
|---|---|---|
toValue() |
int\|string |
Get the backed value (or case name for UnitEnum) |
jsonSerialize() |
int\|string |
Same as toValue(), implements JsonSerializable |
Comparison Methods
| Method | Returns | Description |
|---|---|---|
equals(...$values) |
bool |
Strict match against provided values |
compares(...$values) |
bool |
Loose match, allows cross-enum comparison by value |
Type Inspection (Static Helper Only)
| Method | Returns | Description |
|---|---|---|
Enumerate::getType($enum) |
?string |
Returns 'string', 'int', or null |
Enumerate::isStringBacked($enum) |
bool |
Check if string-backed |
Enumerate::isIntBacked($enum) |
bool |
Check if int-backed |
Enumerate::isBacked($enum) |
bool |
Check if backed (not UnitEnum) |
Detailed Usage
Safe Instantiation with tryFromAny
The native tryFrom() only accepts the exact backing type. tryFromAny() handles real-world input:
Cross-Enum Matching
When migrating between enum versions or comparing related enums, use non-strict mode:
Working with UnitEnums
UnitEnums (no backing value) are matched by case name:
Comparison Methods
Using the Static Helper
All methods are also available via the Enumerate static class, useful when you can't modify the enum:
Real-World Examples
Form Request Handling
Database Model Casting
API Response
Why This Exists
PHP enums prioritize type safety over practicality. While philosophically sound, this creates boilerplate in real applications where enums must interoperate with HTTP requests (always strings), databases, and JSON APIs.
Enumerate handles this complexity internally so you don't have to write defensive code everywhere enums cross system boundaries.
Contributing
Contributions are welcome. Please open issues and submit pull requests.
License
Enumerate is open-source software licensed under the MIT license.