https://github.com/decodelabs/enumerable
Helper traits for PHP enums
https://github.com/decodelabs/enumerable
enum language php
Last synced: 3 months ago
JSON representation
Helper traits for PHP enums
- Host: GitHub
- URL: https://github.com/decodelabs/enumerable
- Owner: decodelabs
- License: mit
- Created: 2024-06-14T19:43:19.000Z (about 1 year ago)
- Default Branch: develop
- Last Pushed: 2025-04-14T08:46:53.000Z (3 months ago)
- Last Synced: 2025-04-15T14:06:43.547Z (3 months ago)
- Topics: enum, language, php
- Language: PHP
- Homepage:
- Size: 36.1 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Enumerable
[](https://packagist.org/packages/decodelabs/enumerable)
[](https://packagist.org/packages/decodelabs/enumerable)
[](https://packagist.org/packages/decodelabs/enumerable)
[](https://github.com/decodelabs/enumerable/actions/workflows/integrate.yml)
[](https://github.com/phpstan/phpstan)
[](https://packagist.org/packages/decodelabs/enumerable)### Helper traits for enums
Enumerable provides a simple structure of interfaces and traits to unlock the full power of PHP enums.
---
## Installation
Install via Composer:
```bash
composer require decodelabs/enumerable
```## Usage
Enumerable defines a powerful top level Enum interface that expands the range of functionality enums provide whilst consolidating the same functionality across both
UnitEnum
andBackedEnum
types.All Enumerable enums implement a type specific interface and use an accompanying trait. Each form dictates a type for a
key
,value
andlabel
property, where the key is used as the index in lists and the label is used for display purposes.## Unit enums
### Named UnitEnum
```php
use DecodeLabs\Enumerable\Unit\Named;
use DecodeLabs\Enumerable\Unit\NamedTrait;enum MyNamedUnitEnum implements Named
{
use NamedTrait;const OptionOne;
const OptionTwo;
const OptionThree;
}MyNamedUnitEnum::OptionOne->getName(); // 'OptionOne'
MyNamedUnitEnum::OptionOne->getKey(); // 'OptionOne'
MyNamedUnitEnum::OptionOne->getLabel(); // 'Option One'
MyNamedUnitEnum::OptionOne->getValue(); // 'OptionOne'
```### Indexed UnitEnum
```php
use DecodeLabs\Enumerable\Unit\Indexed;
use DecodeLabs\Enumerable\Unit\IndexedTrait;enum MyIndexedUnitEnum implements Indexed
{
use IndexedTrait;const OptionOne;
const OptionTwo;
const OptionThree;
}MyNamedUnitEnum::OptionOne->getName(); // 'OptionOne'
MyNamedUnitEnum::OptionOne->getKey(); // 0
MyNamedUnitEnum::OptionOne->getLabel(); // 'Option One'
MyNamedUnitEnum::OptionOne->getValue(); // 'OptionOne'
```## Backed enums
### Named String BackedEnum
```php
use DecodeLabs\Enumerable\Backed\NamedString;
use DecodeLabs\Enumerable\Backed\NamedStringTrait;enum MyNamedStringBackedEnum : string implements NamedString
{
use NamedStringTrait;const OptionOne = 'one';
const OptionTwo = 'two';
const OptionThree = 'three';
}MyNamedStringBackedEnum::OptionOne->getName(); // 'OptionOne'
MyNamedStringBackedEnum::OptionOne->getKey(); // 'OptionOne'
MyNamedStringBackedEnum::OptionOne->getLabel(); // 'Option One'
MyNamedStringBackedEnum::OptionOne->getValue(); // 'one'
```### Labelled String BackedEnum
```php
use DecodeLabs\Enumerable\Backed\LabelledString;
use DecodeLabs\Enumerable\Backed\LabelledStringTrait;enum MyLabelledStringBackedEnum : string implements LabelledString
{
use LabelledStringTrait;const OptionOne = 'one';
const OptionTwo = 'two';
const OptionThree = 'three';
}MyLabelledStringBackedEnum::OptionOne->getName(); // 'OptionOne'
MyLabelledStringBackedEnum::OptionOne->getKey(); // 'OptionOne'
MyLabelledStringBackedEnum::OptionOne->getLabel(); // 'one'
MyLabelledStringBackedEnum::OptionOne->getValue(); // 'one'
```### Value String BackedEnum
```php
use DecodeLabs\Enumerable\Backed\ValueString;
use DecodeLabs\Enumerable\Backed\ValueStringTrait;enum MyValueStringBackedEnum : string implements ValueString
{
use ValueStringTrait;const OptionOne = 'one';
const OptionTwo = 'two';
const OptionThree = 'three';
}MyValueStringBackedEnum::OptionOne->getName(); // 'OptionOne'
MyValueStringBackedEnum::OptionOne->getKey(); // 'one'
MyValueStringBackedEnum::OptionOne->getLabel(); // 'Option One'
MyValueStringBackedEnum::OptionOne->getValue(); // 'one'
```### Named Int BackedEnum
```php
use DecodeLabs\Enumerable\Backed\NamedInt;
use DecodeLabs\Enumerable\Backed\NamedIntTrait;enum MyNamedIntBackedEnum : int implements NamedInt
{
use NamedIntTrait;const OptionOne = 1;
const OptionTwo = 2;
const OptionThree = 3;
}MyNamedIntBackedEnum::OptionOne->getName(); // 'OptionOne'
MyNamedIntBackedEnum::OptionOne->getKey(); // 'OptionOne'
MyNamedIntBackedEnum::OptionOne->getLabel(); // 'Option One'
MyNamedIntBackedEnum::OptionOne->getValue(); // 1
```### Value Int BackedEnum
```php
use DecodeLabs\Enumerable\Backed\ValueInt;
use DecodeLabs\Enumerable\Backed\ValueIntTrait;enum MyValueIntBackedEnum : int implements ValueInt
{
use ValueIntTrait;const OptionOne = 1;
const OptionTwo = 2;
const OptionThree = 3;
}MyValueIntBackedEnum::OptionOne->getName(); // 'OptionOne'
MyValueIntBackedEnum::OptionOne->getKey(); // 1
MyValueIntBackedEnum::OptionOne->getLabel(); // 'Option One'
MyValueIntBackedEnum::OptionOne->getValue(); // 1
```## Instantiation
All enum types can be instantiaed with the following methods:
```php
MyEnum::fromKey('');
MyEnum::fromValue('');
MyEnum::fromName('');
MyEnum::fromIndex('');
// or
MyEnum::tryFromKey('');
MyEnum::tryFromValue('');
MyEnum::tryFromName('');
MyEnum::tryFromIndex('');
```## Lists
Enumerable provides three main ways of listing cases:
```php
// Key to label map
MyEnum::getOptions() => [
'' => '',
];// Key to value map
MyEnum::getValues() => [
'' => '',
];// Alias to cases()
MyEnum::getCases() => [
'' => '[MyEnum::]',
];
```## Licensing
Enumerable is licensed under the MIT License. See [LICENSE](./LICENSE) for the full license text.