Skip to content

Commit f713988

Browse files
authored
Merge pull request #80 from marc-mabe/enum_get_by
renamed Enum::getBy[Name|Ordinal]() to Enum::by[Name|Ordinal]() and added Enum::byValue()
2 parents b9a6b69 + 5b9d27d commit f713988

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
@@ -169,6 +169,19 @@ final public static function get($value)
169169
return $value;
170170
}
171171

172+
return static::byValue($value);
173+
}
174+
175+
/**
176+
* Get an enumerator instance by the given value
177+
*
178+
* @param mixed $value
179+
* @return static
180+
* @throws InvalidArgumentException On an unknwon or invalid value
181+
* @throws LogicException On ambiguous constant values
182+
*/
183+
final public static function byValue($value)
184+
{
172185
$class = get_called_class();
173186
$constants = self::detectConstants($class);
174187
$name = array_search($value, $constants, true);
@@ -194,7 +207,7 @@ final public static function get($value)
194207
* @throws InvalidArgumentException On an invalid or unknown name
195208
* @throws LogicException On ambiguous values
196209
*/
197-
final public static function getByName($name)
210+
final public static function byName($name)
198211
{
199212
$name = (string) $name;
200213
$class = get_called_class();
@@ -218,7 +231,7 @@ final public static function getByName($name)
218231
* @throws InvalidArgumentException On an invalid ordinal number
219232
* @throws LogicException On ambiguous values
220233
*/
221-
final public static function getByOrdinal($ordinal)
234+
final public static function byOrdinal($ordinal)
222235
{
223236
$ordinal = (int) $ordinal;
224237
$class = get_called_class();
@@ -239,6 +252,34 @@ final public static function getByOrdinal($ordinal)
239252
return self::$instances[$class][$name] = new $class(current($item), $ordinal);
240253
}
241254

255+
/**
256+
* Get an enumerator instance by the given name
257+
*
258+
* @param string $name The name of the enumerator
259+
* @return static
260+
* @throws InvalidArgumentException On an invalid or unknown name
261+
* @throws LogicException On ambiguous values
262+
* @deprecated
263+
*/
264+
final public static function getByName($name)
265+
{
266+
return static::byName($name);
267+
}
268+
269+
/**
270+
* Get an enumeration instance by the given ordinal number
271+
*
272+
* @param int $ordinal The ordinal number or the enumerator
273+
* @return static
274+
* @throws InvalidArgumentException On an invalid ordinal number
275+
* @throws LogicException On ambiguous values
276+
* @deprecated
277+
*/
278+
final public static function getByOrdinal($ordinal)
279+
{
280+
return static::byOrdinal($ordinal);
281+
}
282+
242283
/**
243284
* Clear all instantiated enumerators of the called class
244285
*
@@ -260,7 +301,7 @@ final public static function clear()
260301
*/
261302
final public static function getEnumerators()
262303
{
263-
return array_map('self::getByName', array_keys(self::detectConstants(get_called_class())));
304+
return array_map('self::byName', array_keys(self::detectConstants(get_called_class())));
264305
}
265306

266307
/**
@@ -390,6 +431,6 @@ private static function detectConstants($class)
390431
*/
391432
final public static function __callStatic($method, array $args)
392433
{
393-
return self::getByName($method);
434+
return self::byName($method);
394435
}
395436
}

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
@@ -193,28 +193,28 @@ public function testCallingGetOrdinalTwoTimesWillResultTheSameValue()
193193

194194
public function testInstantiateUsingOrdinalNumber()
195195
{
196-
$enum = EnumInheritance::getByOrdinal(16);
196+
$enum = EnumInheritance::byOrdinal(16);
197197
$this->assertSame(16, $enum->getOrdinal());
198198
$this->assertSame('INHERITANCE', $enum->getName());
199199
}
200200

201201
public function testInstantiateUsingInvalidOrdinalNumberThrowsInvalidArgumentException()
202202
{
203203
$this->setExpectedException('InvalidArgumentException');
204-
EnumInheritance::getByOrdinal(17);
204+
EnumInheritance::byOrdinal(17);
205205
}
206206

207207
public function testInstantiateByName()
208208
{
209-
$enum = EnumInheritance::getByName('ONE');
209+
$enum = EnumInheritance::byName('ONE');
210210
$this->assertInstanceOf('MabeEnumTest\TestAsset\EnumInheritance', $enum);
211211
$this->assertSame(EnumInheritance::ONE, $enum->getValue());
212212
}
213213

214214
public function testInstantiateByUnknownNameThrowsInvalidArgumentException()
215215
{
216216
$this->setExpectedException('InvalidArgumentException');
217-
EnumInheritance::getByName('UNKNOWN');
217+
EnumInheritance::byName('UNKNOWN');
218218
}
219219

220220
public function testInstantiateUsingMagicMethod()

0 commit comments

Comments
 (0)