1
0
Fork 0
mirror of https://github.com/archtechx/enums.git synced 2025-12-13 07:44:04 +00:00

Extend functionality to work with pure enums

This commit is contained in:
Samuel Levy 2022-03-12 22:56:43 +10:00
parent 20fa78c2b1
commit c2f0e7faf7
8 changed files with 99 additions and 15 deletions

View file

@ -21,7 +21,7 @@ composer require archtechx/enums
### InvokableCases
This helper lets you get the value of a backed enum by "invoking" it — either statically (`MyEnum::FOO()` instead of `MyEnum::FOO`), or as an instance (`$enum()`).
This helper lets you get the value of a backed enum, or the name of a pure enum by "invoking" it — either statically (`MyEnum::FOO()` instead of `MyEnum::FOO`), or as an instance (`$enum()`).
That way, you can use enums as array keys:
```php
@ -58,6 +58,15 @@ enum TaskStatus: int
case COMPLETED = 1;
case CANCELED = 2;
}
enum Role
{
use InvokableCases;
case ADMINISTRATOR;
case SUBSCRIBER;
case GUEST;
}
```
#### Use static calls to get the primitive value
@ -65,13 +74,16 @@ enum TaskStatus: int
TaskStatus::INCOMPLETE(); // 0
TaskStatus::COMPLETED(); // 1
TaskStatus::CANCELED(); // 2
Role::ADMINISTRATOR(); // 'ADMINISTRATOR'
Role::SUBSCRIBER(); // 'SUBSCRIBER'
Role::GUEST(); // 'GUEST'
```
#### Invoke instances to get the primitive value
```php
public function updateStatus(TaskStatus $status)
public function updateStatus(TaskStatus $status, Role $role)
{
$this->record->setStatus($status());
$this->record->setStatus($status(), $role());
}
```
@ -91,16 +103,26 @@ enum TaskStatus: int
case COMPLETED = 1;
case CANCELED = 2;
}
enum Role
{
use Names;
case ADMINISTRATOR;
case SUBSCRIBER;
case GUEST;
}
```
#### Use the `names()` method
```php
TaskStatus::names(); // ['INCOMPLETE', 'COMPLETED', 'CANCELED']
Role::names(); // ['ADMINISTRATOR', 'SUBSCRIBER', 'GUEST']
```
### Values
This helper returns a list of case *values* in the enum.
This helper returns a list of case *values* in the enum. _**NB:** pure enums don't have values, so this will only ever return an empty array._
#### Apply the trait on your enum
```php
@ -114,16 +136,26 @@ enum TaskStatus: int
case COMPLETED = 1;
case CANCELED = 2;
}
enum Role
{
use Values;
case ADMINISTRATOR;
case SUBSCRIBER;
case GUEST;
}
```
#### Use the `values()` method
```php
TaskStatus::values(); // [0, 1, 2]
Role::values(); // []
```
### Options
This helper returns an associative array of case names and values.
This helper returns an associative array of case names and values for backed enums, or an array of names for pure enums (making this functionally equivalent to `::names()` for pure Enums).
#### Apply the trait on your enum
```php
@ -137,11 +169,21 @@ enum TaskStatus: int
case COMPLETED = 1;
case CANCELED = 2;
}
enum Role
{
use Options;
case ADMINISTRATOR;
case SUBSCRIBER;
case GUEST;
}
```
#### Use the `options()` method
```php
TaskStatus::options(); // ['INCOMPLETE' => 0, 'COMPLETED' => 1, 'CANCELED' => 2]
Role::options(); // ['ADMINISTRATOR', 'SUBSCRIBER', 'GUEST']
```
## Development