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

update stringOptions() examples to match the enums used in the README

This commit is contained in:
Samuel Štancl 2024-01-12 23:24:43 +01:00
parent e5259d2e4b
commit fc428d533d

View file

@ -194,18 +194,20 @@ Role::options(); // ['ADMINISTRATOR', 'SUBSCRIBER', 'GUEST']
The trait also adds the `stringOptions()` method that can be used for generating convenient string representations of your enum options:
```php
// First argument is the callback, second argument is glue
// returns "PENDING => 0, DONE => 1"
Status::stringOptions(fn ($name, $value) => "$name => $value", ', ');
// returns "INCOMPLETE => 0, COMPLETED => 1, CANCELED => 2"
TaskStatus::stringOptions(fn ($name, $value) => "$name => $value", ', ');
```
For pure enums (non-backed), the name is used in place of `$value` (meaning that both `$name` and `$value` are the same).
Both arguments for this method are optional, the glue defaults to `\n` and the callback defaults to generating HTML `<option>` tags:
```php
// <option value="0">Pending</option>
// <option value="1">Done</option>
Status::stringOptions(); // backed enum
// <option value="0">Incomplete</option>
// <option value="1">Completed</option>
// <option value="2">Canceled</option>
TaskStatus::stringOptions(); // backed enum
// <option value="ADMIN">Admin</option>
// <option value="ADMINISTRATOR">Administrator</option>
// <option value="Subscriber">Subscriber</option>
// <option value="GUEST">Guest</option>
Role::stringOptions(); // pure enum
```