skywalker-labs / enum
A robust and type-safe Enum implementation for PHP, featuring singleton-based constants, mapping capabilities, and extensive compatibility from PHP 7.0 to 9.0.
Installs: 75
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/skywalker-labs/enum
Requires
- php: >=7.0 <10.0
Requires (Dev)
- phpunit/phpunit: ^7 || ^8 || ^9 || ^10 || ^11 || ^12
- squizlabs/php_codesniffer: *
README
A robust, type-safe, and singleton-based Enum implementation for PHP.
This library provides a clean way to implement enums in PHP, ensuring that each enum value is a unique singleton instance. It works across PHP versions from 7.0 to 9.0+.
Installation
Install the package via Composer:
composer require skywalker-labs/enum
Basic Usage
To create an enum, extend Skywalker\Enum\AbstractEnum and define your constants as protected const.
use Skywalker\Enum\AbstractEnum; /** * @method static self MONDAY() * @method static self TUESDAY() * ... */ final class WeekDay extends AbstractEnum { protected const MONDAY = null; protected const TUESDAY = null; // ... }
Pro Features
1. Flexible JSON Serialization
Control how your enums appear in API responses.
// Choose between 'name', 'value', or 'object' AbstractEnum::$jsonMode = 'object'; echo json_encode(WeekDay::MONDAY()); // Output: {"name":"MONDAY","value":null,"label":"MONDAY","ordinal":0}
2. Native PHP Compatibility
Use aliases that match PHP 8.1+ native enums for a smoother transition.
$day = WeekDay::from('MONDAY'); // Throws exception if not found $day = WeekDay::tryFrom('INVALID'); // Returns null if not found
3. Randomization
Perfect for seeders and unit tests.
$randomDay = WeekDay::random();
4. String Helpers
echo WeekDay::MONDAY()->lowerName(); // "monday" echo WeekDay::MONDAY()->camelName(); // "Monday"
5. Collection Power-Ups (EnumSet)
Functional methods for modern collections.
$set = EnumSet::allOf(WeekDay::class); $weekends = $set->filter(fn($d) => $d->isAnyOf([WeekDay::SATURDAY(), WeekDay::SUNDAY()])); $names = $set->map(fn($d) => $d->name()); $first = $set->first(); // MONDAY $last = $set->last(); // SUNDAY
Ultimate Edition Features
1. Navigational Helpers
Sequence through your enums easily.
$day = WeekDay::MONDAY(); $next = $day->next(); // TUESDAY $prev = $day->previous(); // null if ($day->isBefore(WeekDay::FRIDAY())) { echo "Hang in there!"; }
2. Set Theory (EnumSet)
Perform mathematical set operations.
$workDays = EnumSet::range(WeekDay::MONDAY(), WeekDay::FRIDAY()); $holidays = EnumSet::of(WeekDay::class, WeekDay::MONDAY()); $actualWork = $workDays->diff($holidays); $allDays = $workDays->union($holidays); $intersect = $workDays->intersect($holidays); $restOfYear = $workDays->complement();
3. Tolerant Lookups
Handle user input with case-insensitivity.
$day = WeekDay::fromNameInsensitive('monday'); // Returns MONDAY instance
Advanced Features
Collection & UI Helpers
// Returns ['MONDAY' => 'MONDAY', 'TUESDAY' => 'TUESDAY', ...] $options = WeekDay::toArray();
Type-Safe Comparisons
if ($day->isAnyOf([WeekDay::SATURDAY(), WeekDay::SUNDAY()])) { echo "It's the weekend!"; }
Bitmask Support
Efficiently store multiple enum values in a single integer.
$mask = $set->toBitmask(); // integer e.g. 5 $restored = EnumSet::fromBitmask(WeekDay::class, $mask);
License
This project is licensed under the MIT License. See the LICENSE file for details.