@@ -42,25 +42,23 @@ class UserStatus extends Enum
4242 const ACTIVE = 'a';
4343 const DELETED = 'd';
4444
45- // all scalar datatypes are supported
45+ // all scalar data types and arrays are supported as enumerator values
4646 const NIL = null;
4747 const BOOLEAN = true;
4848 const INT = 1234;
4949 const STR = 'string';
5050 const FLOAT = 0.123;
51-
52- // Arrays are supported since PHP-5.6
53- const ARR = array('this', 'is', array('an', 'array'));
51+ const ARR = ['this', 'is', ['an', 'array']];
5452
5553 // Enumerators will be generated from public constants only
5654 public const PUBLIC_CONST = 'public constant'; // this will be an enumerator
5755 protected const PROTECTED_CONST = 'protected constant'; // this will NOT be an enumerator
5856 private const PRIVATE_CONST = 'private constant'; // this will NOT be an enumerator
5957
6058 // works since PHP-7.0 - see https://wiki.php.net/rfc/context_sensitive_lexer
61- const TRUE = true;
62- const FALSE = false;
63- const NULL = null;
59+ const TRUE = ' true' ;
60+ const FALSE = ' false' ;
61+ const NULL = ' null' ;
6462 const PUBLIC = 'public';
6563 const PRIVATE = 'private';
6664 const PROTECTED = 'protected';
@@ -85,11 +83,11 @@ $status->getName(); // returns the selected constant name
8583$status->getOrdinal(); // returns the ordinal number of the selected constant
8684
8785// basic methods to list defined enumerators
88- UserStatus::getEnumerators() // returns a list of enumerator instances
89- UserStatus::getValues() // returns a list of enumerator values
90- UserStatus::getNames() // returns a list of enumerator names
91- UserStatus::getOrdinals() // returns a list of ordinal numbers
92- UserStatus::getConstants() // returns an associative array of enumerator names to enumerator values
86+ UserStatus::getEnumerators(); // returns a list of enumerator instances
87+ UserStatus::getValues(); // returns a list of enumerator values
88+ UserStatus::getNames(); // returns a list of enumerator names
89+ UserStatus::getOrdinals(); // returns a list of ordinal numbers
90+ UserStatus::getConstants(); // returns an associative array of enumerator names to enumerator values
9391
9492// same enumerators (of the same enumeration class) holds the same instance
9593UserStatus::get(UserStatus::ACTIVE) === UserStatus::ACTIVE()
@@ -135,11 +133,12 @@ Because in normal OOP the above example allows `UserStatus` and types inherited
135133Please think about the following example:
136134
137135``` php
138- class ExtendedUserStatus
136+ class ExtendedUserStatus extends UserStatus
139137{
140138 const EXTENDED = 'extended';
141139}
142140
141+ $user = new User();
143142$user->setStatus(ExtendedUserStatus::EXTENDED());
144143```
145144
@@ -209,10 +208,10 @@ $enumSet->isEqual($other); // Check if the EnumSet is the same as other
209208$enumSet->isSubset($other); // Check if the EnumSet is a subset of other
210209$enumSet->isSuperset($other); // Check if the EnumSet is a superset of other
211210
212- $enumSet->union($other[, ...] ); // Produce a new set with enumerators from both this and other (this | other)
213- $enumSet->intersect($other[, ...] ); // Produce a new set with enumerators common to both this and other (this & other)
214- $enumSet->diff($other[, ...] ); // Produce a new set with enumerators in this but not in other (this - other)
215- $enumSet->symDiff($other[, ...] ); // Produce a new set with enumerators in either this and other but not in both (this ^ ( other | other) )
211+ $enumSet->union($other); // Produce a new set with enumerators from both this and other (this | other)
212+ $enumSet->intersect($other); // Produce a new set with enumerators common to both this and other (this & other)
213+ $enumSet->diff($other); // Produce a new set with enumerators in this but not in other (this - other)
214+ $enumSet->symDiff($other); // Produce a new set with enumerators in either this and other but not in both (this ^ other)
216215```
217216
218217## EnumMap
@@ -227,20 +226,48 @@ use MabeEnum\EnumMap;
227226// create a new EnumMap
228227$enumMap = new EnumMap('UserStatus');
229228
230- // attach entries (by value or by instance)
231- $enumMap->attach(UserStatus::INACTIVE, 'inaktiv');
232- $enumMap->attach(UserStatus::ACTIVE(), 'aktiv');
233- $enumMap->attach(UserStatus::DELETED(), 'gelöscht');
229+ // read and write key-value-pairs like an array
230+ $enumMap[UserStatus::INACTIVE] = 'inaktiv';
231+ $enumMap[UserStatus::ACTIVE] = 'aktiv';
232+ $enumMap[UserStatus::DELETED] = 'gelöscht';
233+ $enumMap[UserStatus::INACTIVE]; // 'inaktiv';
234+ $enumMap[UserStatus::ACTIVE]; // 'aktiv';
235+ $enumMap[UserStatus::DELETED]; // 'gelöscht';
236+
237+ isset($enumMap[UserStatus::DELETED]); // true
238+ unset($enumMap[UserStatus::DELETED]);
239+ isset($enumMap[UserStatus::DELETED]); // false
240+
241+ // ... no matter if you use enumerator values or enumerator objects
242+ $enumMap[UserStatus::INACTIVE()] = 'inaktiv';
243+ $enumMap[UserStatus::ACTIVE()] = 'aktiv';
244+ $enumMap[UserStatus::DELETED()] = 'gelöscht';
245+ $enumMap[UserStatus::INACTIVE()]; // 'inaktiv';
246+ $enumMap[UserStatus::ACTIVE()]; // 'aktiv';
247+ $enumMap[UserStatus::DELETED()]; // 'gelöscht';
248+
249+ isset($enumMap[UserStatus::DELETED()]); // true
250+ unset($enumMap[UserStatus::DELETED()]);
251+ isset($enumMap[UserStatus::DELETED()]); // false
234252
235- // detach entries (by value or by instance)
236- $enumMap->detach(UserStatus::INACTIVE);
237- $enumMap->detach(UserStatus::DELETED());
238253
239- // iterate
254+ // support for null aware exists check
255+ $enumMap[UserStatus::NULL] = null;
256+ isset($enumMap[UserStatus::NULL]); // false
257+ $enumMap->contains(UserStatus::NULL); // true
258+
259+
260+ // iterating over the map
240261foreach ($enumMap as $enum => $value) {
241- var_dump( get_class($enum)); // UserStatus
242- var_dump( gettype($value)) // string
262+ get_class($enum); // UserStatus (enumerator object)
263+ gettype($value); // string (the value the enumerators maps to)
243264}
265+
266+ // get a list of keys (= a list of enumerator objects)
267+ $enumMap->getKeys();
268+
269+ // get a list of values (= a list of values the enumerator maps to)
270+ $enumMap->getValues();
244271```
245272
246273## Serializing
0 commit comments