Skip to content

Commit 8b14d67

Browse files
committed
Allow id as a property for Enum::from
1 parent efeab73 commit 8b14d67

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/Enum.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ final public static function create(int $id): self
124124
*/
125125
final public static function from(string $property, $value): self
126126
{
127-
return static::create(array_flip(self::getReflectionProperty($property)->getValue())[$value]);
127+
$id = 'id' === $property ? $value : array_flip(self::getReflectionProperty($property)->getValue())[$value];
128+
129+
return static::create($id);
128130
}
129131

130132
final public function getId(): int

tests/EnumTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ public function testFrom(): void
102102
{
103103
self::assertSame('yo', TestEnum::fromName('yo')->getName());
104104
self::assertSame('yo', TestEnum::from('name', 'yo')->getName());
105+
106+
self::assertSame(TestEnum::one(), TestEnum::from('id', 1));
105107
}
106108

107109
public function testUndefinedMethod(): void

0 commit comments

Comments
 (0)