55use ArrayAccess ;
66use Countable ;
77use InvalidArgumentException ;
8- use Iterator ;
8+ use OutOfBoundsException ;
9+ use SeekableIterator ;
910use UnexpectedValueException ;
1011
1112/**
1516 * @copyright Copyright (c) 2017 Marc Bennewitz
1617 * @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
1718 */
18- class EnumMap implements ArrayAccess, Countable, Iterator
19+ class EnumMap implements ArrayAccess, Countable, SeekableIterator
1920{
2021 /**
2122 * The classname of the enumeration type
@@ -67,43 +68,63 @@ public function getEnumeration()
6768 }
6869
6970 /**
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[]
7573 */
76- public function attach ( $ enumerator , $ data = null )
74+ public function getKeys ( )
7775 {
78- return $ this ->offsetSet ( $ enumerator , $ data );
76+ return \array_map ([ $ this ->enumeration , ' byOrdinal ' ], $ this -> ordinals );
7977 }
8078
8179 /**
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[]
8582 */
86- public function contains ( $ enumerator )
83+ public function getValues ( )
8784 {
88- return $ this ->offsetExists ( $ enumerator );
85+ return \array_values ( $ this ->map );
8986 }
9087
9188 /**
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
9693 */
97- public function detach ( $ enumerator )
94+ public function search ( $ value , $ strict = false )
9895 {
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 ;
100103 }
101104
102105 /**
103106 * Test if the given enumerator exists
104107 * @param Enum|null|boolean|int|float|string $enumerator
105108 * @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
107128 */
108129 public function offsetExists ($ enumerator )
109130 {
@@ -112,7 +133,7 @@ public function offsetExists($enumerator)
112133 $ ord = $ enumeration ::get ($ enumerator )->getOrdinal ();
113134 return isset ($ this ->map [$ ord ]);
114135 } 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
116137 return false ;
117138 }
118139 }
@@ -145,15 +166,15 @@ public function offsetGet($enumerator)
145166 * @throws InvalidArgumentException On an invalid given enumerator
146167 * @see attach()
147168 */
148- public function offsetSet ($ enumerator , $ data = null )
169+ public function offsetSet ($ enumerator , $ value = null )
149170 {
150171 $ enumeration = $ this ->enumeration ;
151172 $ ord = $ enumeration ::get ($ enumerator )->getOrdinal ();
152173
153174 if (!isset ($ this ->map [$ ord ])) {
154175 $ this ->ordinals [] = $ ord ;
155176 }
156- $ this ->map [$ ord ] = $ data ;
177+ $ this ->map [$ ord ] = $ value ;
157178 }
158179
159180 /**
@@ -170,9 +191,24 @@ public function offsetUnset($enumerator)
170191
171192 if (($ idx = \array_search ($ ord , $ this ->ordinals , true )) !== false ) {
172193 unset($ this ->map [$ ord ], $ this ->ordinals [$ idx ]);
194+ $ this ->ordinals = \array_values ($ this ->ordinals );
173195 }
174196 }
175197
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+
176212 /**
177213 * Get the current value
178214 * @return mixed
0 commit comments