@@ -42,25 +42,23 @@ class UserStatus extends Enum
42
42
const ACTIVE = 'a';
43
43
const DELETED = 'd';
44
44
45
- // all scalar datatypes are supported
45
+ // all scalar data types and arrays are supported as enumerator values
46
46
const NIL = null;
47
47
const BOOLEAN = true;
48
48
const INT = 1234;
49
49
const STR = 'string';
50
50
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']];
54
52
55
53
// Enumerators will be generated from public constants only
56
54
public const PUBLIC_CONST = 'public constant'; // this will be an enumerator
57
55
protected const PROTECTED_CONST = 'protected constant'; // this will NOT be an enumerator
58
56
private const PRIVATE_CONST = 'private constant'; // this will NOT be an enumerator
59
57
60
58
// 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' ;
64
62
const PUBLIC = 'public';
65
63
const PRIVATE = 'private';
66
64
const PROTECTED = 'protected';
@@ -85,11 +83,11 @@ $status->getName(); // returns the selected constant name
85
83
$status->getOrdinal(); // returns the ordinal number of the selected constant
86
84
87
85
// 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
93
91
94
92
// same enumerators (of the same enumeration class) holds the same instance
95
93
UserStatus::get(UserStatus::ACTIVE) === UserStatus::ACTIVE()
@@ -135,11 +133,12 @@ Because in normal OOP the above example allows `UserStatus` and types inherited
135
133
Please think about the following example:
136
134
137
135
``` php
138
- class ExtendedUserStatus
136
+ class ExtendedUserStatus extends UserStatus
139
137
{
140
138
const EXTENDED = 'extended';
141
139
}
142
140
141
+ $user = new User();
143
142
$user->setStatus(ExtendedUserStatus::EXTENDED());
144
143
```
145
144
@@ -209,10 +208,10 @@ $enumSet->isEqual($other); // Check if the EnumSet is the same as other
209
208
$enumSet->isSubset($other); // Check if the EnumSet is a subset of other
210
209
$enumSet->isSuperset($other); // Check if the EnumSet is a superset of other
211
210
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)
216
215
```
217
216
218
217
## EnumMap
@@ -227,20 +226,48 @@ use MabeEnum\EnumMap;
227
226
// create a new EnumMap
228
227
$enumMap = new EnumMap('UserStatus');
229
228
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
234
252
235
- // detach entries (by value or by instance)
236
- $enumMap->detach(UserStatus::INACTIVE);
237
- $enumMap->detach(UserStatus::DELETED());
238
253
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
240
261
foreach ($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)
243
264
}
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();
244
271
```
245
272
246
273
## Serializing
0 commit comments