4
4
5
5
use SplObjectStorage ;
6
6
use InvalidArgumentException ;
7
- use RuntimeException ;
8
7
9
8
/**
10
9
* EnumMap implementation in base of SplObjectStorage
15
14
*/
16
15
class EnumMap extends SplObjectStorage
17
16
{
18
-
19
- /**
20
- * key()-behaviour: return the iterator position
21
- */
22
- const KEY_AS_INDEX = 1 ;
23
-
24
- /**
25
- * key()-behaviour: return the name of the current element
26
- */
27
- const KEY_AS_NAME = 2 ;
28
-
29
- /**
30
- * key()-behaviour: return the value of the current element
31
- */
32
- const KEY_AS_VALUE = 3 ;
33
-
34
- /**
35
- * key()-behaviour: return the ordinal number of the current element
36
- */
37
- const KEY_AS_ORDINAL = 4 ;
38
-
39
- /**
40
- * current()-behaviour: return the instance of the enumerator
41
- */
42
- const CURRENT_AS_ENUM = 8 ;
43
-
44
- /**
45
- * current()-behaviour: return the data mapped the enumerator
46
- */
47
- const CURRENT_AS_DATA = 16 ;
48
-
49
- /**
50
- * current()-behaviour: return the name of the enumerator
51
- */
52
- const CURRENT_AS_NAME = 24 ;
53
-
54
- /**
55
- * current()-behaviour: return the value of the enumerator
56
- */
57
- const CURRENT_AS_VALUE = 32 ;
58
-
59
- /**
60
- * current()-behaviour: return the ordinal number of the enumerator
61
- */
62
- const CURRENT_AS_ORDINAL = 40 ;
63
-
64
17
/**
65
18
* The classname of the enumeration type
66
19
* @var string
67
20
*/
68
21
private $ enumeration ;
69
22
70
- /**
71
- * Flags to define behaviors
72
- * (Default = KEY_AS_INDEX | CURRENT_AS_ENUM)
73
- * @var int
74
- */
75
- private $ flags = 9 ;
76
-
77
23
/**
78
24
* Constructor
79
- * @param string $enumeration The classname of the enumeration type
80
- * @param int|null $flags Behaviour flags, see KEY_AS_* and CURRENT_AS_* constants
25
+ * @param string $enumeration The classname of the enumeration type
81
26
* @throws InvalidArgumentException
82
27
*/
83
- public function __construct ($ enumeration, $ flags = null )
28
+ public function __construct ($ enumeration )
84
29
{
85
30
if (!is_subclass_of ($ enumeration , __NAMESPACE__ . '\Enum ' )) {
86
31
throw new InvalidArgumentException (sprintf (
@@ -89,10 +34,6 @@ public function __construct($enumeration, $flags = null)
89
34
));
90
35
}
91
36
$ this ->enumeration = $ enumeration ;
92
-
93
- if ($ flags !== null ) {
94
- $ this ->setFlags ($ flags );
95
- }
96
37
}
97
38
98
39
/**
@@ -104,47 +45,6 @@ public function getEnumeration()
104
45
return $ this ->enumeration ;
105
46
}
106
47
107
- /**
108
- * Set behaviour flags
109
- * see KEY_AS_* and CURRENT_AS_* constants
110
- * @param int $flags
111
- * @return void
112
- * @throws InvalidArgumentException On invalid or unsupported flags
113
- */
114
- public function setFlags ($ flags )
115
- {
116
- $ flags = (int )$ flags ;
117
-
118
- $ keyFlag = $ flags & 7 ;
119
- if ($ keyFlag > 4 ) {
120
- throw new InvalidArgumentException (
121
- "Unsupported flag given for key() behavior "
122
- );
123
- } elseif (!$ keyFlag ) {
124
- $ keyFlag = $ this ->flags & 7 ;
125
- }
126
-
127
- $ currentFlag = $ flags & 56 ;
128
- if ($ currentFlag > 40 ) {
129
- throw new InvalidArgumentException (
130
- "Unsupported flag given for current() behavior "
131
- );
132
- } elseif (!$ currentFlag ) {
133
- $ currentFlag = $ this ->flags & 56 ;
134
- }
135
-
136
- $ this ->flags = $ keyFlag | $ currentFlag ;
137
- }
138
-
139
- /**
140
- * Get the behaviour flags
141
- * @return int
142
- */
143
- public function getFlags ()
144
- {
145
- return $ this ->flags ;
146
- }
147
-
148
48
/**
149
49
* Attach a new enumerator or overwrite an existing one
150
50
* @param Enum|null|boolean|int|float|string $enumerator
@@ -188,13 +88,12 @@ public function detach($enumerator)
188
88
189
89
/**
190
90
* Get a unique identifier for the given enumerator
191
- * @param Enum|scalar $enumerator
91
+ * @param Enum|null|boolean|int|float|string $enumerator
192
92
* @return string
193
93
* @throws InvalidArgumentException On an invalid given enumerator
194
94
*/
195
95
public function getHash ($ enumerator )
196
96
{
197
- // getHash is available since PHP 5.4
198
97
$ enumeration = $ this ->enumeration ;
199
98
return spl_object_hash ($ enumeration ::get ($ enumerator ));
200
99
}
@@ -250,46 +149,20 @@ public function offsetUnset($enumerator)
250
149
}
251
150
252
151
/**
253
- * Get the current item
254
- * The return value varied by the behaviour of the current flag
152
+ * Get the current value
255
153
* @return mixed
256
154
*/
257
155
public function current ()
258
156
{
259
- switch ($ this ->flags & 120 ) {
260
- case self ::CURRENT_AS_ENUM :
261
- return parent ::current ();
262
- case self ::CURRENT_AS_DATA :
263
- return parent ::getInfo ();
264
- case self ::CURRENT_AS_VALUE :
265
- return parent ::current ()->getValue ();
266
- case self ::CURRENT_AS_NAME :
267
- return parent ::current ()->getName ();
268
- case self ::CURRENT_AS_ORDINAL :
269
- return parent ::current ()->getOrdinal ();
270
- default :
271
- throw new RuntimeException ('Invalid current flag ' );
272
- }
157
+ return parent ::getInfo ();
273
158
}
274
159
275
160
/**
276
- * Get the current item-key
277
- * The return value varied by the behaviour of the key flag
278
- * @return null|boolean|int|float|string
161
+ * Get the current key
162
+ * @return Enum|null
279
163
*/
280
164
public function key ()
281
165
{
282
- switch ($ this ->flags & 7 ) {
283
- case self ::KEY_AS_INDEX :
284
- return parent ::key ();
285
- case self ::KEY_AS_NAME :
286
- return parent ::current ()->getName ();
287
- case self ::KEY_AS_ORDINAL :
288
- return parent ::current ()->getOrdinal ();
289
- case self ::KEY_AS_VALUE :
290
- return parent ::current ()->getValue ();
291
- default :
292
- throw new RuntimeException ('Invalid key flag ' );
293
- }
166
+ return parent ::current ();
294
167
}
295
168
}
0 commit comments