Skip to content

Commit 30d347e

Browse files
Method created to find the key's value of an Enum and too its test
1 parent 955f570 commit 30d347e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Enum.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,17 @@ public static function search($value)
165165
return array_search($value, static::toArray(), true);
166166
}
167167

168+
/**
169+
* Returns the key's value
170+
*
171+
* @param $key
172+
* @return mixed
173+
*/
174+
public static function findValue($key)
175+
{
176+
return static::toArray()[$key] ?? null;
177+
}
178+
168179
/**
169180
* Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant
170181
*

tests/EnumTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,18 @@ public function testIsValidKey()
189189
$this->assertFalse(EnumFixture::isValidKey('BAZ'));
190190
}
191191

192+
/**
193+
* findValue($key)
194+
*/
195+
public function testFindValue()
196+
{
197+
$enums = EnumFixture::toArray();
198+
199+
foreach ($enums as $key => $value) {
200+
$this->assertEquals($value, EnumFixture::findValue($key));
201+
}
202+
}
203+
192204
/**
193205
* search()
194206
* @see https://github.com/myclabs/php-enum/issues/13

0 commit comments

Comments
 (0)