From c983a9f50b71e00283489fdd822f4193a7eaa7aa Mon Sep 17 00:00:00 2001 From: Anthony <407968526@qq.com> Date: Mon, 28 Feb 2022 14:58:07 +0800 Subject: [PATCH] add enum description --- README.md | 42 +++++++++++++++++++++++++++++++++ src/DescriptionInterface.php | 8 +++++++ src/Descriptions.php | 14 +++++++++++ tests/Pest.php | 16 +++++++++++-- tests/Pest/DescriptionsTest.php | 5 ++++ 5 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 src/DescriptionInterface.php create mode 100644 src/Descriptions.php create mode 100644 tests/Pest/DescriptionsTest.php diff --git a/README.md b/README.md index 4a54fe0..b4cd647 100644 --- a/README.md +++ b/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: diff --git a/src/DescriptionInterface.php b/src/DescriptionInterface.php new file mode 100644 index 0000000..a5d7553 --- /dev/null +++ b/src/DescriptionInterface.php @@ -0,0 +1,8 @@ + $enum->getDescription(), static::cases()); + } +} \ No newline at end of file diff --git a/tests/Pest.php b/tests/Pest.php index 35b60c5..00cd594 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -15,6 +15,8 @@ use ArchTech\Enums\InvokableCases; use ArchTech\Enums\Names; use ArchTech\Enums\Options; use ArchTech\Enums\Values; +use ArchTech\Enums\Descriptions; +use ArchTech\Enums\DescriptionInterface; 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 DONE = 1; + + public function getDescription(): string + { + return match ($this) { + self::PENDING => 'this is `PENDING` description', + self::DONE => 'this is `DONE` description', + }; + } + + } diff --git a/tests/Pest/DescriptionsTest.php b/tests/Pest/DescriptionsTest.php new file mode 100644 index 0000000..ecfd438 --- /dev/null +++ b/tests/Pest/DescriptionsTest.php @@ -0,0 +1,5 @@ +expect(Status::descriptions()) + ->toBe([0 => 'this is `PENDING` description', 1 => 'this is `DONE` description']); \ No newline at end of file