fix(model): support both backed and unit enums
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user