Skip to content

Commit

Permalink
Merge pull request #776 from yob-yob/main
Browse files Browse the repository at this point in the history
Add Support for Predefined Enum Collections in DataObject Initialization
  • Loading branch information
rubenvanassche authored Jun 13, 2024
2 parents 8c7c548 + 0bf01a3 commit c5bcc48
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Casts/EnumCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected function castValue(

/** @var class-string<\BackedEnum> $type */
try {
return $type::from($value);
return $value instanceof $type && $value === $type::from($value->value) ? $value : $type::from($value);
} catch (Throwable $e) {
throw CannotCastEnum::create($type, $value);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Casts/EnumCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,19 @@
)
->toEqual(Uncastable::create());
});


it('it can create data when enum is already casted', function () {
$class = new class () {
public DummyBackedEnum $enum;
};

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

0 comments on commit c5bcc48

Please sign in to comment.