Skip to content

Commit 76dc01c

Browse files
committed
docs: add examples
1 parent a691078 commit 76dc01c

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

README.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
Collection of utilities for working with enums.
88

9-
[Contributing](#contributing) | [Feedback](#feedback) | [License](#license) | [About Code for Romania](#about-code-for-romania)
9+
[Contributing](#contributing) | [Install](#install) | [Usage](#usage) | [Feedback](#feedback) | [License](#license) | [About Code for Romania](#about-code-for-romania)
1010

1111
## Contributing
1212

@@ -16,6 +16,76 @@ Help us out by testing this project in the [staging environment][link-staging].
1616

1717
If you would like to suggest new functionality, open an Issue and mark it as a __[Feature request]__. Please be specific about why you think this functionality will be of use. If you can, please include some visual description of what you would like the UI to look like, if you are suggesting new UI elements.
1818

19+
## Install
20+
21+
```console
22+
composer require commitglobal/enum-utils
23+
```
24+
25+
## Usage
26+
27+
### Arrayable
28+
29+
> [!important]
30+
> For `Arrayable::options()`, your enum must implement a `getLabel()` method.
31+
32+
| Method | Description |
33+
| ----------- | ------------------------------------------------------ |
34+
| `names()` | Returns an array of enum case names. |
35+
| `values()` | Returns an array of enum case values. |
36+
| `options()` | Returns an associative array mapping values to labels. |
37+
38+
```php
39+
enum Status: string
40+
{
41+
use CommitGlobal\Enums\Concerns\Arrayable;
42+
43+
case ACTIVE = 'active';
44+
case INACTIVE = 'inactive';
45+
46+
public function getLabel(): string
47+
{
48+
return match($this) {
49+
self::ACTIVE => 'Account is active',
50+
self::INACTIVE => 'Account is inactive',
51+
};
52+
}
53+
}
54+
```
55+
56+
```php
57+
Status::names(); // ['ACTIVE', 'INACTIVE']
58+
Status::values(); // ['active', 'inactive']
59+
Status::options(); // ['active' => 'Account is active', 'inactive' => 'Account is inactive']
60+
```
61+
62+
### Comparable
63+
64+
| Method | Description |
65+
| -------------- | ----------------------------------------------------------------- |
66+
| `is($enum)` | Check if this enum matches the given enum instance or value. |
67+
| `isNot($enum)` | Check if this enum does't match the given enum instance or value. |
68+
69+
#### Example
70+
```php
71+
enum Status: string
72+
{
73+
use CommitGlobal\Enums\Concerns\Comparable;
74+
75+
case ACTIVE = 'active';
76+
case INACTIVE = 'inactive';
77+
}
78+
```
79+
80+
```php
81+
$status = Status::ACTIVE;
82+
83+
$status->is(Status::ACTIVE); // true
84+
$status->is('active'); // true
85+
$status->is(Status::INACTIVE); // false
86+
$status->isNot('inactive'); // true
87+
```
88+
1989
## Feedback
2090

2191
* Request a new feature on GitHub.

0 commit comments

Comments
 (0)