Skip to content

Commit e7b987d

Browse files
committed
fixes #39: Enum::get() should return passed instance
1 parent ab50cb7 commit e7b987d

File tree

2 files changed

+40
-20
lines changed

2 files changed

+40
-20
lines changed

src/MabeEnum/Enum.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ abstract class Enum
1818
/**
1919
* The selected value
2020
*
21-
* @var null|boolean|int|float|string
21+
* @var null|bool|int|float|string
2222
*/
2323
private $value;
2424

@@ -46,8 +46,8 @@ abstract class Enum
4646
/**
4747
* Constructor
4848
*
49-
* @param null|boolean|int|float|string $value The value to select
50-
* @param int|null $ordinal The ordinal number of the value
49+
* @param null|bool|int|float|string $value The value to select
50+
* @param int|null $ordinal The ordinal number of the value
5151
*/
5252
final private function __construct($value, $ordinal = null)
5353
{
@@ -78,7 +78,7 @@ final private function __clone()
7878
/**
7979
* Get the current selected value
8080
*
81-
* @return null|boolean|int|float|string
81+
* @return null|bool|int|float|string
8282
*/
8383
final public function getValue()
8484
{
@@ -124,25 +124,38 @@ final public function getOrdinal()
124124
* Compare this enum against another enum and check if it's the same value
125125
*
126126
* @param mixed $value
127-
* @return boolean
127+
* @return bool
128128
*/
129129
final public function is($enum)
130130
{
131131
return $this->value === $enum || ($enum instanceof static && $this->value === $enum->getValue());
132132
}
133133

134134
/**
135-
* Get an enum of the given value
135+
* Get an enum of the given value or instance
136136
*
137-
* @param static|null|boolean|int|float|string $value
137+
* On passing an extended instance the instance will be returned if the value
138+
* is inherited by the called class or if $tradeExtendedAsUnknown is disabled
139+
* else an InvalidArgumentException will be thrown.
140+
*
141+
* @param static|null|bool|int|float|string $value
142+
* @param bool $tradeExtendedAsUnknown
138143
* @return static
139144
* @throws InvalidArgumentException On an unknwon or invalid value
140145
* @throws LogicException On ambiguous constant values
141146
*/
142-
final public static function get($value)
147+
final public static function get($value, $tradeExtendedAsUnknown = true)
143148
{
144149
if ($value instanceof static) {
145-
$value = $value->getValue();
150+
if ($tradeExtendedAsUnknown && !defined(get_called_class() . '::' . $value->getName())) {
151+
throw new InvalidArgumentException(sprintf(
152+
"%s::%s is not inherited from %s",
153+
get_class($value),
154+
$value->getName(),
155+
get_called_class()
156+
));
157+
}
158+
return $value;
146159
}
147160

148161
$class = get_called_class();

tests/MabeEnumTest/EnumTest.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,28 +98,35 @@ public function testGetByInstance()
9898
$this->assertSame($enum1, $enum2);
9999
}
100100

101-
public function testGetByInheritInstance()
101+
public function testGetByExtendedInstanceOfKnownValue()
102102
{
103-
$enumInherit = EnumInheritance::get(EnumInheritance::ONE);
104-
$enum1 = EnumBasic::get(EnumBasic::ONE);
105-
$enum2 = EnumBasic::get($enumInherit);
106-
$this->assertSame($enum1, $enum2);
103+
$enum = EnumInheritance::get(EnumInheritance::ONE);
104+
$this->assertSame($enum, EnumBasic::get($enum));
105+
}
106+
107+
public function testGetByExtendedInstanceOfUnknownValueExplicitAllowed()
108+
{
109+
$enum = EnumInheritance::get(EnumInheritance::INHERITANCE);
110+
$this->assertSame($enum, EnumBasic::get($enum, false));
107111
}
108112

109-
public function testGetByInheritInstanceThrowsInvalidArgumentExceptionOnUnknownValue()
113+
public function testGetByExtendedInstanceOfUnknownValueThrowsInvalidArgumentException()
110114
{
111-
$enumInherit = EnumInheritance::get(EnumInheritance::INHERITANCE);
115+
$enum = EnumInheritance::get(EnumInheritance::INHERITANCE);
112116

113-
$this->setExpectedException('InvalidArgumentException');
114-
EnumBasic::get($enumInherit);
117+
$this->setExpectedException(
118+
'InvalidArgumentException',
119+
'MabeEnumTest\TestAsset\EnumInheritance::INHERITANCE is not inherited from MabeEnumTest\TestAsset\EnumBasic'
120+
);
121+
EnumBasic::get($enum, true);
115122
}
116123

117124
public function testGetByInstanceOfDifferentBaseThrowsInvalidArgumentException()
118125
{
119-
$enumDiff = EnumBasic2::get(EnumBasic2::ONE);
126+
$enum = EnumBasic2::get(EnumBasic2::ONE);
120127

121128
$this->setExpectedException('InvalidArgumentException');
122-
EnumBasic::get($enumDiff);
129+
EnumBasic::get($enum);
123130
}
124131

125132
public function testGetAllValues()

0 commit comments

Comments
 (0)