Skip to content

Commit 6183bfe

Browse files
committed
Implemented Enum::getByOrdinal(), fixes #18
1 parent 3b050f4 commit 6183bfe

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/MabeEnum/Enum.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ final public function getOrdinal()
128128
* @throws InvalidArgumentException On an unknwon or invalid value
129129
* @throws LogicException On ambiguous constant values
130130
*/
131-
static public function get($value)
131+
final static public function get($value)
132132
{
133133
$class = get_called_class();
134134
$id = $class . '.' . $value;
@@ -141,6 +141,27 @@ static public function get($value)
141141
return $instance;
142142
}
143143

144+
/**
145+
* Get an enum by the given ordinal number
146+
*
147+
* @param int $ordinal The ordinal number to instantiate the enum by
148+
* @return Enum
149+
* @throws InvalidArgumentException On an invalid ordinal number
150+
* @throws LogicException On ambiguous constant values
151+
*/
152+
final public static function getByOrdinal($ordinal)
153+
{
154+
$constants = static::getConstants();
155+
$item = array_slice($constants, $ordinal, 1, false);
156+
if (!$item) {
157+
throw new InvalidArgumentException(sprintf(
158+
'Invalid ordinal number, must between 0 and %s',
159+
count($constants) - 1
160+
));
161+
}
162+
return static::get(current($item));
163+
}
164+
144165
/**
145166
* Clears all instantiated enums
146167
*
@@ -149,7 +170,7 @@ static public function get($value)
149170
* @param null|string $class
150171
* @return void
151172
*/
152-
final static function clear()
173+
final static public function clear()
153174
{
154175
$class = get_called_class();
155176

@@ -214,6 +235,7 @@ final static public function getConstants()
214235
*
215236
* @param string $const The name of the constant to instantiate the enum with
216237
* @param array $args There should be no arguments
238+
* @return Enum
217239
* @throws BadMethodCallException On an unknown constant name (method name)
218240
* @throws LogicException On ambiguous constant values
219241
*/

tests/MabeEnumTest/EnumTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ public function testInstantiateUsingMagicMethodThrowsBadMethodCallException()
8585
EnumInheritance::UNKNOWN();
8686
}
8787

88+
public function testInstantiateUsingOrdinalNumber()
89+
{
90+
$enum = EnumInheritance::getByOrdinal(2);
91+
$this->assertSame(2, $enum->getOrdinal());
92+
$this->assertSame('INHERITANCE', $enum->getName());
93+
}
94+
95+
public function testInstantiateUsingInvalidOrdinalNumberThrowsInvalidArgumentException()
96+
{
97+
$this->setExpectedException('InvalidArgumentException');
98+
EnumInheritance::getByOrdinal(3);
99+
}
100+
88101
public function testAmbuguousConstantsThrowsLogicException()
89102
{
90103
$this->setExpectedException('LogicException');

0 commit comments

Comments
 (0)