Skip to content

Commit 5b9d27d

Browse files
committed
renamed Enum::getBy[Name|Ordinal]() to Enum::by[Name|Ordinal]() and added Enum::byValue()
1 parent a496c91 commit 5b9d27d

File tree

5 files changed

+57
-16
lines changed

5 files changed

+57
-16
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ class UserStatus extends Enum
7575
// different ways to instantiate an enumerator
7676
$status = UserStatus::get(UserStatus::ACTIVE);
7777
$status = UserStatus::ACTIVE();
78-
$status = UserStatus::getByName('ACTIVE');
79-
$status = UserStatus::getByOrdinal(1);
78+
$status = UserStatus::byName('ACTIVE');
79+
$status = UserStatus::byOrdinal(1);
8080

8181
// available methods to get the selected entry
8282
$status->getValue(); // returns the selected constant value

src/Enum.php

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@ final public static function get($value)
163163
return $value;
164164
}
165165

166+
return static::byValue($value);
167+
}
168+
169+
/**
170+
* Get an enumerator instance by the given value
171+
*
172+
* @param mixed $value
173+
* @return static
174+
* @throws InvalidArgumentException On an unknwon or invalid value
175+
* @throws LogicException On ambiguous constant values
176+
*/
177+
final public static function byValue($value)
178+
{
166179
$class = get_called_class();
167180
$constants = self::detectConstants($class);
168181
$name = array_search($value, $constants, true);
@@ -188,7 +201,7 @@ final public static function get($value)
188201
* @throws InvalidArgumentException On an invalid or unknown name
189202
* @throws LogicException On ambiguous values
190203
*/
191-
final public static function getByName($name)
204+
final public static function byName($name)
192205
{
193206
$name = (string) $name;
194207
$class = get_called_class();
@@ -212,7 +225,7 @@ final public static function getByName($name)
212225
* @throws InvalidArgumentException On an invalid ordinal number
213226
* @throws LogicException On ambiguous values
214227
*/
215-
final public static function getByOrdinal($ordinal)
228+
final public static function byOrdinal($ordinal)
216229
{
217230
$ordinal = (int) $ordinal;
218231
$class = get_called_class();
@@ -233,6 +246,34 @@ final public static function getByOrdinal($ordinal)
233246
return self::$instances[$class][$name] = new $class(current($item), $ordinal);
234247
}
235248

249+
/**
250+
* Get an enumerator instance by the given name
251+
*
252+
* @param string $name The name of the enumerator
253+
* @return static
254+
* @throws InvalidArgumentException On an invalid or unknown name
255+
* @throws LogicException On ambiguous values
256+
* @deprecated
257+
*/
258+
final public static function getByName($name)
259+
{
260+
return static::byName($name);
261+
}
262+
263+
/**
264+
* Get an enumeration instance by the given ordinal number
265+
*
266+
* @param int $ordinal The ordinal number or the enumerator
267+
* @return static
268+
* @throws InvalidArgumentException On an invalid ordinal number
269+
* @throws LogicException On ambiguous values
270+
* @deprecated
271+
*/
272+
final public static function getByOrdinal($ordinal)
273+
{
274+
return static::byOrdinal($ordinal);
275+
}
276+
236277
/**
237278
* Clear all instantiated enumerators of the called class
238279
*
@@ -253,7 +294,7 @@ final public static function clear()
253294
*/
254295
final public static function getEnumerators()
255296
{
256-
return array_map('self::getByName', array_keys(self::detectConstants(get_called_class())));
297+
return array_map('self::byName', array_keys(self::detectConstants(get_called_class())));
257298
}
258299

259300
/**
@@ -353,6 +394,6 @@ private static function detectConstants($class)
353394
*/
354395
final public static function __callStatic($method, array $args)
355396
{
356-
return self::getByName($method);
397+
return self::byName($method);
357398
}
358399
}

src/EnumSet.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function current()
125125
{
126126
if ($this->valid()) {
127127
$enumeration = $this->enumeration;
128-
return $enumeration::getByOrdinal($this->ordinal);
128+
return $enumeration::byOrdinal($this->ordinal);
129129
}
130130

131131
return null;
@@ -376,7 +376,7 @@ public function getValues()
376376
$enumeration = $this->enumeration;
377377
$values = array();
378378
foreach ($this->getOrdinals() as $ord) {
379-
$values[] = $enumeration::getByOrdinal($ord)->getValue();
379+
$values[] = $enumeration::byOrdinal($ord)->getValue();
380380
}
381381
return $values;
382382
}
@@ -390,7 +390,7 @@ public function getNames()
390390
$enumeration = $this->enumeration;
391391
$names = array();
392392
foreach ($this->getOrdinals() as $ord) {
393-
$names[] = $enumeration::getByOrdinal($ord)->getName();
393+
$names[] = $enumeration::byOrdinal($ord)->getName();
394394
}
395395
return $names;
396396
}
@@ -404,7 +404,7 @@ public function getEnumerators()
404404
$enumeration = $this->enumeration;
405405
$enumerators = array();
406406
foreach ($this->getOrdinals() as $ord) {
407-
$enumerators[] = $enumeration::getByOrdinal($ord);
407+
$enumerators[] = $enumeration::byOrdinal($ord);
408408
}
409409
return $enumerators;
410410
}

