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

chore: add Comparable usage in README

This commit is contained in:
Thai Nguyen Hung 2023-06-08 09:57:36 +07:00
parent 46944dc5f4
commit 8b90e21803

View file

@ -8,6 +8,7 @@ A collection of enum helpers for PHP.
- [`Options`](#options) - [`Options`](#options)
- [`From`](#from) - [`From`](#from)
- [`Metadata`](#metadata) - [`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. 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. 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 ## PHPStan
To assist PHPStan when using invokable cases, you can include the PHPStan extensions into your own `phpstan.neon` file: To assist PHPStan when using invokable cases, you can include the PHPStan extensions into your own `phpstan.neon` file: