Download the PHP package tezmanian/utils-enum without Composer
On this page you can find all versions of the php package tezmanian/utils-enum. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tezmanian/utils-enum
More information about tezmanian/utils-enum
Files in tezmanian/utils-enum
Package utils-enum
Short Description A collection of PHP traits that supercharge native enums with comparison, bit-flag, stepping, collection, and translation helpers — zero dependencies, PHP 8.3+.
License MIT
Homepage https://github.com/tezmanian/tez-utils-enum
Informations about the package utils-enum
tez-utils-enum
Composable PHP 8.3 traits that extend native enums with lookup, comparison, navigation, bitmask, label, and collection helpers.
Zero dependencies · PHPStan level 9 · MIT
Installation
Package: tezmanian/utils-enum · Namespace: Tezmanian\Utils\Enum
Requires PHP ≥ 8.3.
Traits
| Trait | Enum type | Purpose |
|---|---|---|
EnumTrait |
Unit | Base helpers: lookup, filtering, ordering |
BackedEnumTrait |
Backed | Extends EnumTrait with value-based helpers |
BitFlagTrait |
Int-backed | Bitmask operations |
ComparableTrait |
Int-backed | Comparison and range checks |
StepTrait |
Any | Navigate to adjacent cases |
TranslatableTrait |
Any | Human-readable labels |
EnumCollectionTrait |
Any | Collection-style selection |
EnumTrait
Base trait for pure (unit) enums. Use BackedEnumTrait instead when your enum has backing values.
Methods
| Method | Description |
|---|---|
is($value) |
True if the case matches by identity or name. Null always returns false. |
isOneOf(?array $values) |
True if the case matches any value in the list. |
isNoneOf(?array $values) |
Inverse of isOneOf(). |
find($value, $ignoreCase) |
Returns a matching case or null. Matches by identity or name. |
filter(callable $cb) |
Returns all cases for which the callback returns true. |
toArray() |
Returns [name => name] for all cases. |
ordinal() |
Zero-based position of this case in the enum definition. |
first() |
Returns the first defined case. |
last() |
Returns the last defined case. |
BackedEnumTrait
Extends EnumTrait for string- or int-backed enums. Overrides is() to match by backing value, and toArray() to return [value => name].
toArray()vstoValueArray()—toArray()returns[value => name], useful for form selects.toValueArray()returns[name => value], useful for serialization. The directions are opposite.
Additional methods
| Method | Description |
|---|---|
values() |
All backing values as a list. |
hasValue($value) |
True if any case has this backing value. |
fromName(string $name) |
Returns a case by name, or throws ValueError. |
tryFromName(string $name) |
Returns a case by name, or null. |
toValueArray() |
Returns [name => value] for all cases. |
BitFlagTrait
Bitmask helpers for int-backed enums used as flag sets. Case values must be powers of 2 (1, 2, 4, 8, …). This constraint is not enforced at runtime.
Does not include BackedEnumTrait — combine them explicitly if needed.
Methods
| Method | Description |
|---|---|
combine(self ...$cases) |
OR-combines case values into a bitmask. Returns 0 with no arguments. |
fromBitmask(int $mask) |
Returns all cases whose bit is set in the mask. Skips cases with value 0. |
isSetIn(int $mask) |
True if this case's bit is set in the mask. |
addTo(int $mask) |
Returns the mask with this case's bit set. |
removeFrom(int $mask) |
Returns the mask with this case's bit cleared. |
ComparableTrait
Comparison helpers for int-backed enums, comparing cases by their backing values.
Methods
| Method | Description |
|---|---|
isGreaterThan(self $other) |
True if this case's value is strictly greater. |
isGreaterThanOrEqual(self $other) |
True if this case's value is greater or equal. |
isLessThan(self $other) |
True if this case's value is strictly less. |
isLessThanOrEqual(self $other) |
True if this case's value is less or equal. |
between($min, $max) |
True if within the inclusive range [$min, $max]. |
clamp($min, $max) |
Returns this case, or $min/$max if out of range. |
StepTrait
Navigate to adjacent cases by definition order. Designed for state machines and ordered workflows. Works with both unit and backed enums.
Order is determined by definition order (ordinal), not backing value. Returns null at boundaries — no wrap-around.
Methods
| Method | Description |
|---|---|
next() |
Returns the next case by definition order, or null at the last case. |
prev() |
Returns the previous case by definition order, or null at the first case. |
TranslatableTrait
Human-readable labels for enum cases, without any framework dependency. Override labels() to supply custom mappings keyed by case name. Falls back to the case name when no label is defined.
Methods
| Method | Description |
|---|---|
label() |
Human-readable label for this case. Falls back to $case->name. |
labelsArray() |
All cases mapped to their labels as [caseName => label]. |
labels() (protected) |
Override in your enum to supply custom label mappings. |
EnumCollectionTrait
Collection-style selection helpers. Can be combined with EnumTrait or BackedEnumTrait. Works with any enum type.
Both except() and only() preserve enum definition order.
Methods
| Method | Description |
|---|---|
random() |
Returns a random case. |
except(self ...$excluded) |
All cases except the given ones, in definition order. |
only(self ...$included) |
Only the given cases, in definition order. |
Combining traits
Traits are designed to be freely mixed. A few common combinations:
All traits use
@phpstan-require-implementsto enforce the correct enum type at static analysis time. MixingBitFlagTraitorComparableTraitwith non-int-backed enums will surface as a PHPStan error.
Requirements
- PHP 8.3+
- No runtime dependencies
Development
License
MIT — see LICENSE.
Author: René Halberstadt