mirror of
https://github.com/archtechx/enums.git
synced 2025-12-12 12:34:04 +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)
|
- [`Names`](#names)
|
||||||
- [`Values`](#values)
|
- [`Values`](#values)
|
||||||
- [`Options`](#options)
|
- [`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.
|
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]
|
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
|
## Development
|
||||||
|
|
||||||
Run all checks locally:
|
Run all checks locally:
|
||||||
|
|
|
||||||
8
src/DescriptionInterface.php
Normal file
8
src/DescriptionInterface.php
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ArchTech\Enums;
|
||||||
|
|
||||||
|
interface DescriptionInterface
|
||||||
|
{
|
||||||
|
public function getDescription(): string;
|
||||||
|
}
|
||||||
14
src/Descriptions.php
Normal file
14
src/Descriptions.php
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace ArchTech\Enums;
|
||||||
|
|
||||||
|
trait Descriptions
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function descriptions(): array
|
||||||
|
{
|
||||||
|
return array_map(fn($enum) => $enum->getDescription(), static::cases());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -15,6 +15,8 @@ use ArchTech\Enums\InvokableCases;
|
||||||
use ArchTech\Enums\Names;
|
use ArchTech\Enums\Names;
|
||||||
use ArchTech\Enums\Options;
|
use ArchTech\Enums\Options;
|
||||||
use ArchTech\Enums\Values;
|
use ArchTech\Enums\Values;
|
||||||
|
use ArchTech\Enums\Descriptions;
|
||||||
|
use ArchTech\Enums\DescriptionInterface;
|
||||||
|
|
||||||
uses(ArchTech\Enums\Tests\TestCase::class)->in('Pest');
|
uses(ArchTech\Enums\Tests\TestCase::class)->in('Pest');
|
||||||
|
|
||||||
|
|
@ -49,10 +51,20 @@ function something()
|
||||||
// ..
|
// ..
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Status: int
|
enum Status: int implements DescriptionInterface
|
||||||
{
|
{
|
||||||
use InvokableCases, Options, Names, Values;
|
use InvokableCases, Options, Names, Values, Descriptions;
|
||||||
|
|
||||||
case PENDING = 0;
|
case PENDING = 0;
|
||||||
case DONE = 1;
|
case DONE = 1;
|
||||||
|
|
||||||
|
public function getDescription(): string
|
||||||
|
{
|
||||||
|
return match ($this) {
|
||||||
|
self::PENDING => 'this is `PENDING` description',
|
||||||
|
self::DONE => 'this is `DONE` description',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
5
tests/Pest/DescriptionsTest.php
Normal file
5
tests/Pest/DescriptionsTest.php
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
it('can return an array of case descriptions')
|
||||||
|
->expect(Status::descriptions())
|
||||||
|
->toBe([0 => 'this is `PENDING` description', 1 => 'this is `DONE` description']);
|
||||||
Loading…
Add table
Add a link
Reference in a new issue