Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class UserStatus extends Enum
// different ways to instantiate an enumerator
$status = UserStatus::get(UserStatus::ACTIVE);
$status = UserStatus::ACTIVE();
$status = UserStatus::getByName('ACTIVE');
$status = UserStatus::getByOrdinal(1);
$status = UserStatus::byName('ACTIVE');
$status = UserStatus::byOrdinal(1);

// available methods to get the selected entry
$status->getValue(); // returns the selected constant value
Expand Down
49 changes: 45 additions & 4 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@ final public static function get($value)
return $value;
}

return static::byValue($value);
}

/**
* Get an enumerator instance by the given value
*
* @param mixed $value
* @return static
* @throws InvalidArgumentException On an unknwon or invalid value
* @throws LogicException On ambiguous constant values
*/
final public static function byValue($value)
{
$class = get_called_class();
$constants = self::detectConstants($class);
$name = array_search($value, $constants, true);
Expand All @@ -188,7 +201,7 @@ final public static function get($value)
* @throws InvalidArgumentException On an invalid or unknown name
* @throws LogicException On ambiguous values
*/
final public static function getByName($name)
final public static function byName($name)
{
$name = (string) $name;
$class = get_called_class();
Expand All @@ -212,7 +225,7 @@ final public static function getByName($name)
* @throws InvalidArgumentException On an invalid ordinal number
* @throws LogicException On ambiguous values
*/
final public static function getByOrdinal($ordinal)
final public static function byOrdinal($ordinal)
{
$ordinal = (int) $ordinal;
$class = get_called_class();
Expand All @@ -233,6 +246,34 @@ final public static function getByOrdinal($ordinal)
return self::$instances[$class][$name] = new $class(current($item), $ordinal);
}

/**
* Get an enumerator instance by the given name
*
* @param string $name The name of the enumerator
* @return static
* @throws InvalidArgumentException On an invalid or unknown name
* @throws LogicException On ambiguous values
* @deprecated
*/
final public static function getByName($name)
{
return static::byName($name);
}

/**
* Get an enumeration instance by the given ordinal number
*
* @param int $ordinal The ordinal number or the enumerator
* @return static
* @throws InvalidArgumentException On an invalid ordinal number
* @throws LogicException On ambiguous values
* @deprecated
*/
final public static function getByOrdinal($ordinal)
{
return static::byOrdinal($ordinal);
}

/**
* Clear all instantiated enumerators of the called class
*
Expand All @@ -253,7 +294,7 @@ final public static function clear()
*/
final public static function getEnumerators()
{
return array_map('self::getByName', array_keys(self::detectConstants(get_called_class())));
return array_map('self::byName', array_keys(self::detectConstants(get_called_class())));
}

/**
Expand Down Expand Up @@ -353,6 +394,6 @@ private static function detectConstants($class)
*/
final public static function __callStatic($method, array $args)
{
return self::getByName($method);
return self::byName($method);
}
}
8 changes: 4 additions & 4 deletions src/EnumSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function current()
{
if ($this->valid()) {
$enumeration = $this->enumeration;
return $enumeration::getByOrdinal($this->ordinal);
return $enumeration::byOrdinal($this->ordinal);
}

return null;
Expand Down Expand Up @@ -376,7 +376,7 @@ public function getValues()
$enumeration = $this->enumeration;
$values = array();
foreach ($this->getOrdinals() as $ord) {
$values[] = $enumeration::getByOrdinal($ord)->getValue();
$values[] = $enumeration::byOrdinal($ord)->getValue();
}
return $values;
}
Expand All @@ -390,7 +390,7 @@ public function getNames()
$enumeration = $this->enumeration;
$names = array();
foreach ($this->getOrdinals() as $ord) {
$names[] = $enumeration::getByOrdinal($ord)->getName();
$names[] = $enumeration::byOrdinal($ord)->getName();
}
return $names;
}
Expand All @@ -404,7 +404,7 @@ public function getEnumerators()
$enumeration = $this->enumeration;
$enumerators = array();
foreach ($this->getOrdinals() as $ord) {
$enumerators[] = $enumeration::getByOrdinal($ord);
$enumerators[] = $enumeration::byOrdinal($ord);
}
return $enumerators;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/MabeEnumTest/EnumSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function testInitEnumThrowsInvalidArgumentExceptionOnInvalidEnum()
public function testIterateOutOfRangeIfLastOrdinalEnumIsSet()
{
$enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
$enum = EnumBasic::getByOrdinal(count(EnumBasic::getConstants()) - 1);
$enum = EnumBasic::byOrdinal(count(EnumBasic::getConstants()) - 1);

$enumSet->attach($enum);
$enumSet->rewind();
Expand Down Expand Up @@ -246,7 +246,7 @@ public function test65EnumerationsSet()
{
$enum = new EnumSet('MabeEnumTest\TestAsset\Enum65');

$this->assertNull($enum->attach(Enum65::getByOrdinal(64)));
$this->assertNull($enum->attach(Enum65::byOrdinal(64)));
$enum->next();
$this->assertTrue($enum->valid());
}
Expand Down
8 changes: 4 additions & 4 deletions tests/MabeEnumTest/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,28 +159,28 @@ public function testCallingGetOrdinalTwoTimesWillResultTheSameValue()

public function testInstantiateUsingOrdinalNumber()
{
$enum = EnumInheritance::getByOrdinal(16);
$enum = EnumInheritance::byOrdinal(16);
$this->assertSame(16, $enum->getOrdinal());
$this->assertSame('INHERITANCE', $enum->getName());
}

public function testInstantiateUsingInvalidOrdinalNumberThrowsInvalidArgumentException()
{
$this->setExpectedException('InvalidArgumentException');
EnumInheritance::getByOrdinal(17);
EnumInheritance::byOrdinal(17);
}

public function testInstantiateByName()
{
$enum = EnumInheritance::getByName('ONE');
$enum = EnumInheritance::byName('ONE');
$this->assertInstanceOf('MabeEnumTest\TestAsset\EnumInheritance', $enum);
$this->assertSame(EnumInheritance::ONE, $enum->getValue());
}

public function testInstantiateByUnknownNameThrowsInvalidArgumentException()
{
$this->setExpectedException('InvalidArgumentException');
EnumInheritance::getByName('UNKNOWN');
EnumInheritance::byName('UNKNOWN');
}

public function testInstantiateUsingMagicMethod()
Expand Down