Skip to content

Commit b9b545d

Browse files
committed
Implemented Enum::getEnumerators()
... to get a list all of enumerator instance of the called enumeration type ... fixes #45
1 parent 3b7f19f commit b9b545d

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/MabeEnum/Enum.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,16 @@ final public static function clear()
250250
unset(self::$instances[$class], self::$constants[$class]);
251251
}
252252

253+
/**
254+
* Get a list of enumerator instances ordered by ordinal number
255+
*
256+
* @return static[]
257+
*/
258+
final public static function getEnumerators()
259+
{
260+
return array_map('self::getByName', array_keys(self::detectConstants(get_called_class())));
261+
}
262+
253263
/**
254264
* Get all available constants of the called class
255265
*

tests/MabeEnumTest/EnumTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,22 @@ public function testGetByExtendedInstanceOfKnownValue()
106106
EnumBasic::get($enum);
107107
}
108108

109+
public function testGetEnumerators()
110+
{
111+
$constants = EnumInheritance::getConstants();
112+
$enumerators = EnumInheritance::getEnumerators();
113+
114+
$this->assertSame(count($constants), count($enumerators));
115+
for ($i = 0; $i < count($enumerators); ++$i) {
116+
$this->assertArrayHasKey($i, $enumerators);
117+
$this->assertInstanceOf('MabeEnumTest\TestAsset\EnumInheritance', $enumerators[$i]);
118+
119+
$enumerator = $enumerators[$i];
120+
$this->assertArrayHasKey($enumerator->getName(), $constants);
121+
$this->assertSame($constants[$enumerator->getName()], $enumerator->getValue());
122+
}
123+
}
124+
109125
public function testGetAllValues()
110126
{
111127
$constants = EnumBasic::getConstants();

0 commit comments

Comments
 (0)