fix(model): support both backed and unit enums

This commit is contained in:
kj
2026-06-13 16:48:07 -03:00
parent 5312e5060e
commit e37035eed8

View File

@@ -300,8 +300,15 @@ class Model
foreach ($elem as $key => $value) {
$index = array_search($key, $propertyNames);
if (is_numeric($index)) {
if (enum_exists($properties[$index]->getType()->getName())) {
$instance->{$properties[$index]->name} = $properties[$index]->getType()->getName()::tryfrom($value);
$type = $properties[$index]->getType();
if (enum_exists($type->getName())) {
if (method_exists($type->getName(), 'tryFrom')) {
$instance->{$properties[$index]->name} = $type->getName()::tryfrom($value);
} elseif (
in_array($value, array_column($type->getName()::cases(), 'name'))
) {
$instance->{$properties[$index]->name} = $type->getName()::{$value};
}
} else {
$instance->{$properties[$index]->name} = $value;
}