tests/MabeEnumTest/EnumSetTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public function testInitEnumThrowsInvalidArgumentExceptionOnInvalidEnum()
174174
public function testIterateOutOfRangeIfLastOrdinalEnumIsSet()
175175
{
176176
$enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
177-
$enum = EnumBasic::getByOrdinal(count(EnumBasic::getConstants()) - 1);
177+
$enum = EnumBasic::byOrdinal(count(EnumBasic::getConstants()) - 1);
178178

179179
$enumSet->attach($enum);
180180
$enumSet->rewind();
@@ -246,7 +246,7 @@ public function test65EnumerationsSet()
246246
{
247247
$enum = new EnumSet('MabeEnumTest\TestAsset\Enum65');
248248

249-
$this->assertNull($enum->attach(Enum65::getByOrdinal(64)));
249+
$this->assertNull($enum->attach(Enum65::byOrdinal(64)));
250250
$enum->next();
251251
$this->assertTrue($enum->valid());
252252
}

tests/MabeEnumTest/EnumTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,28 +159,28 @@ public function testCallingGetOrdinalTwoTimesWillResultTheSameValue()
159159

160160
public function testInstantiateUsingOrdinalNumber()
161161
{
162-
$enum = EnumInheritance::getByOrdinal(16);
162+
$enum = EnumInheritance::byOrdinal(16);
163163
$this->assertSame(16, $enum->getOrdinal());
164164
$this->assertSame('INHERITANCE', $enum->getName());
165165
}
166166

167167
public function testInstantiateUsingInvalidOrdinalNumberThrowsInvalidArgumentException()
168168
{
169169
$this->setExpectedException('InvalidArgumentException');
170-
EnumInheritance::getByOrdinal(17);
170+
EnumInheritance::byOrdinal(17);
171171
}
172172

173173
public function testInstantiateByName()
174174
{
175-
$enum = EnumInheritance::getByName('ONE');
175+
$enum = EnumInheritance::byName('ONE');
176176
$this->assertInstanceOf('MabeEnumTest\TestAsset\EnumInheritance', $enum);
177177
$this->assertSame(EnumInheritance::ONE, $enum->getValue());
178178
}
179179

180180
public function testInstantiateByUnknownNameThrowsInvalidArgumentException()
181181
{
182182
$this->setExpectedException('InvalidArgumentException');
183-
EnumInheritance::getByName('UNKNOWN');
183+
EnumInheritance::byName('UNKNOWN');
184184
}
185185

186186
public function testInstantiateUsingMagicMethod()

0 commit comments

Comments
 (0)