Skip to content

Commit 21f5fc3

Browse files
committed
Implemented Enum::getByName(), fixes #18
1 parent 6183bfe commit 21f5fc3

File tree

2 files changed

+44
-24
lines changed

2 files changed

+44
-24
lines changed

src/MabeEnum/Enum.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,24 @@ final 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+
144162
/**
145163
* Get an enum by the given ordinal number
146164
*
@@ -233,19 +251,14 @@ final static public function getConstants()
233251
* This will be called automatically on calling a method
234252
* with the same name of a defined constant.
235253
*
236-
* @param string $const The name of the constant to instantiate the enum with
237-
* @param array $args There should be no arguments
254+
* @param string $method The name to instantiate the enum by (called as method)
255+
* @param array $args There should be no arguments
238256
* @return Enum
239-
* @throws BadMethodCallException On an unknown constant name (method name)
240-
* @throws LogicException On ambiguous constant values
257+
* @throws InvalidArgumentException On an invalid or unknown name
258+
* @throws LogicException On ambiguous constant values
241259
*/
242-
final public static function __callStatic($const, array $args)
260+
final public static function __callStatic($method, array $args)
243261
{
244-
$classConst = 'static::' . $const;
245-
if (!defined($classConst)) {
246-
$class = get_called_class();
247-
throw new BadMethodCallException($class . '::' . $const . ' not defined');
248-
}
249-
return static::get(constant($classConst));
262+
return static::getByName($method);
250263
}
251264
}

tests/MabeEnumTest/EnumTest.php

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

75-
public function testInstantiateUsingMagicMethod()
76-
{
77-
$enum = EnumInheritance::ONE();
78-
$this->assertInstanceOf('MabeEnumTest\TestAsset\EnumInheritance', $enum);
79-
$this->assertSame(EnumInheritance::ONE, $enum->getValue());
80-
}
81-
82-
public function testInstantiateUsingMagicMethodThrowsBadMethodCallException()
83-
{
84-
$this->setExpectedException('BadMethodCallException');
85-
EnumInheritance::UNKNOWN();
86-
}
87-
8875
public function testInstantiateUsingOrdinalNumber()
8976
{
9077
$enum = EnumInheritance::getByOrdinal(2);
@@ -98,6 +85,26 @@ public function testInstantiateUsingInvalidOrdinalNumberThrowsInvalidArgumentExc
9885
EnumInheritance::getByOrdinal(3);
9986
}
10087

88+
public function testInstantiateByName()
89+
{
90+
$enum = EnumInheritance::getByName('ONE');
91+
$this->assertInstanceOf('MabeEnumTest\TestAsset\EnumInheritance', $enum);
92+
$this->assertSame(EnumInheritance::ONE, $enum->getValue());
93+
}
94+
95+
public function testInstantiateByUnknownNameThrowsInvalidArgumentException()
96+
{
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());
106+
}
107+
101108
public function testAmbuguousConstantsThrowsLogicException()
102109
{
103110
$this->setExpectedException('LogicException');

0 commit comments

Comments
 (0)