mirror of
https://github.com/archtechx/enums.git
synced 2025-12-12 16:14:03 +00:00
add enum description
This commit is contained in:
parent
20fa78c2b1
commit
c983a9f50b
5 changed files with 83 additions and 2 deletions
42
README.md
42
README.md
|
|
@ -6,6 +6,7 @@ A collection of enum helpers for PHP.
|
|||
- [`Names`](#names)
|
||||
- [`Values`](#values)
|
||||
- [`Options`](#options)
|
||||
- [`Descriptions`](#descriptions)
|
||||
|
||||
You can read more about the idea on [Twitter](https://twitter.com/archtechx/status/1495158228757270528). I originally wanted to include the `InvokableCases` helper in [`archtechx/helpers`](https://github.com/archtechx/helpers), but it makes more sense to make it a separate dependency and use it *inside* the other package.
|
||||
|
||||
|
|
@ -144,6 +145,47 @@ enum TaskStatus: int
|
|||
TaskStatus::options(); // ['INCOMPLETE' => 0, 'COMPLETED' => 1, 'CANCELED' => 2]
|
||||
```
|
||||
|
||||
### Descriptions
|
||||
|
||||
This helper returns an associative array of case descriptions.
|
||||
|
||||
#### Apply the trait on your enum
|
||||
```php
|
||||
use ArchTech\Enums\Descriptions;
|
||||
use ArchTech\Enums\DescriptionInterface;
|
||||
|
||||
enum TaskStatus: int implements DescriptionInterface
|
||||
{
|
||||
use Descriptions;
|
||||
|
||||
case INCOMPLETE = 0;
|
||||
case COMPLETED = 1;
|
||||
case CANCELED = 2;
|
||||
|
||||
public function getDescription(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::INCOMPLETE => 'this is `INCOMPLETE` description',
|
||||
self::COMPLETED => 'this is `COMPLETED` description',
|
||||
self::CANCELED => 'this is `CANCELED` description'
|
||||
};
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Use the `options()` method
|
||||
```php
|
||||
TaskStatus::descriptions();
|
||||
/**
|
||||
[
|
||||
0 => 'this is `INCOMPLETE` description',
|
||||
1 => 'this is `COMPLETED` description',
|
||||
2 => 'this is `CANCELED` description'
|
||||
]
|
||||
*/
|
||||
```
|
||||
|
||||
|
||||
## Development
|
||||
|
||||
Run all checks locally:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue