@@ -123,7 +123,6 @@ public function setFlags($flags)
123
123
} elseif (!$ keyFlag ) {
124
124
$ keyFlag = $ this ->flags & 7 ;
125
125
}
126
-
127
126
128
127
$ currentFlag = $ flags & 56 ;
129
128
if ($ currentFlag > 40 ) {
@@ -155,7 +154,8 @@ public function getFlags()
155
154
*/
156
155
public function attach ($ enum , $ data = null )
157
156
{
158
- parent ::attach ($ this ->initEnum ($ enum ), $ data );
157
+ $ enumClass = $ this ->enumClass ;
158
+ parent ::attach ($ enumClass ::get ($ enum ), $ data );
159
159
}
160
160
161
161
/**
@@ -166,7 +166,8 @@ public function attach($enum, $data = null)
166
166
public function contains ($ enum )
167
167
{
168
168
try {
169
- return parent ::contains ($ this ->initEnum ($ enum ));
169
+ $ enumClass = $ this ->enumClass ;
170
+ return parent ::contains ($ enumClass ::get ($ enum ));
170
171
} catch (InvalidArgumentException $ e ) {
171
172
// On an InvalidArgumentException the given argument can't be contained in this map
172
173
return false ;
@@ -181,7 +182,8 @@ public function contains($enum)
181
182
*/
182
183
public function detach ($ enum )
183
184
{
184
- parent ::detach ($ this ->initEnum ($ enum ));
185
+ $ enumClass = $ this ->enumClass ;
186
+ parent ::detach ($ enumClass ::get ($ enum ));
185
187
}
186
188
187
189
/**
@@ -193,7 +195,8 @@ public function detach($enum)
193
195
public function getHash ($ enum )
194
196
{
195
197
// getHash is available since PHP 5.4
196
- return spl_object_hash ($ this ->initEnum ($ enum ));
198
+ $ enumClass = $ this ->enumClass ;
199
+ return spl_object_hash ($ enumClass ::get ($ enum ));
197
200
}
198
201
199
202
/**
@@ -215,7 +218,8 @@ public function offsetExists($enum)
215
218
*/
216
219
public function offsetGet ($ enum )
217
220
{
218
- return parent ::offsetGet ($ this ->initEnum ($ enum ));
221
+ $ enumClass = $ this ->enumClass ;
222
+ return parent ::offsetGet ($ enumClass ::get ($ enum ));
219
223
}
220
224
221
225
/**
@@ -228,7 +232,8 @@ public function offsetGet($enum)
228
232
*/
229
233
public function offsetSet ($ enum , $ data = null )
230
234
{
231
- parent ::offsetSet ($ this ->initEnum ($ enum ), $ data );
235
+ $ enumClass = $ this ->enumClass ;
236
+ parent ::offsetSet ($ enumClass ::get ($ enum ), $ data );
232
237
}
233
238
234
239
/**
@@ -240,7 +245,8 @@ public function offsetSet($enum, $data = null)
240
245
*/
241
246
public function offsetUnset ($ enum )
242
247
{
243
- parent ::offsetUnset ($ this ->initEnum ($ enum ));
248
+ $ enumClass = $ this ->enumClass ;
249
+ parent ::offsetUnset ($ enumClass ::get ($ enum ));
244
250
}
245
251
246
252
/**
@@ -286,32 +292,4 @@ public function key()
286
292
throw new RuntimeException ('Invalid key flag ' );
287
293
}
288
294
}
289
-
290
- /**
291
- * Initialize an enumeration
292
- * @param Enum|null|boolean|int|float|string $enum
293
- * @return Enum
294
- * @throws InvalidArgumentException On an invalid given enum
295
- */
296
- private function initEnum ($ enum )
297
- {
298
- // auto instantiate
299
- if (is_scalar ($ enum )) {
300
- $ enumClass = $ this ->enumClass ;
301
- return $ enumClass ::get ($ enum );
302
- }
303
-
304
- // allow only enums of the same type
305
- // (don't allow instance of)
306
- $ enumClass = get_class ($ enum );
307
- if ($ enumClass && strcasecmp ($ enumClass , $ this ->enumClass ) === 0 ) {
308
- return $ enum ;
309
- }
310
-
311
- throw new InvalidArgumentException (sprintf (
312
- "The given enum of type '%s' isn't same as the required type '%s' " ,
313
- get_class ($ enum ) ?: gettype ($ enum ),
314
- $ this ->enumClass
315
- ));
316
- }
317
295
}
0 commit comments