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
@@ -187,7 +187,7 @@ class User
187
187
}
188
188
```
189
189
190
- * Makes sure the resulting enumerator exactly matches an enumeration. (Inherited enumerators as not allowed).
190
+ * Makes sure the resulting enumerator exactly matches an enumeration. (Inherited enumerators are not allowed).
191
191
192
192
* Allows enumerator values directly
193
193
* ` $user->setStatus(UserStatus::ACTIVE) ` works
@@ -204,40 +204,61 @@ 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 ` or ` remove ` where immutable methods starts with ` with ` .
217
220
218
221
``` php
219
222
use MabeEnum\EnumSet;
220
223
221
- // create a new EnumSet
222
- $enumSet = new EnumSet('UserStatus');
224
+ // create a new EnumSet and initialize with the given enumerators
225
+ $enumSet = new EnumSet('UserStatus', [UserStatus::ACTIVE()] );
223
226
227
+ // modify an EnumSet (mutable interface)
224
228
225
- // attach enumerators (by value or by instance)
226
- $enumSet->attach(UserStatus::INACTIVE);
227
- $enumSet->attach(UserStatus::ACTIVE());
228
- $enumSet->attach(UserStatus::DELETED());
229
+ // add enumerators (by value or by instance)
230
+ $enumSet->addIterable([UserStatus::INACTIVE, UserStatus::DELETED()]);
231
+ // or
232
+ $enumSet->add(UserStatus::INACTIVE);
233
+ $enumSet->add(UserStatus::DELETED());
229
234
235
+ // remove enumerators (by value or by instance)
236
+ $enumSet->removeIterable([UserStatus::INACTIVE, UserStatus::DELETED()]);
237
+ // or
238
+ $enumSet->remove(UserStatus::INACTIVE);
239
+ $enumSet->remove(UserStatus::DELETED());
230
240
231
- // detach enumerators (by value or by instance)
232
- $enumSet->detach(UserStatus::INACTIVE);
233
- $enumSet->detach(UserStatus::DELETED());
234
241
242
+ // The immutable interface will create a new EnumSet for each modification
235
243
236
- // contains enumerators (by value or by instance)
237
- $enumSet->contains(UserStatus::INACTIVE); // bool
244
+ // add enumerators (by value or by instance)
245
+ $enumSet = $enumSet->withIterable([UserStatus::INACTIVE, UserStatus::DELETED()]);
246
+ // or
247
+ $enumSet = $enumSet->with(UserStatus::INACTIVE);
248
+ $enumSet = $enumSet->with(UserStatus::DELETED());
238
249
250
+ // remove enumerators (by value or by instance)
251
+ $enumSet->withoutIterable([UserStatus::INACTIVE, UserStatus::DELETED()]);
252
+ // or
253
+ $enumSet = $enumSet->without(UserStatus::INACTIVE);
254
+ $enumSet = $enumSet->without(UserStatus::DELETED());
239
255
240
- // count number of attached enumerations
256
+
257
+ // Tests if an enumerator exists (by value or by instance)
258
+ $enumSet->has(UserStatus::INACTIVE); // bool
259
+
260
+
261
+ // count the number of enumerators
241
262
$enumSet->count();
242
263
count($enumSet);
243
264
@@ -261,17 +282,28 @@ $enumSet->isEqual($other); // Check if the EnumSet is the same as other
261
282
$enumSet->isSubset($other); // Check if the EnumSet is a subset of other
262
283
$enumSet->isSuperset($other); // Check if the EnumSet is a superset of other
263
284
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)
285
+
286
+ // union, intersect, difference and symmetric difference
287
+
288
+ // ... the mutable interface will modify the set
289
+ $enumSet->setUnion($other); // Enumerators from both this and other (this | other)
290
+ $enumSet->setIntersect($other); // Enumerators common to both this and other (this & other)
291
+ $enumSet->setDiff($other); // Enumerators in this but not in other (this - other)
292
+ $enumSet->setSymDiff($other); // Enumerators in either this and other but not in both (this ^ other)
293
+
294
+ // ... the immutable interface will produce a new set
295
+ $enumSet = $enumSet->withUnion($other); // Enumerators from both this and other (this | other)
296
+ $enumSet = $enumSet->withIntersect($other); // Enumerators common to both this and other (this & other)
297
+ $enumSet = $enumSet->withDiff($other); // Enumerators in this but not in other (this - other)
298
+ $enumSet = $enumSet->withSymDiff($other); // Enumerators in either this and other but not in both (this ^ other)
268
299
```
269
300
301
+
270
302
## EnumMap
271
303
272
304
An ` EnumMap ` maps enumerators of the same type to data assigned to.
273
305
274
- It implements ` ArrayAccess ` , ` Countable ` and ` SeekableIterator `
306
+ It implements ` ArrayAccess ` , ` Countable ` and ` IteratorAggregate `
275
307
so elements can be accessed, iterated and counted like a normal array
276
308
using ` $enumMap[$key] ` , ` foreach ` and ` count() ` .
277
309
@@ -314,14 +346,14 @@ count($enumMap);
314
346
315
347
// support for null aware exists check
316
348
$enumMap[UserStatus::NULL] = null;
317
- isset($enumMap[UserStatus::NULL]); // false
318
- $enumMap->contains (UserStatus::NULL); // true
349
+ isset($enumMap[UserStatus::NULL]); // false
350
+ $enumMap->has (UserStatus::NULL); // true
319
351
320
352
321
353
// iterating over the map
322
354
foreach ($enumMap as $enum => $value) {
323
355
get_class($enum); // UserStatus (enumerator object)
324
- gettype($value); // string (the value the enumerators maps to)
356
+ gettype($value); // mixed (the value the enumerators maps to)
325
357
}
326
358
327
359
// get a list of keys (= a list of enumerator objects)
@@ -331,6 +363,7 @@ $enumMap->getKeys();
331
363
$enumMap->getValues();
332
364
```
333
365
366
+
334
367
## Serializing
335
368
336
369
Because this enumeration implementation is based on a singleton pattern and in PHP
@@ -373,13 +406,19 @@ var_dump($north1->is($north2)); // returns TRUE - this way the two instances are
373
406
var_dump($north2->is($north1)); // returns TRUE - equality works in both directions
374
407
```
375
408
409
+
376
410
# Why not ` SplEnum `
377
411
378
412
* ` SplEnum ` is not build-in into PHP and requires pecl extension installed.
379
413
* Instances of the same value of an ` SplEnum ` are not the same instance.
380
414
* No support for ` EnumMap ` or ` EnumSet ` .
381
415
382
416
417
+ # Changelog
418
+
419
+ Changes are documented in the (release page)[ https://github.com/marc-mabe/php-enum/releases ] .
420
+
421
+
383
422
# Install
384
423
385
424
## Composer
0 commit comments