Skip to content

Commit e5259d2

Browse files
committed
document stringOptions()
1 parent 6684067 commit e5259d2

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ A collection of enum helpers for PHP.
1010
- [`Metadata`](#metadata)
1111
- [`Comparable`](#comparable)
1212

13-
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.
13+
You can read more about the original idea on [Twitter](https://twitter.com/archtechx/status/1495158228757270528).
1414

1515
## Installation
1616

@@ -189,6 +189,27 @@ TaskStatus::options(); // ['INCOMPLETE' => 0, 'COMPLETED' => 1, 'CANCELED' => 2]
189189
Role::options(); // ['ADMINISTRATOR', 'SUBSCRIBER', 'GUEST']
190190
```
191191

192+
#### stringOptions()
193+
194+
The trait also adds the `stringOptions()` method that can be used for generating convenient string representations of your enum options:
195+
```php
196+
// First argument is the callback, second argument is glue
197+
// returns "PENDING => 0, DONE => 1"
198+
Status::stringOptions(fn ($name, $value) => "$name => $value", ', ');
199+
```
200+
For pure enums (non-backed), the name is used in place of `$value` (meaning that both `$name` and `$value` are the same).
201+
202+
Both arguments for this method are optional, the glue defaults to `\n` and the callback defaults to generating HTML `<option>` tags:
203+
```php
204+
// <option value="0">Pending</option>
205+
// <option value="1">Done</option>
206+
Status::stringOptions(); // backed enum
207+
208+
// <option value="ADMIN">Admin</option>
209+
// <option value="GUEST">Guest</option>
210+
Role::stringOptions(); // pure enum
211+
```
212+
192213
### From
193214

194215
This helper adds `from()` and `tryFrom()` to pure enums, and adds `fromName()` and `tryFromName()` to all enums.
@@ -369,7 +390,7 @@ And if you're using the same meta property in multiple enums, you can create a d
369390

370391
### Comparable
371392

372-
This helper lets you compare enums by `is()`, `isNot()`, `in()` and `notIn()` operators.
393+
This trait lets you compare enums using `is()`, `isNot()`, `in()` and `notIn()`.
373394

374395
#### Apply the trait on your enum
375396
```php

0 commit comments

Comments
 (0)