Skip to content

Commit

Permalink
Merge pull request #841 from lkmadushan/enum-cast
Browse files Browse the repository at this point in the history
Passing an enum to caster handle without an error
  • Loading branch information
rubenvanassche authored Oct 4, 2024
2 parents d017301 + 77df321 commit 06fd7ce
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Casts/EnumCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ protected function castValue(
return Uncastable::create();
}

if ($value instanceof $type) {
return $value;
}

/** @var class-string<\BackedEnum> $type */
try {
return $type::from($value);
Expand Down
6 changes: 3 additions & 3 deletions tests/Casts/EnumCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
$this->caster = new EnumCast();
});

it('can cast enum', function () {
it('can cast enum', function (mixed $value) {
$class = new class () {
public DummyBackedEnum $enum;
};

expect(
$this->caster->cast(
FakeDataStructureFactory::property($class, 'enum'),
'foo',
$value,
[],
CreationContextFactory::createFromConfig($class::class)->get()
)
)->toEqual(DummyBackedEnum::FOO);
});
})->with(['foo', DummyBackedEnum::FOO]);

it('fails when it cannot cast an enum from value', function () {
$class = new class () {
Expand Down

0 comments on commit 06fd7ce

Please sign in to comment.