You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[](https://packagist.org/packages/ekvedaras/php-enum)
Original idea taken from [happy-types/enumerable-type](https://packagist.org/packages/happy-types/enumerable-type), so take a look, it may suit your needs better.
Big thanks [happy-types/enumerable-type](https://packagist.org/packages/happy-types/enumerable-type) for the original idea. Take a look if it suits your needs better.
11
+
9
12
This package adds `meta` field, provides a few more methods like `options`, `keys`, `json`, etc.
10
13
and there are `array` and illuminate `collection` implementations to choose from.
11
14
@@ -15,9 +18,10 @@ and there are `array` and illuminate `collection` implementations to choose from
15
18
* By type hinting forces only allowed values to be passed to methods (or returned)
16
19
* Easy way to list all possible values
17
20
* More feature rich and flexible then other enum implementations
21
+
* Works with strict (`===`) operator
18
22
* IDE friendly, so auto complete, usage analysis and refactorings all work
19
23
20
-
## Usage
24
+
## Defining enums
21
25
22
26
Create enums by extending either `EKvedaras\PHPEnum\PHPArray\Enum` or `EKvedaras\PHPEnum\Illuminate\Collection\Enum`.
23
27
@@ -26,7 +30,8 @@ Create enums by extending either `EKvedaras\PHPEnum\PHPArray\Enum` or `EKvedaras
26
30
27
31
use EKvedaras\PHPEnum\PHPArray\Enum;
28
32
29
-
class PaymentStatus extends Enum {
33
+
class PaymentStatus extends Enum
34
+
{
30
35
/**
31
36
* @return static
32
37
*/
@@ -52,3 +57,340 @@ class PaymentStatus extends Enum {
52
57
}
53
58
}
54
59
```
60
+
61
+
Integers can be used as IDs instead of string values if you prefer.
62
+
63
+
## Usage
64
+
65
+
### Retrieving and comparing enum values
66
+
67
+
```php
68
+
// Retrieving value statically
69
+
$status1 = PaymentStatus::completed();
70
+
71
+
// Retrieving value dynamically from ID
72
+
$status2 = PaymentStatus::from('completed');
73
+
74
+
// Strict comparison is supported
75
+
var_dump($status1 === $status2); // true
76
+
```
77
+
78
+
### Accessing value properties
79
+
80
+
```php
81
+
$status = PaymentStatus::copmleted();
82
+
83
+
$status->id(); // 'completed'
84
+
$status->name(); // 'Payment has been processed'
85
+
$status->meta(); // null
86
+
```
87
+
88
+
### Listing enum values
89
+
90
+
There are two implementations provided:
91
+
92
+
#### PHP array
93
+
94
+
To use PHP array your enums should extend `EKvedaras\PHPEnum\PHPArray\Enum` class
95
+
96
+
```php
97
+
var_dump(PaymentStatus::enum());
98
+
/*
99
+
array(3) {
100
+
'pending' =>
101
+
class PaymentStatus#12 (3) {
102
+
protected $id =>
103
+
string(7) "pending"
104
+
protected $name =>
105
+
string(18) "Payment is pending"
106
+
protected $meta =>
107
+
NULL
108
+
}
109
+
'completed' =>
110
+
class PaymentStatus#363 (3) {
111
+
protected $id =>
112
+
string(9) "completed"
113
+
protected $name =>
114
+
string(26) "Payment has been processed"
115
+
protected $meta =>
116
+
NULL
117
+
}
118
+
'failed' =>
119
+
class PaymentStatus#13 (3) {
120
+
protected $id =>
121
+
string(6) "failed"
122
+
protected $name =>
123
+
string(18) "Payment has failed"
124
+
protected $meta =>
125
+
NULL
126
+
}
127
+
}
128
+
*/
129
+
```
130
+
131
+
```php
132
+
var_dump(PaymentStatus::options());
133
+
/*
134
+
array(3) {
135
+
'pending' =>
136
+
string(18) "Payment is pending"
137
+
'completed' =>
138
+
string(26) "Payment has been processed"
139
+
'failed' =>
140
+
string(18) "Payment has failed"
141
+
}
142
+
*/
143
+
```
144
+
145
+
```php
146
+
var_dump(PaymentStatus::keys());
147
+
/*
148
+
array(3) {
149
+
[0] =>
150
+
string(7) "pending"
151
+
[1] =>
152
+
string(9) "completed"
153
+
[2] =>
154
+
string(6) "failed"
155
+
}
156
+
*/
157
+
```
158
+
159
+
```php
160
+
var_dump(PaymentStatus::json()); // Will include meta if defined
161
+
```
162
+
```json
163
+
{
164
+
"pending": {
165
+
"id": "pending",
166
+
"name": "Payment is pending"
167
+
},
168
+
"completed": {
169
+
"id": "completed",
170
+
"name": "Payment has been processed"
171
+
},
172
+
"failed": {
173
+
"id": "failed",
174
+
"name": "Payment has failed"
175
+
}
176
+
}
177
+
```
178
+
179
+
```php
180
+
var_dump(PaymentStatus::jsonOptions());
181
+
```
182
+
```json
183
+
{
184
+
"pending": "Payment is pending",
185
+
"completed": "Payment has been processed",
186
+
"failed": "Payment has failed"
187
+
}
188
+
```
189
+
190
+
#### Illuminate Collection
191
+
192
+
**Either `illuminate/support` or `illuminate/collections` package is required which is not installed by default.**
193
+
194
+
To use Illuminate Collection your enums should extend `EKvedaras\PHPEnum\Illuminate\Collection\Enum` class.
195
+
196
+
The only difference is `enum`, `options` and `keys` methods will return `Collection` instances instead of arrays.
197
+
The rest works exactly the same.
198
+
199
+
```php
200
+
var_dump(PaymentStatus::enum());
201
+
/*
202
+
class Illuminate\Support\Collection#362 (1) {
203
+
protected $items =>
204
+
array(3) {
205
+
'pending' =>
206
+
class PaymentStatus#12 (3) {
207
+
protected $id =>
208
+
string(7) "pending"
209
+
protected $name =>
210
+
string(18) "Payment is pending"
211
+
protected $meta =>
212
+
NULL
213
+
}
214
+
'completed' =>
215
+
class PaymentStatus#363 (3) {
216
+
protected $id =>
217
+
string(9) "completed"
218
+
protected $name =>
219
+
string(26) "Payment has been processed"
220
+
protected $meta =>
221
+
NULL
222
+
}
223
+
'failed' =>
224
+
class PaymentStatus#13 (3) {
225
+
protected $id =>
226
+
string(6) "failed"
227
+
protected $name =>
228
+
string(18) "Payment has failed"
229
+
protected $meta =>
230
+
NULL
231
+
}
232
+
}
233
+
}
234
+
*/
235
+
```
236
+
237
+
```php
238
+
var_dump(PaymentStatus::options());
239
+
/*
240
+
class Illuminate\Support\Collection#368 (1) {
241
+
protected $items =>
242
+
array(3) {
243
+
'pending' =>
244
+
string(18) "Payment is pending"
245
+
'completed' =>
246
+
string(26) "Payment has been processed"
247
+
'failed' =>
248
+
string(18) "Payment has failed"
249
+
}
250
+
}
251
+
*/
252
+
```
253
+
254
+
```php
255
+
var_dump(PaymentStatus::keys());
256
+
/*
257
+
class Illuminate\Support\Collection#13 (1) {
258
+
protected $items =>
259
+
array(3) {
260
+
[0] =>
261
+
string(7) "pending"
262
+
[1] =>
263
+
string(9) "completed"
264
+
[2] =>
265
+
string(6) "failed"
266
+
}
267
+
}
268
+
*/
269
+
```
270
+
271
+
### Meta
272
+
273
+
Meta field is intentionally left as mixed type as it could be used for various reasons.
274
+
For example linking enum options with a specific implementation:
0 commit comments