Skip to content

Commit c6092db

Browse files
committed
Enum::get() An instance of the called class will return an enum of the same value of the called class
1 parent b52575f commit c6092db

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

src/MabeEnum/Enum.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,17 @@ final public function getOrdinal()
123123
/**
124124
* Get an enum of the given value
125125
*
126-
* @param null|boolean|int|float|string $value
126+
* @param static|null|boolean|int|float|string $value
127127
* @return static
128128
* @throws InvalidArgumentException On an unknwon or invalid value
129129
* @throws LogicException On ambiguous constant values
130130
*/
131131
final static public function get($value)
132132
{
133+
if ($value instanceof static) {
134+
$value = $value->getValue();
135+
}
136+
133137
$class = get_called_class();
134138
$constants = self::detectConstants($class);
135139
$name = array_search($value, $constants, true);

tests/MabeEnumTest/EnumTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MabeEnumTest;
44

55
use MabeEnumTest\TestAsset\EnumBasic;
6+
use MabeEnumTest\TestAsset\EnumBasic2;
67
use MabeEnumTest\TestAsset\EnumInheritance;
78
use MabeEnumTest\TestAsset\EnumAmbiguous;
89
use PHPUnit_Framework_TestCase as TestCase;
@@ -90,6 +91,37 @@ public function testGetWithInvalidTypeOfValueThrowsInvalidArgumentException()
9091
EnumBasic::get(array());
9192
}
9293

94+
public function testGetByInstance()
95+
{
96+
$enum1 = EnumBasic::get(EnumBasic::ONE);
97+
$enum2 = EnumBasic::get($enum1);
98+
$this->assertSame($enum1, $enum2);
99+
}
100+
101+
public function testGetByInheritInstance()
102+
{
103+
$enumInherit = EnumInheritance::get(EnumInheritance::ONE);
104+
$enum1 = EnumBasic::get(EnumBasic::ONE);
105+
$enum2 = EnumBasic::get($enumInherit);
106+
$this->assertSame($enum1, $enum2);
107+
}
108+
109+
public function testGetByInheritInstanceThrowsInvalidArgumentExceptionOnUnknownValue()
110+
{
111+
$enumInherit = EnumInheritance::get(EnumInheritance::INHERITANCE);
112+
113+
$this->setExpectedException('InvalidArgumentException');
114+
EnumBasic::get($enumInherit);
115+
}
116+
117+
public function testGetByInstanceOfDifferentBaseThrowsInvalidArgumentException()
118+
{
119+
$enumDiff = EnumBasic2::get(EnumBasic2::ONE);
120+
121+
$this->setExpectedException('InvalidArgumentException');
122+
EnumBasic::get($enumDiff);
123+
}
124+
93125
public function testGetAllValues()
94126
{
95127
$constants = EnumBasic::getConstants();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace MabeEnumTest\TestAsset;
4+
5+
use MabeEnum\Enum;
6+
7+
/**
8+
* Unit tests for the class MabeEnum\Enum
9+
*
10+
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
11+
* @copyright Copyright (c) 2013 Marc Bennewitz
12+
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
13+
*/
14+
class EnumBasic2 extends Enum
15+
{
16+
const ONE = 1;
17+
const TWO = 2;
18+
const THREE = 3;
19+
const FOUR = 4;
20+
const FIVE = 5;
21+
const SIX = 6;
22+
const SEVEN = 7;
23+
const EIGHT = 8;
24+
const NINE = 9;
25+
const ZERO = 0;
26+
27+
const FLOAT = 0.123;
28+
const STR = 'str';
29+
const STR_EMPTY = '';
30+
const NIL = null;
31+
const BOOLEAN_TRUE = true;
32+
const BOOLEAN_FALSE = false;
33+
}

0 commit comments

Comments
 (0)