Skip to content

Commit 58d6533

Browse files
author
Marc Bennewitz
committed
Merge pull request #19 from marc-mabe/feature/getBy
Implemented Enum::getBy[Name|Ordinal]() - fixes #18
2 parents 3b050f4 + 21f5fc3 commit 58d6533

File tree

2 files changed

+73
-18
lines changed

2 files changed

+73
-18
lines changed

src/MabeEnum/Enum.php

Lines changed: 48 additions & 13 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,45 @@ static public function get($value)
141141
return $instance;
142142
}
143143

144+
/**
145+
* Get an enum by the given name
146+
*
147+
* @param string $name The name to instantiate the enum by
148+
* @return Enum
149+
* @throws InvalidArgumentException On an invalid or unknown name
150+
* @throws LogicException On ambiguous constant values
151+
*/
152+
final public static function getByName($name)
153+
{
154+
$classConst = 'static::' . $name;
155+
if (!defined($classConst)) {
156+
$class = get_called_class();
157+
throw new InvalidArgumentException($class . '::' . $name . ' not defined');
158+
}
159+
return static::get(constant($classConst));
160+
}
161+
162+
/**
163+
* Get an enum by the given ordinal number
164+
*
165+
* @param int $ordinal The ordinal number to instantiate the enum by
166+
* @return Enum
167+
* @throws InvalidArgumentException On an invalid ordinal number
168+
* @throws LogicException On ambiguous constant values
169+
*/
170+
final public static function getByOrdinal($ordinal)
171+
{
172+
$constants = static::getConstants();
173+
$item = array_slice($constants, $ordinal, 1, false);
174+
if (!$item) {
175+
throw new InvalidArgumentException(sprintf(
176+
'Invalid ordinal number, must between 0 and %s',
177+
count($constants) - 1
178+
));
179+
}
180+
return static::get(current($item));
181+
}
182+
144183
/**
145184
* Clears all instantiated enums
146185
*
@@ -149,7 +188,7 @@ static public function get($value)
149188
* @param null|string $class
150189
* @return void
151190
*/
152-
final static function clear()
191+
final static public function clear()
153192
{
154193
$class = get_called_class();
155194

@@ -212,18 +251,14 @@ final static public function getConstants()
212251
* This will be called automatically on calling a method
213252
* with the same name of a defined constant.
214253
*
215-
* @param string $const The name of the constant to instantiate the enum with
216-
* @param array $args There should be no arguments
217-
* @throws BadMethodCallException On an unknown constant name (method name)
218-
* @throws LogicException On ambiguous constant values
254+
* @param string $method The name to instantiate the enum by (called as method)
255+
* @param array $args There should be no arguments
256+
* @return Enum
257+
* @throws InvalidArgumentException On an invalid or unknown name
258+
* @throws LogicException On ambiguous constant values
219259
*/
220-
final public static function __callStatic($const, array $args)
260+
final public static function __callStatic($method, array $args)
221261
{
222-
$classConst = 'static::' . $const;
223-
if (!defined($classConst)) {
224-
$class = get_called_class();
225-
throw new BadMethodCallException($class . '::' . $const . ' not defined');
226-
}
227-
return static::get(constant($classConst));
262+
return static::getByName($method);
228263
}
229264
}

tests/MabeEnumTest/EnumTest.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,37 @@ public function testCallingGetOrdinalTwoTimesWillResultTheSameValue()
7272
$this->assertSame(1, $enum->getOrdinal());
7373
}
7474

75-
public function testInstantiateUsingMagicMethod()
75+
public function testInstantiateUsingOrdinalNumber()
7676
{
77-
$enum = EnumInheritance::ONE();
77+
$enum = EnumInheritance::getByOrdinal(2);
78+
$this->assertSame(2, $enum->getOrdinal());
79+
$this->assertSame('INHERITANCE', $enum->getName());
80+
}
81+
82+
public function testInstantiateUsingInvalidOrdinalNumberThrowsInvalidArgumentException()
83+
{
84+
$this->setExpectedException('InvalidArgumentException');
85+
EnumInheritance::getByOrdinal(3);
86+
}
87+
88+
public function testInstantiateByName()
89+
{
90+
$enum = EnumInheritance::getByName('ONE');
7891
$this->assertInstanceOf('MabeEnumTest\TestAsset\EnumInheritance', $enum);
7992
$this->assertSame(EnumInheritance::ONE, $enum->getValue());
8093
}
8194

82-
public function testInstantiateUsingMagicMethodThrowsBadMethodCallException()
95+
public function testInstantiateByUnknownNameThrowsInvalidArgumentException()
8396
{
84-
$this->setExpectedException('BadMethodCallException');
85-
EnumInheritance::UNKNOWN();
97+
$this->setExpectedException('InvalidArgumentException');
98+
EnumInheritance::getByName('UNKNOWN');
99+
}
100+
101+
public function testInstantiateUsingMagicMethod()
102+
{
103+
$enum = EnumInheritance::ONE();
104+
$this->assertInstanceOf('MabeEnumTest\TestAsset\EnumInheritance', $enum);
105+
$this->assertSame(EnumInheritance::ONE, $enum->getValue());
86106
}
87107

88108
public function testAmbuguousConstantsThrowsLogicException()

0 commit comments

Comments
 (0)