5
5
use ArrayAccess ;
6
6
use Countable ;
7
7
use InvalidArgumentException ;
8
- use Iterator ;
8
+ use OutOfBoundsException ;
9
+ use SeekableIterator ;
9
10
use UnexpectedValueException ;
10
11
11
12
/**
15
16
* @copyright Copyright (c) 2017 Marc Bennewitz
16
17
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
17
18
*/
18
- class EnumMap implements ArrayAccess, Countable, Iterator
19
+ class EnumMap implements ArrayAccess, Countable, SeekableIterator
19
20
{
20
21
/**
21
22
* The classname of the enumeration type
@@ -67,43 +68,63 @@ public function getEnumeration()
67
68
}
68
69
69
70
/**
70
- * Attach a new enumerator or overwrite an existing one
71
- * @param Enum|null|boolean|int|float|string $enumerator
72
- * @param mixed $data
73
- * @return void
74
- * @throws InvalidArgumentException On an invalid given enumerator
71
+ * Get a list of map keys
72
+ * @return Enum[]
75
73
*/
76
- public function attach ( $ enumerator , $ data = null )
74
+ public function getKeys ( )
77
75
{
78
- return $ this ->offsetSet ( $ enumerator , $ data );
76
+ return \array_map ([ $ this ->enumeration , ' byOrdinal ' ], $ this -> ordinals );
79
77
}
80
78
81
79
/**
82
- * Test if the given enumerator exists
83
- * @param Enum|null|boolean|int|float|string $enumerator
84
- * @return boolean
80
+ * Get a list of map values
81
+ * @return mixed[]
85
82
*/
86
- public function contains ( $ enumerator )
83
+ public function getValues ( )
87
84
{
88
- return $ this ->offsetExists ( $ enumerator );
85
+ return \array_values ( $ this ->map );
89
86
}
90
87
91
88
/**
92
- * Detach an enumerator
93
- * @param Enum|null|boolean|int|float|string $enumerator
94
- * @return void
95
- * @throws InvalidArgumentException On an invalid given enumerator
89
+ * Search for the given value
90
+ * @param mixed $value
91
+ * @param bool $strict Use strict type comparison
92
+ * @return Enum|null The found key or NULL
96
93
*/
97
- public function detach ( $ enumerator )
94
+ public function search ( $ value , $ strict = false )
98
95
{
99
- $ this ->offsetUnset ($ enumerator );
96
+ $ ord = \array_search ($ value , $ this ->map , $ strict );
97
+ if ($ ord !== false ) {
98
+ $ enumeration = $ this ->enumeration ;
99
+ return $ enumeration ::byOrdinal ($ ord );
100
+ }
101
+
102
+ return null ;
100
103
}
101
104
102
105
/**
103
106
* Test if the given enumerator exists
104
107
* @param Enum|null|boolean|int|float|string $enumerator
105
108
* @return boolean
106
- * @see contains()
109
+ * @see offsetExists
110
+ */
111
+ public function contains ($ enumerator )
112
+ {
113
+ try {
114
+ $ enumeration = $ this ->enumeration ;
115
+ $ ord = $ enumeration ::get ($ enumerator )->getOrdinal ();
116
+ return array_key_exists ($ ord , $ this ->map );
117
+ } catch (InvalidArgumentException $ e ) {
118
+ // An invalid enumerator can't be contained in this map
119
+ return false ;
120
+ }
121
+ }
122
+
123
+ /**
124
+ * Test if the given enumerator key exists and is not NULL
125
+ * @param Enum|null|boolean|int|float|string $enumerator
126
+ * @return boolean
127
+ * @see contains
107
128
*/
108
129
public function offsetExists ($ enumerator )
109
130
{
@@ -112,7 +133,7 @@ public function offsetExists($enumerator)
112
133
$ ord = $ enumeration ::get ($ enumerator )->getOrdinal ();
113
134
return isset ($ this ->map [$ ord ]);
114
135
} catch (InvalidArgumentException $ e ) {
115
- // An invalid enumerator can't be contained in this map
136
+ // An invalid enumerator can't be an offset of this map
116
137
return false ;
117
138
}
118
139
}
@@ -145,15 +166,15 @@ public function offsetGet($enumerator)
145
166
* @throws InvalidArgumentException On an invalid given enumerator
146
167
* @see attach()
147
168
*/
148
- public function offsetSet ($ enumerator , $ data = null )
169
+ public function offsetSet ($ enumerator , $ value = null )
149
170
{
150
171
$ enumeration = $ this ->enumeration ;
151
172
$ ord = $ enumeration ::get ($ enumerator )->getOrdinal ();
152
173
153
174
if (!isset ($ this ->map [$ ord ])) {
154
175
$ this ->ordinals [] = $ ord ;
155
176
}
156
- $ this ->map [$ ord ] = $ data ;
177
+ $ this ->map [$ ord ] = $ value ;
157
178
}
158
179
159
180
/**
@@ -170,9 +191,24 @@ public function offsetUnset($enumerator)
170
191
171
192
if (($ idx = \array_search ($ ord , $ this ->ordinals , true )) !== false ) {
172
193
unset($ this ->map [$ ord ], $ this ->ordinals [$ idx ]);
194
+ $ this ->ordinals = \array_values ($ this ->ordinals );
173
195
}
174
196
}
175
197
198
+ /**
199
+ * Seeks to the given iterator position.
200
+ * @param int $pos
201
+ */
202
+ public function seek ($ pos )
203
+ {
204
+ $ pos = (int )$ pos ;
205
+ if (!isset ($ this ->ordinals [$ pos ])) {
206
+ throw new OutOfBoundsException ("Position {$ pos } not found " );
207
+ }
208
+
209
+ $ this ->pos = $ pos ;
210
+ }
211
+
176
212
/**
177
213
* Get the current value
178
214
* @return mixed
0 commit comments