7
7
use ArrayAccess ;
8
8
use Countable ;
9
9
use InvalidArgumentException ;
10
- use OutOfBoundsException ;
11
- use SeekableIterator ;
12
- use TypeError ;
10
+ use Iterator ;
11
+ use IteratorAggregate ;
13
12
use UnexpectedValueException ;
14
13
15
14
/**
19
18
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
20
19
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
21
20
*/
22
- class EnumMap implements ArrayAccess, Countable, SeekableIterator
21
+ class EnumMap implements ArrayAccess, Countable, IteratorAggregate
23
22
{
24
23
/**
25
24
* The classname of the enumeration type
@@ -33,18 +32,6 @@ class EnumMap implements ArrayAccess, Countable, SeekableIterator
33
32
*/
34
33
private $ map = [];
35
34
36
- /**
37
- * List of ordinal numbers
38
- * @var int[]
39
- */
40
- private $ ordinals = [];
41
-
42
- /**
43
- * Current iterator position
44
- * @var int
45
- */
46
- private $ pos = 0 ;
47
-
48
35
/**
49
36
* Constructor
50
37
* @param string $enumeration The classname of the enumeration type
@@ -77,7 +64,7 @@ public function getEnumeration(): string
77
64
*/
78
65
public function getKeys (): array
79
66
{
80
- return \array_map ([$ this ->enumeration , 'byOrdinal ' ], $ this ->ordinals );
67
+ return \array_map ([$ this ->enumeration , 'byOrdinal ' ], \array_keys ( $ this ->map ) );
81
68
}
82
69
83
70
/**
@@ -115,7 +102,7 @@ public function contains($enumerator): bool
115
102
{
116
103
try {
117
104
$ ord = ($ this ->enumeration )::get ($ enumerator )->getOrdinal ();
118
- return array_key_exists ($ ord , $ this ->map );
105
+ return \ array_key_exists ($ ord , $ this ->map );
119
106
} catch (InvalidArgumentException $ e ) {
120
107
// An invalid enumerator can't be contained in this map
121
108
return false ;
@@ -149,7 +136,7 @@ public function offsetGet($enumerator)
149
136
{
150
137
$ enumerator = ($ this ->enumeration )::get ($ enumerator );
151
138
$ ord = $ enumerator ->getOrdinal ();
152
- if (!isset ($ this ->map [$ ord ]) && !array_key_exists ($ ord , $ this ->map )) {
139
+ if (!isset ($ this ->map [$ ord ]) && !\ array_key_exists ($ ord , $ this ->map )) {
153
140
throw new UnexpectedValueException (sprintf (
154
141
'Enumerator %s could not be found ' ,
155
142
\var_export ($ enumerator ->getValue (), true )
@@ -170,10 +157,6 @@ public function offsetGet($enumerator)
170
157
public function offsetSet ($ enumerator , $ value = null ): void
171
158
{
172
159
$ ord = ($ this ->enumeration )::get ($ enumerator )->getOrdinal ();
173
-
174
- if (!array_key_exists ($ ord , $ this ->map )) {
175
- $ this ->ordinals [] = $ ord ;
176
- }
177
160
$ this ->map [$ ord ] = $ value ;
178
161
}
179
162
@@ -187,86 +170,20 @@ public function offsetSet($enumerator, $value = null): void
187
170
public function offsetUnset ($ enumerator ): void
188
171
{
189
172
$ ord = ($ this ->enumeration )::get ($ enumerator )->getOrdinal ();
190
-
191
- if (($ idx = \array_search ($ ord , $ this ->ordinals , true )) !== false ) {
192
- unset($ this ->map [$ ord ], $ this ->ordinals [$ idx ]);
193
- $ this ->ordinals = \array_values ($ this ->ordinals );
194
- }
195
- }
196
-
197
- /**
198
- * Seeks to the given iterator position.
199
- * @param int $pos
200
- * @throws OutOfBoundsException On an invalid position
201
- */
202
- public function seek ($ pos ): void
203
- {
204
- if (!is_int ($ pos )) {
205
- throw new TypeError (\sprintf (
206
- 'Argument 1 passed to %s() must be of the type int, %s given ' ,
207
- __METHOD__ ,
208
- gettype ($ pos )
209
- ));
210
- }
211
-
212
- if (!isset ($ this ->ordinals [$ pos ])) {
213
- throw new OutOfBoundsException ("Position {$ pos } not found " );
214
- }
215
-
216
- $ this ->pos = $ pos ;
217
- }
218
-
219
- /**
220
- * Get the current value
221
- * @return mixed
222
- */
223
- public function current ()
224
- {
225
- if (!isset ($ this ->ordinals [$ this ->pos ])) {
226
- return null ;
227
- }
228
-
229
- return $ this ->map [$ this ->ordinals [$ this ->pos ]];
173
+ unset($ this ->map [$ ord ]);
230
174
}
231
175
232
176
/**
233
- * Get the current key
234
- * @return Enum|null
177
+ * Get a new Iterator.
178
+ *
179
+ * @return Iterator
235
180
*/
236
- public function key (): ? Enum
181
+ public function getIterator (): Iterator
237
182
{
238
- if (!isset ($ this ->ordinals [$ this ->pos ])) {
239
- return null ;
183
+ $ map = $ this ->map ;
184
+ foreach ($ map as $ ordinal => $ value ) {
185
+ yield ($ this ->enumeration )::byOrdinal ($ ordinal ) => $ value ;
240
186
}
241
-
242
- return ($ this ->enumeration )::byOrdinal ($ this ->ordinals [$ this ->pos ]);
243
- }
244
-
245
- /**
246
- * Reset the iterator position to zero.
247
- * @return void
248
- */
249
- public function rewind (): void
250
- {
251
- $ this ->pos = 0 ;
252
- }
253
-
254
- /**
255
- * Increment the iterator position by one.
256
- * @return void
257
- */
258
- public function next (): void
259
- {
260
- ++$ this ->pos ;
261
- }
262
-
263
- /**
264
- * Test if the iterator is in a valid state
265
- * @return bool
266
- */
267
- public function valid (): bool
268
- {
269
- return isset ($ this ->ordinals [$ this ->pos ]);
270
187
}
271
188
272
189
/**
@@ -276,6 +193,6 @@ public function valid(): bool
276
193
*/
277
194
public function count (): int
278
195
{
279
- return \count ($ this ->ordinals );
196
+ return \count ($ this ->map );
280
197
}
281
198
}
0 commit comments