diff --git a/README.md b/README.md index 8350831..395ff12 100644 --- a/README.md +++ b/README.md @@ -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: