@@ -143,66 +143,120 @@ $user->setStatus(ExtendedUserStatus::EXTENDED());
143143```
144144
145145Now the setter receives a status it doesn't know about but allows it.
146- If your ` User ` class doesn't allow it the following is the recommanded way:
146+
147+ #### Solution 1: Finilize the numeration
147148
148149``` php
149- class User
150+ final class UserStatus extends Enum
150151{
151152 // ...
153+ }
154+
155+ class User
156+ {
157+ protected $status;
158+
159+ public function setStatus(UserStatus $status)
160+ {
161+ $this->status = $status;
162+ }
163+ }
164+ ````
165+
166+ * Nice and obvious solution
167+
168+ * Resulting behaviour matches native enumeration implementation of most other languages (like Java)
169+
170+ But as this library emulates enumerations it has a view downsides:
171+
172+ * Enumerator values can not be used directly
173+ * `$user->setStatus(UserStatus::ACTIVE)` fails
174+ * `$user->setStatus(UserStatus::ACTIVE())` works
175+
176+ * Does not help if the enumeration was defined in an external library
177+
178+
179+ #### Solution 2: Using `Enum::get()`
180+
181+ ```php
182+ class User
183+ {
152184 public function setStatus($status)
153185 {
154186 $this->status = UserStatus::get($status);
155187 }
156- // ...
157188}
158189```
159190
160- Now you are 100% sure to work with an exact instance of ` UserStatus ` .
191+ * Makes sure the resulting enumerator exactly matches an enumeration. (Inherited enumerators as not allowed).
192+
193+ * Allows enumerator values directly
194+ * ` $user->setStatus(UserStatus::ACTIVE) ` works
195+ * ` $user->setStatus(UserStatus::ACTIVE()) ` works
196+
197+ * Also works for enumerations defined in external libraries
198+
199+ But of course this solution has downsides, too:
161200
162- If the setter receives an ` ExtendedUserStatus ` an exception will be thrown
163- because inheritance is not allowed with ` Enum::get ` .
201+ * Looses declarative type-hint
202+
203+ * A bit slower
164204
165- On the same time this method will accept scalar values matching one of the
166- defined values of ` UserStatus ` and returns an instance of it.
167205
168206## EnumSet
169207
170208An ` EnumSet ` groups enumerators of the same enumeration type together.
171209
172- It implements ` Iterator ` and ` Countable ` so it's simple to iterate the set
173- or count all attached enumerators of it with ` foreach ` and ` count() ` .
210+ It implements ` Iterator ` and ` Countable `
211+ so elements can be iterated and counted like a normal array
212+ using ` foreach ` and ` count() ` .
213+
214+ Internally it's based on a bitset. Integer bitset or binary bitset
215+ depending on how many enumerators are defined for the given enumeration.
174216
175- Internally it's based on a bitset of a binary string so the order will be
176- by the ordinal number by design.
217+ Enumerators attached to an ` EnumSet ` are unique and ordered based on it's ordinal number by design.
177218
178219``` php
179220use MabeEnum\EnumSet;
180221
181222// create a new EnumSet
182223$enumSet = new EnumSet('UserStatus');
183224
225+
184226// attach enumerators (by value or by instance)
185227$enumSet->attach(UserStatus::INACTIVE);
186228$enumSet->attach(UserStatus::ACTIVE());
187229$enumSet->attach(UserStatus::DELETED());
188230
231+
189232// detach enumerators (by value or by instance)
190233$enumSet->detach(UserStatus::INACTIVE);
191234$enumSet->detach(UserStatus::DELETED());
192235
236+
193237// contains enumerators (by value or by instance)
194238$enumSet->contains(UserStatus::INACTIVE); // bool
195239
240+
196241// count number of attached enumerations
197242$enumSet->count();
198243count($enumSet);
199244
245+
200246// convert to array
201247$enumSet->getValues(); // List of enumerator values
202248$enumSet->getEnumerators(); // List of enumerator instances
203249$enumSet->getNames(); // List of enumerator names
204250$enumSet->getOrdinals(); // List of ordinal numbers
205251
252+
253+ // iterating over the set
254+ foreach ($enumSet as $ordinal => $enum) {
255+ gettype($ordinal); // int (the ordinal number of the enumerator)
256+ get_class($enum); // UserStatus (enumerator object)
257+ }
258+
259+
206260// compare two EnumSets
207261$enumSet->isEqual($other); // Check if the EnumSet is the same as other
208262$enumSet->isSubset($other); // Check if the EnumSet is a subset of other
@@ -218,14 +272,17 @@ $enumSet->symDiff($other); // Produce a new set with enumerators in either thi
218272
219273An ` EnumMap ` maps enumerators of the same type to data assigned to.
220274
221- Internally an ` EnumMap ` is based of ` SplObjectStorage ` .
275+ It implements ` ArrayAccess ` , ` Countable ` and ` SeekableIterator `
276+ so elements can be accessed, iterated and counted like a normal array
277+ using ` $enumMap[$key] ` , ` foreach ` and ` count() ` .
222278
223279``` php
224280use MabeEnum\EnumMap;
225281
226282// create a new EnumMap
227283$enumMap = new EnumMap('UserStatus');
228284
285+
229286// read and write key-value-pairs like an array
230287$enumMap[UserStatus::INACTIVE] = 'inaktiv';
231288$enumMap[UserStatus::ACTIVE] = 'aktiv';
@@ -251,6 +308,11 @@ unset($enumMap[UserStatus::DELETED()]);
251308isset($enumMap[UserStatus::DELETED()]); // false
252309
253310
311+ // count number of attached elements
312+ $enumMap->count();
313+ count($enumMap);
314+
315+
254316// support for null aware exists check
255317$enumMap[UserStatus::NULL] = null;
256318isset($enumMap[UserStatus::NULL]); // false
@@ -284,6 +346,9 @@ instance and you could result in two different instance of the same enumeration.
284346
285347** Use it with caution!**
286348
349+ PS: ` EnumSet ` and ` EnumMap ` are serializable by default as long as you don't set other non-serializable values.
350+
351+
287352### Example of using EnumSerializableTrait
288353
289354``` php
@@ -313,7 +378,7 @@ var_dump($north2->is($north1)); // returns TRUE - equality works in both directi
313378
314379* ` SplEnum ` is not build-in into PHP and requires pecl extension installed.
315380* Instances of the same value of an ` SplEnum ` are not the same instance.
316- * ` SplEnum ` doesn't have implemented ` EnumMap ` or ` EnumSet ` .
381+ * No support for ` EnumMap ` or ` EnumSet ` .
317382
318383
319384# Install
0 commit comments