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

Comparable enum (#20)

* feat: comparable enum

* test: comparable enum

* ci: php-cs-fixer in repository scope

* chore: add `Comparable` usage in README

* ci: globally use `php-cs-fixer`

* improve Comparable logic

* test more PHP versions in CI

* update ci job name

* remove class name quoting in exceptions to match PHP behavior

* migrate pest config

* add comment to test

---------

Co-authored-by: Samuel Štancl <samuel@archte.ch>
Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
This commit is contained in:
Thai Nguyen Hung 2024-01-13 04:02:32 +07:00 committed by GitHub
parent fb521d2dcb
commit f0ea4c36c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 188 additions and 21 deletions

View file

@ -8,6 +8,7 @@ A collection of enum helpers for PHP.
- [`Options`](#options)
- [`From`](#from)
- [`Metadata`](#metadata)
- [`Comparable`](#comparable)
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.
@ -366,6 +367,65 @@ enum TaskStatus: int
And if you're using the same meta property in multiple enums, you can create a dedicated trait that includes this `@method` annotation.
### Comparable
This helper lets you compare enums by `is()`, `isNot()`, `in()` and `notIn()` operators.
#### Apply the trait on your enum
```php
use ArchTech\Enums\Comparable;
enum TaskStatus: int
{
use Comparable;
case INCOMPLETE = 0;
case COMPLETED = 1;
case CANCELED = 2;
}
enum Role
{
use Comparable;
case ADMINISTRATOR;
case SUBSCRIBER;
case GUEST;
}
```
#### Use the `is()` method
```php
TaskStatus::INCOMPLETE->is(TaskStatus::INCOMPLETE); // true
TaskStatus::INCOMPLETE->is(TaskStatus::COMPLETED); // false
Role::ADMINISTRATOR->is(Role::ADMINISTRATOR); // true
Role::ADMINISTRATOR->is(Role::NOBODY); // false
```
#### Use the `isNot()` method
```php
TaskStatus::INCOMPLETE->isNot(TaskStatus::INCOMPLETE); // false
TaskStatus::INCOMPLETE->isNot(TaskStatus::COMPLETED); // true
Role::ADMINISTRATOR->isNot(Role::ADMINISTRATOR); // false
Role::ADMINISTRATOR->isNot(Role::NOBODY); // true
```
#### Use the `in()` method
```php
TaskStatus::INCOMPLETE->in([TaskStatus::INCOMPLETE, TaskStatus::COMPLETED]); // true
TaskStatus::INCOMPLETE->in([TaskStatus::COMPLETED, TaskStatus::CANCELED]); // false
Role::ADMINISTRATOR->in([Role::ADMINISTRATOR, Role::GUEST]); // true
Role::ADMINISTRATOR->in([Role::SUBSCRIBER, Role::GUEST]); // false
```
#### Use the `notIn()` method
```php
TaskStatus::INCOMPLETE->notIn([TaskStatus::INCOMPLETE, TaskStatus::COMPLETED]); // false
TaskStatus::INCOMPLETE->notIn([TaskStatus::COMPLETED, TaskStatus::CANCELED]); // true
Role::ADMINISTRATOR->notIn([Role::ADMINISTRATOR, Role::GUEST]); // false
Role::ADMINISTRATOR->notIn([Role::SUBSCRIBER, Role::GUEST]); // true
```
## PHPStan
To assist PHPStan when using invokable cases, you can include the PHPStan extensions into your own `phpstan.neon` file: