6
6
use InvalidArgumentException ;
7
7
use RuntimeException ;
8
8
9
+ /**
10
+ * EnumMap implementation in base of SplObjectStorage
11
+ *
12
+ * @link http://github.com/marc-mabe/php-enum for the canonical source repository
13
+ * @copyright Copyright (c) 2012 Marc Bennewitz
14
+ * @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
15
+ */
9
16
class EnumMap extends SplObjectStorage
10
17
{
11
18
19
+ /**
20
+ * key()-behaviour: return the current iterator position
21
+ */
12
22
const KEY_AS_INDEX = 1 ;
23
+
24
+ /**
25
+ * key()-behaviour: return the current enum name
26
+ */
13
27
const KEY_AS_NAME = 2 ;
28
+
29
+ /**
30
+ * key()-behaviour: return the current enum value
31
+ */
14
32
const KEY_AS_VALUE = 3 ;
33
+
34
+ /**
35
+ * key()-behaviour: return the current enum ordinal
36
+ */
15
37
const KEY_AS_ORDINAL = 4 ;
38
+
39
+ /**
40
+ * current()-behaviour: return the current enum object
41
+ */
16
42
const CURRENT_AS_ENUM = 8 ;
43
+
44
+ /**
45
+ * current()-behaviour: return data mapped the current enum
46
+ */
17
47
const CURRENT_AS_DATA = 16 ;
48
+
49
+ /**
50
+ * current()-behaviour: return the current enum name
51
+ */
18
52
const CURRENT_AS_NAME = 24 ;
53
+
54
+ /**
55
+ * current()-behaviour: return the current enum value
56
+ */
19
57
const CURRENT_AS_VALUE = 32 ;
58
+
59
+ /**
60
+ * current()-behaviour: return the current enum ordinal
61
+ */
20
62
const CURRENT_AS_ORDINAL = 40 ;
21
63
64
+ /**
65
+ * The classname of an enumeration this map is for
66
+ * @var string
67
+ */
22
68
private $ enumClass ;
23
69
24
70
/**
@@ -28,6 +74,11 @@ class EnumMap extends SplObjectStorage
28
74
*/
29
75
private $ flags = 9 ;
30
76
77
+ /**
78
+ * Constructor
79
+ * @param string $enumClass The classname of an enumeration the map is for
80
+ * @param int|null $flags Behaviour flags, see KEY_AS_* and CURRENT_AS_* constants
81
+ */
31
82
public function __construct ($ enumClass , $ flags = null )
32
83
{
33
84
if (!is_subclass_of ($ enumClass , __NAMESPACE__ . '\Enum ' )) {
@@ -43,11 +94,22 @@ public function __construct($enumClass, $flags = null)
43
94
}
44
95
}
45
96
97
+ /**
98
+ * Get the classname of enumeration this map is for
99
+ * @return string
100
+ */
46
101
public function getEnumClass ()
47
102
{
48
103
return $ this ->enumClass ;
49
104
}
50
105
106
+ /**
107
+ * Set behaviour flags
108
+ * see KEY_AS_* and CURRENT_AS_* constants
109
+ * @param int $flags
110
+ * @return void
111
+ * @throws InvalidArgumentException On invalid or unsupported flags
112
+ */
51
113
public function setFlags ($ flags )
52
114
{
53
115
$ flags = (int )$ flags ;
@@ -74,17 +136,33 @@ public function setFlags($flags)
74
136
$ this ->flags = $ keyFlag | $ currentFlag ;
75
137
}
76
138
139
+ /**
140
+ * Get the behaviour flags
141
+ * @return int
142
+ */
77
143
public function getFlags ()
78
144
{
79
145
return $ this ->flags ;
80
146
}
81
147
148
+ /**
149
+ * Attach a new enumeration or overwrite an existing one
150
+ * @param Enum|scalar $enum
151
+ * @param mixed $data
152
+ * @return void
153
+ * @throws InvalidArgumentException On an invalid given enum
154
+ */
82
155
public function attach ($ enum , $ data = null )
83
156
{
84
157
$ this ->initEnum ($ enum );
85
158
parent ::attach ($ enum , $ data );
86
159
}
87
160
161
+ /**
162
+ * Test if the given enumeration exists
163
+ * @param Enum|scalar $enum
164
+ * @return boolean
165
+ */
88
166
public function contains ($ enum )
89
167
{
90
168
try {
@@ -96,12 +174,24 @@ public function contains($enum)
96
174
}
97
175
}
98
176
177
+ /**
178
+ * Detach an enumeration
179
+ * @param Enum|scalar $enum
180
+ * @return void
181
+ * @throws InvalidArgumentException On an invalid given enum
182
+ */
99
183
public function detach ($ enum )
100
184
{
101
185
$ this ->initEnum ($ enum );
102
186
parent ::detach ($ enum );
103
187
}
104
188
189
+ /**
190
+ * Get a unique identifier for the given enumeration
191
+ * @param Enum|scalar $enum
192
+ * @return string
193
+ * @throws InvalidArgumentException On an invalid given enum
194
+ */
105
195
public function getHash ($ enum )
106
196
{
107
197
$ this ->initEnum ($ enum );
@@ -110,63 +200,106 @@ public function getHash($enum)
110
200
return spl_object_hash ($ enum );
111
201
}
112
202
203
+ /**
204
+ * Test if the given enumeration exists
205
+ * @param Enum|scalar $enum
206
+ * @return boolean
207
+ * @see contains()
208
+ */
113
209
public function offsetExists ($ enum )
114
210
{
115
211
return $ this ->contains ($ enum );
116
212
}
117
213
214
+ /**
215
+ * Get mapped data for this given enum
216
+ * @param Enum|scalar $enum
217
+ * @return mixed
218
+ * @throws InvalidArgumentException On an invalid given enum
219
+ */
118
220
public function offsetGet ($ enum )
119
221
{
120
222
$ this ->initEnum ($ enum );
121
223
return parent ::offsetGet ($ enum );
122
224
}
123
225
226
+ /**
227
+ * Attach a new enumeration or overwrite an existing one
228
+ * @param Enum|scalar $enum
229
+ * @param mixed $data
230
+ * @return void
231
+ * @throws InvalidArgumentException On an invalid given enum
232
+ * @see attach()
233
+ */
124
234
public function offsetSet ($ enum , $ data = null )
125
235
{
126
236
$ this ->initEnum ($ enum );
127
237
parent ::offsetSet ($ enum , $ data );
128
238
}
129
239
240
+ /**
241
+ * Detach an existing enumeration
242
+ * @param Enum|scalar $enum
243
+ * @return void
244
+ * @throws InvalidArgumentException On an invalid given enum
245
+ * @see detach()
246
+ */
130
247
public function offsetUnset ($ enum )
131
248
{
132
249
$ this ->initEnum ($ enum );
133
250
parent ::offsetUnset ($ enum );
134
251
}
135
252
253
+ /**
254
+ * Get the current item
255
+ * The return value varied by the behaviour of the current flag
256
+ * @return mixed
257
+ */
136
258
public function current ()
137
259
{
138
260
switch ($ this ->flags & 120 ) {
139
261
case self ::CURRENT_AS_ENUM :
140
262
return parent ::current ();
141
263
case self ::CURRENT_AS_DATA :
142
264
return parent ::getInfo ();
143
- case self ::CURRENT_AS_NAME :
144
- return parent ::current ()->getName ();
145
265
case self ::CURRENT_AS_VALUE :
146
266
return parent ::current ()->getValue ();
267
+ case self ::CURRENT_AS_NAME :
268
+ return parent ::current ()->getName ();
147
269
case self ::CURRENT_AS_ORDINAL :
148
270
return parent ::current ()->getOrdinal ();
149
271
default :
150
272
throw new RuntimeException ('Invalid current flag ' );
151
273
}
152
274
}
153
275
276
+ /**
277
+ * Get the current item-key
278
+ * The return value varied by the behaviour of the key flag
279
+ * @return scalar
280
+ */
154
281
public function key ()
155
282
{
156
283
switch ($ this ->flags & 7 ) {
157
284
case self ::KEY_AS_INDEX :
158
285
return parent ::key ();
159
286
case self ::KEY_AS_NAME :
160
287
return parent ::current ()->getName ();
161
- case self ::KEY_AS_VALUE :
162
- return parent ::current ()->getValue ();
163
288
case self ::KEY_AS_ORDINAL :
164
289
return parent ::current ()->getOrdinal ();
290
+ case self ::KEY_AS_VALUE :
291
+ return parent ::current ()->getValue ();
165
292
default :
166
293
throw new RuntimeException ('Invalid key flag ' );
167
294
}
168
295
}
169
296
297
+ /**
298
+ * Initialize an enumeration
299
+ * @param Enum|scalar $enum
300
+ * @return Enum
301
+ * @throws InvalidArgumentException On an invalid given enum
302
+ */
170
303
private function initEnum (&$ enum )
171
304
{
172
305
// auto instantiate
0 commit comments