refactor(model): extract type casting logic into helper methods
This commit is contained in:
@@ -289,29 +289,20 @@ class Model
|
|||||||
*/
|
*/
|
||||||
protected static function getInstance(array $elem = []): static
|
protected static function getInstance(array $elem = []): static
|
||||||
{
|
{
|
||||||
$class = get_called_class();
|
$class = get_called_class();
|
||||||
$instance = new $class();
|
$instance = new $class();
|
||||||
$reflection = new ReflectionClass($instance);
|
$reflection = new ReflectionClass($instance);
|
||||||
$properties = $reflection->getProperties();
|
$properties = $reflection->getProperties();
|
||||||
$propertyNames = array_map(function ($property) {
|
|
||||||
return static::camelCaseToSnakeCase($property->name);
|
$map = [];
|
||||||
}, $properties);
|
foreach ($properties as $prop) {
|
||||||
|
$map[static::camelCaseToSnakeCase($prop->name)] = $prop;
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($elem as $key => $value) {
|
foreach ($elem as $key => $value) {
|
||||||
$index = array_search($key, $propertyNames);
|
if (isset($map[$key])) {
|
||||||
if (is_numeric($index)) {
|
$property = $map[$key];
|
||||||
$type = $properties[$index]->getType();
|
$instance->{$property->name} = static::castValue($property, $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 {
|
|
||||||
$instance->{$properties[$index]->name} = $value;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
$instance->{static::snakeCaseToCamelCase($key)} = $value;
|
$instance->{static::snakeCaseToCamelCase($key)} = $value;
|
||||||
}
|
}
|
||||||
@@ -322,6 +313,32 @@ class Model
|
|||||||
return $instance;
|
return $instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles type casting for properties, especially for Enums.
|
||||||
|
*
|
||||||
|
* @param ReflectionProperty $property
|
||||||
|
* @param mixed $value
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
protected static function castValue(ReflectionProperty $property, mixed $value): mixed
|
||||||
|
{
|
||||||
|
$type = $property->getType();
|
||||||
|
if (!$type || !enum_exists($typeName = $type->getName())) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (method_exists($typeName, 'tryFrom')) {
|
||||||
|
return $typeName::tryFrom($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (in_array($value, array_column($typeName::cases(), 'name'))) {
|
||||||
|
return $typeName::{$value};
|
||||||
|
}
|
||||||
|
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the attributes to be saved for the current class.
|
* Returns the attributes to be saved for the current class.
|
||||||
* Attributes will be those that are public and not excluded in static::$ignoreSave,
|
* Attributes will be those that are public and not excluded in static::$ignoreSave,
|
||||||
@@ -338,29 +355,37 @@ class Model
|
|||||||
|
|
||||||
foreach ($properties as $property) {
|
foreach ($properties as $property) {
|
||||||
if (!in_array($property->name, static::$dbIgnoreSave)) {
|
if (!in_array($property->name, static::$dbIgnoreSave)) {
|
||||||
$result[$this->camelCaseToSnakeCase($property->name)] = isset($this->{$property->name})
|
$name = $this->camelCaseToSnakeCase($property->name);
|
||||||
? $this->{$property->name} : null;
|
$result[$name] = $this->castToDbValue($this->{$property->name});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (static::$dbForceSave as $value) {
|
foreach (static::$dbForceSave as $value) {
|
||||||
$result[$value] = isset($this->$value)
|
$result[$value] = $this->castToDbValue($this->$value);
|
||||||
? $this->$value : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($result as $i => $property) {
|
|
||||||
if (gettype($property) == 'boolean') {
|
|
||||||
$result[$i] = $property ? '1' : '0';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($property instanceof \UnitEnum) {
|
|
||||||
$result[$i] = $property->value ?? $property->name;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a PHP value to a database-compatible value.
|
||||||
|
*
|
||||||
|
* @param mixed $value
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
protected function castToDbValue(mixed $value): mixed
|
||||||
|
{
|
||||||
|
if (is_bool($value)) {
|
||||||
|
return $value ? '1' : '0';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($value instanceof \UnitEnum) {
|
||||||
|
return $value->value ?? $value->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the name of the current class, even if it's an extended class.
|
* Returns the name of the current class, even if it's an extended class.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user