|
1 | 1 | # Enums |
2 | 2 |
|
3 | | -A collection\* of enum helpers for PHP. |
| 3 | +A collection of enum helpers for PHP. |
4 | 4 |
|
5 | | -\* Currently there's only one helper — [`InvokableCases`](#invokablecases) — but the goal of the package is to provide general purpose enum helpers. |
| 5 | +- [`InvokableCases`](#invokablecases) |
| 6 | +- [`Names`](#names) |
| 7 | +- [`Values`](#values) |
| 8 | +- [`Options`](#options) |
6 | 9 |
|
7 | | -You can read more about the idea on [Twitter](https://twitter.com/archtechx/status/1495158228757270528). I originally wanted to include that 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. |
| 10 | +You can read more about the idea on [Twitter](https://twitter.com/archtechx/status/1495158228757270528). I originally wanted to include the 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. |
8 | 11 |
|
9 | 12 | ## Installation |
10 | 13 |
|
@@ -72,6 +75,75 @@ public function updateStatus(TaskStatus $status) |
72 | 75 | } |
73 | 76 | ``` |
74 | 77 |
|
| 78 | +### Names |
| 79 | + |
| 80 | +This helper returns a list of case *names* in the enum. |
| 81 | + |
| 82 | +#### Apply the trait on your enum |
| 83 | +```php |
| 84 | +use ArchTech\Enums\Names; |
| 85 | + |
| 86 | +enum TaskStatus: int |
| 87 | +{ |
| 88 | + use Names; |
| 89 | + |
| 90 | + case INCOMPLETE = 0; |
| 91 | + case COMPLETED = 1; |
| 92 | + case CANCELED = 2; |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +#### Use the `names()` method |
| 97 | +```php |
| 98 | +TaskStatus::names(); // ['INCOMPLETE', 'COMPLETED', 'CANCELED'] |
| 99 | +``` |
| 100 | + |
| 101 | +### Values |
| 102 | + |
| 103 | +This helper returns a list of case *values* in the enum. |
| 104 | + |
| 105 | +#### Apply the trait on your enum |
| 106 | +```php |
| 107 | +use ArchTech\Enums\Values; |
| 108 | + |
| 109 | +enum TaskStatus: int |
| 110 | +{ |
| 111 | + use Values; |
| 112 | + |
| 113 | + case INCOMPLETE = 0; |
| 114 | + case COMPLETED = 1; |
| 115 | + case CANCELED = 2; |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +#### Use the `values()` method |
| 120 | +```php |
| 121 | +TaskStatus::values(); // [0, 1, 2] |
| 122 | +``` |
| 123 | + |
| 124 | +### Options |
| 125 | + |
| 126 | +This helper returns an associative array of case names and values. |
| 127 | + |
| 128 | +#### Apply the trait on your enum |
| 129 | +```php |
| 130 | +use ArchTech\Enums\Options; |
| 131 | + |
| 132 | +enum TaskStatus: int |
| 133 | +{ |
| 134 | + use Options; |
| 135 | + |
| 136 | + case INCOMPLETE = 0; |
| 137 | + case COMPLETED = 1; |
| 138 | + case CANCELED = 2; |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +#### Use the `options()` method |
| 143 | +```php |
| 144 | +TaskStatus::options(); // ['INCOMPLETE' => 0, 'COMPLETED' => 1, 'CANCELED' => 2] |
| 145 | +``` |
| 146 | + |
75 | 147 | ## Development |
76 | 148 |
|
77 | 149 | Run all checks locally: |
|
0 commit comments