Convert object/database naming conventions.
Adhere to PSR-12 by converting object properties to "lowerCamelCase" for objects and "snake_case" for database interactions. Backward compatibility is maintained: object properties already using "snake_case" will continue to work without issue.
This commit is contained in:
@ -139,9 +139,9 @@ class Model
|
|||||||
|
|
||||||
throw new Exception(
|
throw new Exception(
|
||||||
"\nError at query to database.\n" .
|
"\nError at query to database.\n" .
|
||||||
"Query: $query\n" .
|
"Query: $query\n" .
|
||||||
"Vars: $vars\n" .
|
"Vars: $vars\n" .
|
||||||
"Error:\n" . $e->getMessage()
|
"Error:\n" . $e->getMessage()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -256,14 +256,20 @@ class Model
|
|||||||
$instance = new $class();
|
$instance = new $class();
|
||||||
$reflection = new ReflectionClass($instance);
|
$reflection = new ReflectionClass($instance);
|
||||||
$properties = $reflection->getProperties();
|
$properties = $reflection->getProperties();
|
||||||
$propertyNames = array_column($properties, 'name');
|
$propertyNames = array_map(function ($property) {
|
||||||
|
return static::camelCaseToSnakeCase($property->name);
|
||||||
|
}, $properties);
|
||||||
|
|
||||||
foreach ($elem as $key => $value) {
|
foreach ($elem as $key => $value) {
|
||||||
$index = array_search($key, $propertyNames);
|
$index = array_search($key, $propertyNames);
|
||||||
if (is_numeric($index) && enum_exists($properties[$index]->getType()->getName())) {
|
if (is_numeric($index)) {
|
||||||
$instance->$key = $properties[$index]->getType()->getName()::tryfrom($value);
|
if (enum_exists($properties[$index]->getType()->getName())) {
|
||||||
|
$instance->{$properties[$index]->name} = $properties[$index]->getType()->getName()::tryfrom($value);
|
||||||
|
} else {
|
||||||
|
$instance->{$properties[$index]->name} = $value;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$instance->$key = $value;
|
$instance->{static::snakeCaseToCamelCase($key)} = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -286,17 +292,15 @@ class Model
|
|||||||
$result = [];
|
$result = [];
|
||||||
|
|
||||||
foreach ($properties as $property) {
|
foreach ($properties as $property) {
|
||||||
$result[$property->name] = isset($this->{$property->name})
|
if (!in_array($property->name, static::$ignoreSave)) {
|
||||||
? $this->{$property->name} : null;
|
$result[$this->camelCaseToSnakeCase($property->name)] = isset($this->{$property->name})
|
||||||
}
|
? $this->{$property->name} : null;
|
||||||
|
}
|
||||||
foreach (static::$ignoreSave as $del) {
|
|
||||||
unset($result[$del]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (static::$forceSave as $value) {
|
foreach (static::$forceSave as $value) {
|
||||||
$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) {
|
||||||
@ -305,10 +309,12 @@ class Model
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($property instanceof \UnitEnum) {
|
if ($property instanceof \UnitEnum) {
|
||||||
$result[$i] = $property->value;
|
$result[$i] = $property->value ?? $property->name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
print_r($result);
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -316,7 +322,7 @@ class Model
|
|||||||
* Devuelve el nombre de la clase actual aunque sea una clase extendida.
|
* Devuelve el nombre de la clase actual aunque sea una clase extendida.
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
* Devuelve el nombre de la clase actual.
|
*
|
||||||
*/
|
*/
|
||||||
public static function className(): string
|
public static function className(): string
|
||||||
{
|
{
|
||||||
@ -339,13 +345,39 @@ class Model
|
|||||||
return static::$table;
|
return static::$table;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return static::camelCaseToSnakeCase(static::className()) . static::$tableSufix;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convierte de lowerCamelCase a snake_case
|
||||||
|
*
|
||||||
|
* @param string $string
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected static function camelCaseToSnakeCase(string $string): string
|
||||||
|
{
|
||||||
return strtolower(
|
return strtolower(
|
||||||
preg_replace(
|
preg_replace(
|
||||||
'/(?<!^)[A-Z]/',
|
'/(?<!^)[A-Z]/',
|
||||||
'_$0',
|
'_$0',
|
||||||
static::className()
|
$string
|
||||||
)
|
)
|
||||||
) . static::$tableSufix;
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convierte de snake_case a lowerCamelCase
|
||||||
|
*
|
||||||
|
* @param string $string
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected static function snakeCaseToCamelCase(string $string): string
|
||||||
|
{
|
||||||
|
return preg_replace_callback('/_([a-z])/', function ($matches) {
|
||||||
|
return strtoupper($matches[1]);
|
||||||
|
}, $string);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user