fix(model): support both backed and unit enums
This commit is contained in:
@@ -300,8 +300,15 @@ class Model
|
|||||||
foreach ($elem as $key => $value) {
|
foreach ($elem as $key => $value) {
|
||||||
$index = array_search($key, $propertyNames);
|
$index = array_search($key, $propertyNames);
|
||||||
if (is_numeric($index)) {
|
if (is_numeric($index)) {
|
||||||
if (enum_exists($properties[$index]->getType()->getName())) {
|
$type = $properties[$index]->getType();
|
||||||
$instance->{$properties[$index]->name} = $properties[$index]->getType()->getName()::tryfrom($value);
|
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 {
|
} else {
|
||||||
$instance->{$properties[$index]->name} = $value;
|
$instance->{$properties[$index]->name} = $value;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user