Skip to content

Commit d7d8e85

Browse files
authored
Merge pull request #123 from marc-mabe/4.x
Branch 4.x as master
2 parents 61b680b + 9fb2c1f commit d7d8e85

18 files changed

+2095
-1496
lines changed

.travis.yml

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,14 @@ env:
1616
matrix:
1717
fast_finish: true
1818
include:
19-
- php: 5.6
20-
env:
21-
- CODE_COVERAGE="1"
22-
- php: 7.0
23-
env:
24-
- CODE_COVERAGE="1"
25-
- PHPDOC="1"
26-
- php: 7.1
27-
env:
28-
- CODE_COVERAGE="1"
2919
- php: 7.2
3020
env:
3121
- CODE_COVERAGE="1"
22+
- PHPDOC="1"
3223
- php: 7.3
3324
env:
3425
- CODE_COVERAGE="1"
3526
- php: nightly
36-
37-
# HHVM is no longer supported on Ubuntu Precise. Please consider using Trusty with `dist: trusty`.
38-
- php: hhvm-3.27 # LTS
39-
dist: trusty
40-
- php: hhvm-3.30 # last with PHP support
41-
dist: trusty
4227
allow_failures:
4328
- php: nightly
4429

@@ -62,6 +47,9 @@ script:
6247
php -d 'zend.assertions=1' vendor/bin/phpunit --verbose;
6348
fi
6449

50+
# run benchmarks to make sure they are working fine
51+
- php vendor/bin/phpbench run --no-interaction --revs=1 --retry-threshold=100
52+
6553
after_script:
6654
- if [ "${CODE_COVERAGE}" == "1" ]; then
6755
php ocular.phar code-coverage:upload --format=php-clover coverage.clover;

README.md

Lines changed: 68 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![Total Downloads](https://poser.pugx.org/marc-mabe/php-enum/downloads.png)](https://packagist.org/packages/marc-mabe/php-enum)
66
[![Latest Stable](https://poser.pugx.org/marc-mabe/php-enum/v/stable.png)](https://packagist.org/packages/marc-mabe/php-enum)
77

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.
99
It's an abstract class that needs to be extended to use it.
1010

1111

@@ -187,7 +187,7 @@ class User
187187
}
188188
```
189189

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).
191191

192192
* Allows enumerator values directly
193193
* `$user->setStatus(UserStatus::ACTIVE)` works
@@ -204,40 +204,61 @@ But of course this solution has downsides, too:
204204

205205
## EnumSet
206206

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.
208210

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.
212215

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()`.
215217

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`.
217220

218221
```php
219222
use MabeEnum\EnumSet;
220223

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()]);
223226

227+
// modify an EnumSet (mutable interface)
224228

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());
229234

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());
230240

231-
// detach enumerators (by value or by instance)
232-
$enumSet->detach(UserStatus::INACTIVE);
233-
$enumSet->detach(UserStatus::DELETED());
234241

242+
// The immutable interface will create a new EnumSet for each modification
235243

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());
238249

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());
239255

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
241262
$enumSet->count();
242263
count($enumSet);
243264

@@ -261,17 +282,28 @@ $enumSet->isEqual($other); // Check if the EnumSet is the same as other
261282
$enumSet->isSubset($other); // Check if the EnumSet is a subset of other
262283
$enumSet->isSuperset($other); // Check if the EnumSet is a superset of other
263284

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)
268299
```
269300

301+
270302
## EnumMap
271303

272304
An `EnumMap` maps enumerators of the same type to data assigned to.
273305

274-
It implements `ArrayAccess`, `Countable` and `SeekableIterator`
306+
It implements `ArrayAccess`, `Countable` and `IteratorAggregate`
275307
so elements can be accessed, iterated and counted like a normal array
276308
using `$enumMap[$key]`, `foreach` and `count()`.
277309

@@ -314,14 +346,14 @@ count($enumMap);
314346

315347
// support for null aware exists check
316348
$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
319351

320352

321353
// iterating over the map
322354
foreach ($enumMap as $enum => $value) {
323355
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)
325357
}
326358

327359
// get a list of keys (= a list of enumerator objects)
@@ -331,6 +363,7 @@ $enumMap->getKeys();
331363
$enumMap->getValues();
332364
```
333365

366+
334367
## Serializing
335368

336369
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
373406
var_dump($north2->is($north1)); // returns TRUE - equality works in both directions
374407
```
375408

409+
376410
# Why not `SplEnum`
377411

378412
* `SplEnum` is not build-in into PHP and requires pecl extension installed.
379413
* Instances of the same value of an `SplEnum` are not the same instance.
380414
* No support for `EnumMap` or `EnumSet`.
381415

382416

417+
# Changelog
418+
419+
Changes are documented in the (release page)[https://github.com/marc-mabe/php-enum/releases].
420+
421+
383422
# Install
384423

385424
## Composer

0 commit comments

Comments
 (0)