5
5
[ ![ Total Downloads] ( https://poser.pugx.org/marc-mabe/php-enum/downloads.png )] ( https://packagist.org/packages/marc-mabe/php-enum )
6
6
[ ![ Latest Stable] ( https://poser.pugx.org/marc-mabe/php-enum/v/stable.png )] ( https://packagist.org/packages/marc-mabe/php-enum )
7
7
8
- This is a native PHP implementation to add enumeration support to PHP >= 5.3 .
8
+ This is a native PHP implementation to add enumeration support to PHP.
9
9
It's an abstract class that needs to be extended to use it.
10
10
11
11
@@ -204,40 +204,62 @@ But of course this solution has downsides, too:
204
204
205
205
## EnumSet
206
206
207
- An ` EnumSet ` groups enumerators of the same enumeration type together.
207
+ An ` EnumSet ` is a specialized Set implementation for use with enumeration types.
208
+ All of the enumerators in an ` EnumSet ` must come from a single enumeration type that is specified, when the set is
209
+ created.
208
210
209
- It implements ` Iterator ` and ` Countable `
210
- so elements can be iterated and counted like a normal array
211
- using ` foreach ` and ` count() ` .
211
+ Enum sets are represented internally as bit vectors. The bit vektor is eigther an integer type or a binary string type
212
+ depending on how many enumerators are defined is the enumeration type. This representation is extremely compact and
213
+ efficient. Bulk operations will run very quickly. Enumerators of an ` EnumSet ` are unique and ordered based on it's
214
+ ordinal number by design.
212
215
213
- Internally it's based on a bitset. Integer bitset or binary bitset
214
- depending on how many enumerators are defined for the given enumeration.
216
+ It implements ` IteratorAggregate ` and ` Countable ` to be directly iterable with ` foreach ` and countable with ` count() ` .
215
217
216
- Enumerators attached to an ` EnumSet ` are unique and ordered based on it's ordinal number by design.
218
+ The ` EnumSet ` has a mutable and an immutable interface.
219
+ Mutable methods starts with ` set ` , ` attach ` and ` detach ` .
220
+ Immutable methods starts with ` with ` or ` without ` .
217
221
218
222
``` php
219
223
use MabeEnum\EnumSet;
220
224
221
- // create a new EnumSet
222
- $enumSet = new EnumSet('UserStatus');
225
+ // create a new EnumSet and initialize with the given enumerators
226
+ $enumSet = new EnumSet('UserStatus', [UserStatus::ACTIVE()] );
223
227
228
+ // modify an EnumSet (mutable interface)
224
229
225
230
// attach enumerators (by value or by instance)
226
- $enumSet->attach(UserStatus::INACTIVE);
227
- $enumSet->attach(UserStatus::ACTIVE());
228
- $enumSet->attach(UserStatus::DELETED());
231
+ $enumSet->attachEnumerators([UserStatus::INACTIVE, UserStatus::DELETED()]);
232
+ // or
233
+ $enumSet->attachEnumerator(UserStatus::INACTIVE);
234
+ $enumSet->attachEnumerator(UserStatus::DELETED());
229
235
236
+ // detach enumerators (by value or by instance)
237
+ $enumSet->detachEnumerators([UserStatus::INACTIVE, UserStatus::DELETED()]);
238
+ // or
239
+ $enumSet->detachEnumerator(UserStatus::INACTIVE);
240
+ $enumSet->detachEnumerator(UserStatus::DELETED());
241
+
242
+
243
+ // The immutable interface will create a new EnumSet for each modification
244
+
245
+ // add enumerators (by value or by instance)
246
+ $enumSet = $enumSet->withEnumerators([UserStatus::INACTIVE, UserStatus::DELETED()]);
247
+ // or
248
+ $enumSet = $enumSet->withEnumerator(UserStatus::INACTIVE);
249
+ $enumSet = $enumSet->withEnumerator(UserStatus::DELETED());
230
250
231
251
// detach enumerators (by value or by instance)
232
- $enumSet->detach(UserStatus::INACTIVE);
233
- $enumSet->detach(UserStatus::DELETED());
252
+ $enumSet->withoutEnumerators([UserStatus::INACTIVE, UserStatus::DELETED()]);
253
+ // or
254
+ $enumSet = $enumSet->withEnumerator(UserStatus::INACTIVE);
255
+ $enumSet = $enumSet->withEnumerator(UserStatus::DELETED());
234
256
235
257
236
258
// contains enumerators (by value or by instance)
237
259
$enumSet->contains(UserStatus::INACTIVE); // bool
238
260
239
261
240
- // count number of attached enumerations
262
+ // count the number of enumerators
241
263
$enumSet->count();
242
264
count($enumSet);
243
265
@@ -261,12 +283,23 @@ $enumSet->isEqual($other); // Check if the EnumSet is the same as other
261
283
$enumSet->isSubset($other); // Check if the EnumSet is a subset of other
262
284
$enumSet->isSuperset($other); // Check if the EnumSet is a superset of other
263
285
264
- $enumSet->union($other); // Produce a new set with enumerators from both this and other (this | other)
265
- $enumSet->intersect($other); // Produce a new set with enumerators common to both this and other (this & other)
266
- $enumSet->diff($other); // Produce a new set with enumerators in this but not in other (this - other)
267
- $enumSet->symDiff($other); // Produce a new set with enumerators in either this and other but not in both (this ^ other)
286
+
287
+ // union, intersect, difference and symmetric difference
288
+
289
+ // ... the mutable interface will modify the set
290
+ $enumSet->setUnion($other); // Enumerators from both this and other (this | other)
291
+ $enumSet->setIntersect($other); // Enumerators common to both this and other (this & other)
292
+ $enumSet->setDiff($other); // Enumerators in this but not in other (this - other)
293
+ $enumSet->setSymDiff($other); // Enumerators in either this and other but not in both (this ^ other)
294
+
295
+ // ... the immutable interface will produce a new set
296
+ $enumSet = $enumSet->withUnion($other); // Enumerators from both this and other (this | other)
297
+ $enumSet = $enumSet->withIntersect($other); // Enumerators common to both this and other (this & other)
298
+ $enumSet = $enumSet->withDiff($other); // Enumerators in this but not in other (this - other)
299
+ $enumSet = $enumSet->withSymDiff($other); // Enumerators in either this and other but not in both (this ^ other)
268
300
```
269
301
302
+
270
303
## EnumMap
271
304
272
305
An ` EnumMap ` maps enumerators of the same type to data assigned to.
@@ -331,6 +364,7 @@ $enumMap->getKeys();
331
364
$enumMap->getValues();
332
365
```
333
366
367
+
334
368
## Serializing
335
369
336
370
Because this enumeration implementation is based on a singleton pattern and in PHP
@@ -373,6 +407,7 @@ var_dump($north1->is($north2)); // returns TRUE - this way the two instances are
373
407
var_dump($north2->is($north1)); // returns TRUE - equality works in both directions
374
408
```
375
409
410
+
376
411
# Why not ` SplEnum `
377
412
378
413
* ` SplEnum ` is not build-in into PHP and requires pecl extension installed.
0 commit comments