Enum is a Zero-dependencies PHP library that can supercharge enum functionalities.
Installation
You can install this package via composer:
composer require cerbero/enum
Usage
To supercharge our enums with all functionalities provided by this package, we can simply use the Enumerates trait in both pure enums and backed enums:
use Cerbero\Enum\Concerns\Enumerates;
enum PureEnum
{
use Enumerates;
case one;
case two;
case three;
}
enum BackedEnum: int
{
use Enumerates;
case one = 1;
case two = 2;
case three = 3;
}
Classification
These methods determine whether an enum is pure or backed:
PureEnum::isPure(); // true
PureEnum::isBacked(); // false
BackedEnum::isPure(); // false
BackedEnum::isBacked(); // true
Comparison
We can check whether an enum includes some names or values. Pure enums check for names, whilst backed enums check for values:
PureEnum::has('one'); // true
PureEnum::has('four'); // false
PureEnum::doesntHave('one'); // false
PureEnum::doesntHave('four'); // true
BackedEnum::has(1); // true
BackedEnum::has(4); // false
BackedEnum::doesntHave(1); // false
BackedEnum::doesntHave(4); // true
Otherwise, we can let cases determine whether they match with a name or a value:
PureEnum::one->is('one'); // true
PureEnum::one->is(1); // false
PureEnum::one->is('four'); // false
PureEnum::one->isNot('one'); // false
PureEnum::one->isNot(1); // true
PureEnum::one->isNot('four'); // true
BackedEnum::one->is(1); // true
BackedEnum::one->is('1'); // false
BackedEnum::one->is(4); // false
BackedEnum::one->isNot(1); // false
BackedEnum::one->isNot('1'); // true
BackedEnum::one->isNot(4); // true
Comparisons can also be performed within arrays:
PureEnum::one->in(['one', 'four']); // true
PureEnum::one->in([1, 4]); // false
PureEnum::one->notIn('one', 'four'); // false
PureEnum::one->notIn([1, 4]); // true
BackedEnum::one->in([1, 4]); // true
BackedEnum::one->in(['one', 'four']); // false
BackedEnum::one->notIn([1, 4]); // false
BackedEnum::one->notIn(['one', 'four']); // true
Cases collection
When a plain list of cases is returned by one of the operations of the case, it gets wrapped into a CasesCollection which provides a fluent API to perform further operations on the set of cases:
PureEnum::filter('isOdd')->sortBy('color')->pluck('color', 'name'); // ['three' => 'blue', 'one' => 'red']
Cases can be collected by calling collect() or any other cases operation returning a CasesCollection:
PureEnum::collect(); // CasesCollection<PureEnum::one, PureEnum::two, PureEnum::three>
BackedEnum::only('one', 'two'); // CasesCollection<BackedEnum::one, BackedEnum::two>
We can iterate cases collections within any loop:
foreach (PureEnum::collect() as $case) {
echo $case->name;
}
Obtaining the underlying plain list of cases is easy:
PureEnum::collect()->cases(); // [PureEnum::one, PureEnum::two, PureEnum::three]
Sometimes we may need to extract only the first case of the collection:
PureEnum::filter(fn (PureEnum $case) => !$case->isOdd())->first(); // PureEnum::two
This package has a lot of other options with code examples. If you want to dig more you can visit its complete documentation on Github.