Skip to content

Commit

Permalink
document stringOptions()
Browse files Browse the repository at this point in the history
  • Loading branch information
stancl committed Jan 12, 2024
1 parent 6684067 commit e5259d2
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A collection of enum helpers for PHP.
- [`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 original idea on [Twitter](https://twitter.com/archtechx/status/1495158228757270528).

## Installation

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

#### stringOptions()

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", ', ');
```
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="ADMIN">Admin</option>
// <option value="GUEST">Guest</option>
Role::stringOptions(); // pure enum
```

### From

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

### Comparable

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

#### Apply the trait on your enum
```php
Expand Down

0 comments on commit e5259d2

Please sign in to comment.