Skip to content

Commit 5595d4f

Browse files
committed
Merge branch 'feature/getReturnPassedInstance'
2 parents f31e7ba + 65bf4ac commit 5595d4f

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
{
@@ -96,7 +96,7 @@ final public function __wakeup()
9696
/**
9797
* Get the current selected value
9898
*
99-
* @return null|boolean|int|float|string
99+
* @return null|bool|int|float|string
100100
*/
101101
final public function getValue()
102102
{
@@ -142,7 +142,7 @@ final public function getOrdinal()
142142
* Compare this enum against another enum and check if it's the same value
143143
*
144144
* @param mixed $value
145-
* @return boolean
145+
* @return bool
146146
*/
147147
final public function is($enum)
148148
{
@@ -151,17 +151,30 @@ final public function is($enum)
151151
}
152152

153153
/**
154-
* Get an enum of the given value
154+
* Get an enum of the given value or instance
155155
*
156-
* @param static|null|boolean|int|float|string $value
156+
* On passing an extended instance the instance will be returned if the value
157+
* is inherited by the called class or if $tradeExtendedAsUnknown is disabled
158+
* else an InvalidArgumentException will be thrown.
159+
*
160+
* @param static|null|bool|int|float|string $value
161+
* @param bool $tradeExtendedAsUnknown
157162
* @return static
158163
* @throws InvalidArgumentException On an unknwon or invalid value
159164
* @throws LogicException On ambiguous constant values
160165
*/
161-
final public static function get($value)
166+
final public static function get($value, $tradeExtendedAsUnknown = true)
162167
{
163168
if ($value instanceof static) {
164-
$value = $value->getValue();
169+
if ($tradeExtendedAsUnknown && !defined('static::' . $value->getName())) {
170+
throw new InvalidArgumentException(sprintf(
171+
"%s::%s is not inherited from %s",
172+
get_class($value),
173+
$value->getName(),
174+
get_called_class()
175+
));
176+
}
177+
return $value;
165178
}
166179

167180
$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)