Skip to content

Commit f6be933

Browse files
authored
Merge pull request #92 from marc-mabe/refactor_EnumMap
Refactor EnumMap - fixes #91
2 parents fc93d58 + a0fdfaf commit f6be933

File tree

7 files changed

+410
-106
lines changed

7 files changed

+410
-106
lines changed

README.md

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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
9593
UserStatus::get(UserStatus::ACTIVE) === UserStatus::ACTIVE()
@@ -135,11 +133,12 @@ Because in normal OOP the above example allows `UserStatus` and types inherited
135133
Please 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
240261
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)
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

bench/EnumMapBench.php

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,36 @@ public function init()
5353
}
5454
}
5555

56+
public function benchGetKeysEmpty()
57+
{
58+
$this->emptyMap->getKeys();
59+
}
60+
61+
public function benchGetKeysFull()
62+
{
63+
$this->fullMap->getKeys();
64+
}
65+
66+
public function benchGetValuesEmpty()
67+
{
68+
$this->emptyMap->getValues();
69+
}
70+
71+
public function benchGetValuesFull()
72+
{
73+
$this->fullMap->getValues();
74+
}
75+
76+
public function benchSearchTypeJuggling()
77+
{
78+
$this->fullMap->search('31');
79+
}
80+
81+
public function benchSearchStrict()
82+
{
83+
$this->fullMap->search(31, true);
84+
}
85+
5686
public function benchOffsetSetEnumerator()
5787
{
5888
foreach ($this->enumerators as $enumerator) {
@@ -81,31 +111,31 @@ public function benchOffsetUnsetValue()
81111
}
82112
}
83113

84-
public function benchOffsetExistsEnumeratorTrue()
114+
public function benchOffsetExistsEnumerator()
85115
{
86116
foreach ($this->enumerators as $enumerator) {
87117
$this->fullMap->offsetExists($enumerator);
88118
}
89119
}
90120

91-
public function benchOffsetExistsEnumeratorFalse()
121+
public function benchOffsetExistsValue()
92122
{
93-
foreach ($this->enumerators as $enumerator) {
94-
$this->fullMap->offsetExists($enumerator);
123+
foreach ($this->values as $value) {
124+
$this->fullMap->offsetExists($value);
95125
}
96126
}
97127

98-
public function benchOffsetExistsValueTrue()
128+
public function benchContainsEnumerator()
99129
{
100-
foreach ($this->values as $value) {
101-
$this->fullMap->offsetExists($value);
130+
foreach ($this->enumerators as $enumerator) {
131+
$this->fullMap->contains($enumerator);
102132
}
103133
}
104134

105-
public function benchOffsetExistsValueFalse()
135+
public function benchContainsValue()
106136
{
107137
foreach ($this->values as $value) {
108-
$this->fullMap->offsetExists($value);
138+
$this->fullMap->contains($value);
109139
}
110140
}
111141

composer.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
"php": ">=5.6",
2525
"ext-reflection": "*"
2626
},
27-
"suggest": {
28-
"php": "PHP>=5.4 will be required for using trait EnumSerializableTrait"
29-
},
3027
"require-dev": {
3128
"phpunit/phpunit": "^5.7 || ^6.0",
3229
"phpbench/phpbench": "@dev",

src/Enum.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ final public static function byOrdinal($ordinal)
247247
}
248248

249249
if (!isset(self::$names[$class][$ordinal])) {
250-
throw new InvalidArgumentException(sprintf(
250+
throw new InvalidArgumentException(\sprintf(
251251
'Invalid ordinal number, must between 0 and %s',
252252
\count(self::$names[$class]) - 1
253253
));

0 commit comments

Comments
 (0)