mirror of
https://github.com/archtechx/enums.git
synced 2025-12-12 19:24:04 +00:00
Extend functionality to work with pure enums (#6)
* Extend functionality to work with pure enums * Code review changes Altered `::values()` to act like `::names()` for backed enums, code style fixes, general cleanups
This commit is contained in:
parent
20fa78c2b1
commit
0c87357a6e
9 changed files with 109 additions and 16 deletions
52
README.md
52
README.md
|
|
@ -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* for backed enums, or a list of case *names* for pure enums (making this functionally equivalent to [`::names()`](#names) for pure Enums)
|
||||
|
||||
#### 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(); // ['ADMINISTRATOR', 'SUBSCRIBER', 'GUEST']
|
||||
```
|
||||
|
||||
### 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 a list of names for pure enums (making this functionally equivalent to [`::names()`](#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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue