Skip to content

Commit b9a6b69

Browse files
authored
Merge pull request #78 from marc-mabe/enum_get_values_names_ordinals
Added static Enum::getValues(), Enum::getNames() and Enum::getOrdinals()
2 parents 8f0733d + 2bfc886 commit b9a6b69

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
@@ -263,6 +263,36 @@ final public static function getEnumerators()
263263
return array_map('self::getByName', array_keys(self::detectConstants(get_called_class())));
264264
}
265265

266+
/**
267+
* Get a list of enumerator values ordered by ordinal number
268+
*
269+
* @return mixed[]
270+
*/
271+
final public static function getValues()
272+
{
273+
return array_values(self::detectConstants(get_called_class()));
274+
}
275+
276+
/**
277+
* Get a list of enumerator names ordered by ordinal number
278+
*
279+
* @return string[]
280+
*/
281+
final public static function getNames()
282+
{
283+
return array_keys(self::detectConstants(get_called_class()));
284+
}
285+
/*
286+
* Get a list of enumerator ordinal numbers
287+
*
288+
* @return int[]
289+
*/
290+
final public static function getOrdinals()
291+
{
292+
$count = count(self::detectConstants(get_called_class()));
293+
return $count === 0 ? array() : range(0, $count - 1);
294+
}
295+
266296
/**
267297
* Get all available constants of the called class
268298
*

tests/MabeEnumTest/EnumTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,45 @@ public function testGetEnumerators()
121121
}
122122
}
123123

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

0 commit comments

Comments
 (0)