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
@ -242,11 +242,18 @@ class Model {
|
||||
*/
|
||||
protected static function getInstance(array $elem = []): static
|
||||
{
|
||||
$class = get_called_class();
|
||||
$instance = new $class;
|
||||
$class = get_called_class();
|
||||
$instance = new $class;
|
||||
$reflection = new ReflectionClass($instance);
|
||||
$properties = $reflection->getProperties();
|
||||
$propertyNames = array_column($properties, 'name');
|
||||
|
||||
foreach ($elem as $key => $value) {
|
||||
$instance->$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;
|
||||
}
|
||||
|
||||
return $instance;
|
||||
@ -278,10 +285,14 @@ class Model {
|
||||
$result[$value] = isset($this->$value)
|
||||
? $this->$value: null;
|
||||
|
||||
foreach ($result as $i => $property)
|
||||
foreach ($result as $i => $property) {
|
||||
if (gettype($property) == 'boolean')
|
||||
$result[$i] = $property ? '1' : '0';
|
||||
|
||||
if ($property instanceof \UnitEnum)
|
||||
$result[$i] = $property->value;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user