Model properties now can be typed as enums.
With this PHP 8.0 support is dropped.
This commit is contained in:
parent
daf7250882
commit
df424ffab5
@ -244,8 +244,15 @@ class Model {
|
|||||||
{
|
{
|
||||||
$class = get_called_class();
|
$class = get_called_class();
|
||||||
$instance = new $class;
|
$instance = new $class;
|
||||||
|
$reflection = new ReflectionClass($instance);
|
||||||
|
$properties = $reflection->getProperties();
|
||||||
|
$propertyNames = array_column($properties, 'name');
|
||||||
|
|
||||||
foreach ($elem as $key => $value) {
|
foreach ($elem as $key => $value) {
|
||||||
|
$index = array_search($key, $propertyNames);
|
||||||
|
if (is_numeric($index) && enum_exists($properties[$index]->getType()->getName()))
|
||||||
|
$instance->$key = $properties[$index]->getType()->getName()::tryfrom($value);
|
||||||
|
else
|
||||||
$instance->$key = $value;
|
$instance->$key = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -278,10 +285,14 @@ class Model {
|
|||||||
$result[$value] = isset($this->$value)
|
$result[$value] = isset($this->$value)
|
||||||
? $this->$value: null;
|
? $this->$value: null;
|
||||||
|
|
||||||
foreach ($result as $i => $property)
|
foreach ($result as $i => $property) {
|
||||||
if (gettype($property) == 'boolean')
|
if (gettype($property) == 'boolean')
|
||||||
$result[$i] = $property ? '1' : '0';
|
$result[$i] = $property ? '1' : '0';
|
||||||
|
|
||||||
|
if ($property instanceof \UnitEnum)
|
||||||
|
$result[$i] = $property->value;
|
||||||
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user