Skip to content

Commit 68a5a47

Browse files
committed
Allow get id via Enum::get('id')
1 parent 7544592 commit 68a5a47

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ DriveWheel::all($ids); // [DriveWheel::front(), DriveWheel::rear()]
6060
DriveWheel::all($ids, $reverse = true); // [DriveWheel::allDrive()]
6161

6262
// Methods
63-
$drive->getId(); // 1
64-
(string) $drive; // '1'
63+
$drive->getId(); // 1
64+
$drive->get('id'); // 1
65+
(string) $drive; // '1'
6566

6667
$drive->getName(); // 'front'
6768

src/Enum.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ final public function getName(): string
151151
*/
152152
final public function get(string $property)
153153
{
154+
if ('id' === $property) {
155+
return $this->id;
156+
}
157+
154158
if (!array_key_exists($property, self::$properties[static::class])) {
155159
throw new InvalidArgumentException(sprintf('Property "%s" not exist at class "%s"', $property, static::class));
156160
}

tests/EnumTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public function testGet(): void
4040

4141
$this->expectException(InvalidArgumentException::class);
4242
TestEnum::two()->get('undefined_property');
43+
44+
self::assertSame(1, TestEnum::one()->get('id'));
45+
self::assertSame(2, TestEnum::one()->get('id'));
4346
}
4447

4548
public function testCustomPropertyValue(): void

0 commit comments

Comments
 (0)