@@ -143,66 +143,120 @@ $user->setStatus(ExtendedUserStatus::EXTENDED());
143
143
```
144
144
145
145
Now the setter receives a status it doesn't know about but allows it.
146
- If your ` User ` class doesn't allow it the following is the recommanded way:
146
+
147
+ #### Solution 1: Finilize the numeration
147
148
148
149
``` php
149
- class User
150
+ final class UserStatus extends Enum
150
151
{
151
152
// ...
153
+ }
154
+
155
+ class User
156
+ {
157
+ protected $status;
158
+
159
+ public function setStatus(UserStatus $status)
160
+ {
161
+ $this->status = $status;
162
+ }
163
+ }
164
+ ````
165
+
166
+ * Nice and obvious solution
167
+
168
+ * Resulting behaviour matches native enumeration implementation of most other languages (like Java)
169
+
170
+ But as this library emulates enumerations it has a view downsides:
171
+
172
+ * Enumerator values can not be used directly
173
+ * `$user->setStatus(UserStatus::ACTIVE)` fails
174
+ * `$user->setStatus(UserStatus::ACTIVE())` works
175
+
176
+ * Does not help if the enumeration was defined in an external library
177
+
178
+
179
+ #### Solution 2: Using `Enum::get()`
180
+
181
+ ```php
182
+ class User
183
+ {
152
184
public function setStatus($status)
153
185
{
154
186
$this->status = UserStatus::get($status);
155
187
}
156
- // ...
157
188
}
158
189
```
159
190
160
- Now you are 100% sure to work with an exact instance of ` UserStatus ` .
191
+ * Makes sure the resulting enumerator exactly matches an enumeration. (Inherited enumerators as not allowed).
192
+
193
+ * Allows enumerator values directly
194
+ * ` $user->setStatus(UserStatus::ACTIVE) ` works
195
+ * ` $user->setStatus(UserStatus::ACTIVE()) ` works
196
+
197
+ * Also works for enumerations defined in external libraries
198
+
199
+ But of course this solution has downsides, too:
161
200
162
- If the setter receives an ` ExtendedUserStatus ` an exception will be thrown
163
- because inheritance is not allowed with ` Enum::get ` .
201
+ * Looses declarative type-hint
202
+
203
+ * A bit slower
164
204
165
- On the same time this method will accept scalar values matching one of the
166
- defined values of ` UserStatus ` and returns an instance of it.
167
205
168
206
## EnumSet
169
207
170
208
An ` EnumSet ` groups enumerators of the same enumeration type together.
171
209
172
- It implements ` Iterator ` and ` Countable ` so it's simple to iterate the set
173
- or count all attached enumerators of it with ` foreach ` and ` count() ` .
210
+ It implements ` Iterator ` and ` Countable `
211
+ so elements can be iterated and counted like a normal array
212
+ using ` foreach ` and ` count() ` .
213
+
214
+ Internally it's based on a bitset. Integer bitset or binary bitset
215
+ depending on how many enumerators are defined for the given enumeration.
174
216
175
- Internally it's based on a bitset of a binary string so the order will be
176
- by the ordinal number by design.
217
+ Enumerators attached to an ` EnumSet ` are unique and ordered based on it's ordinal number by design.
177
218
178
219
``` php
179
220
use MabeEnum\EnumSet;
180
221
181
222
// create a new EnumSet
182
223
$enumSet = new EnumSet('UserStatus');
183
224
225
+
184
226
// attach enumerators (by value or by instance)
185
227
$enumSet->attach(UserStatus::INACTIVE);
186
228
$enumSet->attach(UserStatus::ACTIVE());
187
229
$enumSet->attach(UserStatus::DELETED());
188
230
231
+
189
232
// detach enumerators (by value or by instance)
190
233
$enumSet->detach(UserStatus::INACTIVE);
191
234
$enumSet->detach(UserStatus::DELETED());
192
235
236
+
193
237
// contains enumerators (by value or by instance)
194
238
$enumSet->contains(UserStatus::INACTIVE); // bool
195
239
240
+
196
241
// count number of attached enumerations
197
242
$enumSet->count();
198
243
count($enumSet);
199
244
245
+
200
246
// convert to array
201
247
$enumSet->getValues(); // List of enumerator values
202
248
$enumSet->getEnumerators(); // List of enumerator instances
203
249
$enumSet->getNames(); // List of enumerator names
204
250
$enumSet->getOrdinals(); // List of ordinal numbers
205
251
252
+
253
+ // iterating over the set
254
+ foreach ($enumSet as $ordinal => $enum) {
255
+ gettype($ordinal); // int (the ordinal number of the enumerator)
256
+ get_class($enum); // UserStatus (enumerator object)
257
+ }
258
+
259
+
206
260
// compare two EnumSets
207
261
$enumSet->isEqual($other); // Check if the EnumSet is the same as other
208
262
$enumSet->isSubset($other); // Check if the EnumSet is a subset of other
@@ -218,14 +272,17 @@ $enumSet->symDiff($other); // Produce a new set with enumerators in either thi
218
272
219
273
An ` EnumMap ` maps enumerators of the same type to data assigned to.
220
274
221
- Internally an ` EnumMap ` is based of ` SplObjectStorage ` .
275
+ It implements ` ArrayAccess ` , ` Countable ` and ` SeekableIterator `
276
+ so elements can be accessed, iterated and counted like a normal array
277
+ using ` $enumMap[$key] ` , ` foreach ` and ` count() ` .
222
278
223
279
``` php
224
280
use MabeEnum\EnumMap;
225
281
226
282
// create a new EnumMap
227
283
$enumMap = new EnumMap('UserStatus');
228
284
285
+
229
286
// read and write key-value-pairs like an array
230
287
$enumMap[UserStatus::INACTIVE] = 'inaktiv';
231
288
$enumMap[UserStatus::ACTIVE] = 'aktiv';
@@ -251,6 +308,11 @@ unset($enumMap[UserStatus::DELETED()]);
251
308
isset($enumMap[UserStatus::DELETED()]); // false
252
309
253
310
311
+ // count number of attached elements
312
+ $enumMap->count();
313
+ count($enumMap);
314
+
315
+
254
316
// support for null aware exists check
255
317
$enumMap[UserStatus::NULL] = null;
256
318
isset($enumMap[UserStatus::NULL]); // false
@@ -284,6 +346,9 @@ instance and you could result in two different instance of the same enumeration.
284
346
285
347
** Use it with caution!**
286
348
349
+ PS: ` EnumSet ` and ` EnumMap ` are serializable by default as long as you don't set other non-serializable values.
350
+
351
+
287
352
### Example of using EnumSerializableTrait
288
353
289
354
``` php
@@ -313,7 +378,7 @@ var_dump($north2->is($north1)); // returns TRUE - equality works in both directi
313
378
314
379
* ` SplEnum ` is not build-in into PHP and requires pecl extension installed.
315
380
* Instances of the same value of an ` SplEnum ` are not the same instance.
316
- * ` SplEnum ` doesn't have implemented ` EnumMap ` or ` EnumSet ` .
381
+ * No support for ` EnumMap ` or ` EnumSet ` .
317
382
318
383
319
384
# Install
0 commit comments