Model properties now can be typed as enums.

With this PHP 8.0 support is dropped.
This commit is contained in:
KJ 2024-08-27 19:01:02 -04:00
parent daf7250882
commit df424ffab5
1 changed files with 15 additions and 4 deletions

View File

@ -244,8 +244,15 @@ class Model {
{
$class = get_called_class();
$instance = new $class;
$reflection = new ReflectionClass($instance);
$properties = $reflection->getProperties();
$propertyNames = array_column($properties, 'name');
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;
}
@ -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;
}