Skip to content

Commit 2bfc886

Browse files
committed
Added static method Enum::getValues(), Enum::getNames() and Enum::getOrdinals()
to be consistent with the object methods of EnumSet.
1 parent a496c91 commit 2bfc886

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/Enum.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,36 @@ final public static function getEnumerators()
256256
return array_map('self::getByName', array_keys(self::detectConstants(get_called_class())));
257257
}
258258

259+
/**
260+
* Get a list of enumerator values ordered by ordinal number
261+
*
262+
* @return mixed[]
263+
*/
264+
final public static function getValues()
265+
{
266+
return array_values(self::detectConstants(get_called_class()));
267+
}
268+
269+
/**
270+
* Get a list of enumerator names ordered by ordinal number
271+
*
272+
* @return string[]
273+
*/
274+
final public static function getNames()
275+
{
276+
return array_keys(self::detectConstants(get_called_class()));
277+
}
278+
/*
279+
* Get a list of enumerator ordinal numbers
280+
*
281+
* @return int[]
282+
*/
283+
final public static function getOrdinals()
284+
{
285+
$count = count(self::detectConstants(get_called_class()));
286+
return $count === 0 ? array() : range(0, $count - 1);
287+
}
288+
259289
/**
260290
* Get all available constants of the called class
261291
*

tests/MabeEnumTest/EnumTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,45 @@ public function testGetEnumerators()
126126
}
127127
}
128128

129+
public function testGetValues()
130+
{
131+
$expectedValues = array_values(EnumInheritance::getConstants());
132+
$values = EnumInheritance::getValues();
133+
$count = count($values);
134+
135+
$this->assertSame(count($expectedValues), $count);
136+
for ($i = 0; $i < $count; ++$i) {
137+
$this->assertArrayHasKey($i, $values);
138+
$this->assertSame($expectedValues[$i], $values[$i]);
139+
}
140+
}
141+
142+
public function testGetNames()
143+
{
144+
$expectedNames = array_keys(EnumInheritance::getConstants());
145+
$names = EnumInheritance::getNames();
146+
$count = count($names);
147+
148+
$this->assertSame(count($expectedNames), $count);
149+
for ($i = 0; $i < $count; ++$i) {
150+
$this->assertArrayHasKey($i, $names);
151+
$this->assertSame($expectedNames[$i], $names[$i]);
152+
}
153+
}
154+
155+
public function testGetOrdinals()
156+
{
157+
$constants = EnumInheritance::getConstants();
158+
$ordinals = EnumInheritance::getOrdinals();
159+
$count = count($ordinals);
160+
161+
$this->assertSame(count($constants), $count);
162+
for ($i = 0; $i < $count; ++$i) {
163+
$this->assertArrayHasKey($i, $ordinals);
164+
$this->assertSame($i, $ordinals[$i]);
165+
}
166+
}
167+
129168
public function testGetAllValues()
130169
{
131170
$constants = EnumBasic::getConstants();

0 commit comments

Comments
 (0)