Reorganize code for PSR and others code prettify.
This commit is contained in:
@ -49,7 +49,8 @@ class Model {
|
||||
*
|
||||
* @return PDO
|
||||
*/
|
||||
protected static function db() : PDO {
|
||||
protected static function db(): PDO
|
||||
{
|
||||
if (is_null(static::$db))
|
||||
static::$db = Database::getInstance();
|
||||
|
||||
@ -62,7 +63,8 @@ class Model {
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function beginTransaction() : bool {
|
||||
public function beginTransaction(): bool
|
||||
{
|
||||
return static::db()->beginTransaction();
|
||||
}
|
||||
|
||||
@ -72,7 +74,8 @@ class Model {
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function rollBack() : bool {
|
||||
public function rollBack(): bool
|
||||
{
|
||||
return static::db()->rollBack();
|
||||
}
|
||||
|
||||
@ -82,7 +85,8 @@ class Model {
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function commit() : bool {
|
||||
public function commit(): bool
|
||||
{
|
||||
return static::db()->commit();
|
||||
}
|
||||
|
||||
@ -103,7 +107,8 @@ class Model {
|
||||
* @return array
|
||||
* Contiene el resultado de la llamada SQL .
|
||||
*/
|
||||
protected static function query(string $query, bool $resetQuery = true) : array {
|
||||
protected static function query(string $query, bool $resetQuery = true): array
|
||||
{
|
||||
$db = static::db();
|
||||
|
||||
try {
|
||||
@ -136,7 +141,8 @@ class Model {
|
||||
* Reinicia la configuración de la sentencia SQL.
|
||||
* @return void
|
||||
*/
|
||||
protected static function resetQuery(): void {
|
||||
protected static function resetQuery(): void
|
||||
{
|
||||
static::$querySelect = [
|
||||
'select' => ['*'],
|
||||
'where' => '',
|
||||
@ -158,7 +164,8 @@ class Model {
|
||||
* @return string
|
||||
* Contiene la sentencia SQL.
|
||||
*/
|
||||
protected static function buildQuery() : string {
|
||||
protected static function buildQuery(): string
|
||||
{
|
||||
$sql = 'SELECT '.join(', ', static::$querySelect['select']);
|
||||
|
||||
if (static::$querySelect['from'] != '')
|
||||
@ -201,7 +208,8 @@ class Model {
|
||||
* @return string
|
||||
* Parámetro de sustitución.
|
||||
*/
|
||||
private static function bindValue(string $value) : string{
|
||||
private static function bindValue(string $value): string
|
||||
{
|
||||
$index = ':v_'.count(static::$queryVars);
|
||||
static::$queryVars[$index] = $value;
|
||||
return $index;
|
||||
@ -217,7 +225,8 @@ class Model {
|
||||
* @return static
|
||||
* Retorna un objeto de la clase actual.
|
||||
*/
|
||||
protected static function getInstance(array $elem = []) : static {
|
||||
protected static function getInstance(array $elem = []): static
|
||||
{
|
||||
$class = get_called_class();
|
||||
$instance = new $class;
|
||||
|
||||
@ -237,7 +246,8 @@ class Model {
|
||||
* @return array
|
||||
* Contiene los atributos indexados del objeto actual.
|
||||
*/
|
||||
protected function getVars() : array {
|
||||
protected function getVars(): array
|
||||
{
|
||||
$reflection = new ReflectionClass($this);
|
||||
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
|
||||
$result = [];
|
||||
@ -266,7 +276,8 @@ class Model {
|
||||
* @return string
|
||||
* Devuelve el nombre de la clase actual.
|
||||
*/
|
||||
public static function className() : string {
|
||||
public static function className(): string
|
||||
{
|
||||
return strtolower(substr(strrchr(get_called_class(), '\\'), 1));
|
||||
}
|
||||
|
||||
@ -277,7 +288,8 @@ class Model {
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function table() : string {
|
||||
protected static function table(): string
|
||||
{
|
||||
if (isset(static::$table))
|
||||
return static::$table;
|
||||
return static::className().static::$tableSufix;
|
||||
@ -287,7 +299,8 @@ class Model {
|
||||
* Actualiza los valores en la BD con los valores del objeto actual.
|
||||
* @return void
|
||||
*/
|
||||
protected function update(): void {
|
||||
protected function update(): void
|
||||
{
|
||||
$atts = $this->getVars();
|
||||
|
||||
foreach ($atts as $key => $value) {
|
||||
@ -316,7 +329,8 @@ class Model {
|
||||
* objeto actual.
|
||||
* @return void
|
||||
*/
|
||||
protected function add(): void {
|
||||
protected function add(): void
|
||||
{
|
||||
$db = static::db();
|
||||
$atts = $this->getVars();
|
||||
|
||||
@ -341,7 +355,8 @@ class Model {
|
||||
* llama a update para actualizar o add para insertar una nueva fila.
|
||||
* @return void
|
||||
*/
|
||||
public function save(): void {
|
||||
public function save(): void
|
||||
{
|
||||
$pk = static::$primaryKey;
|
||||
if (isset($this->$pk))
|
||||
$this->update();
|
||||
@ -370,7 +385,8 @@ class Model {
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function select(array $columns) : static {
|
||||
public static function select(array $columns): static
|
||||
{
|
||||
static::$querySelect['select'] = $columns;
|
||||
|
||||
return new static();
|
||||
@ -384,7 +400,8 @@ class Model {
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function from(array $tables) : static {
|
||||
public static function from(array $tables): static
|
||||
{
|
||||
static::$querySelect['from'] = join(', ', $tables);
|
||||
|
||||
return new static();
|
||||
@ -408,8 +425,19 @@ class Model {
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function where(string $column, string $operatorOrValue, string $value=null, bool $no_filter = false) : static {
|
||||
return static::and($column, $operatorOrValue, $value, $no_filter);
|
||||
public static function where(
|
||||
string $column,
|
||||
string $operatorOrValue,
|
||||
string $value = null,
|
||||
bool $no_filter = false
|
||||
): static
|
||||
{
|
||||
return static::and(
|
||||
$column,
|
||||
$operatorOrValue,
|
||||
$value,
|
||||
$no_filter
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -430,7 +458,13 @@ class Model {
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function and(string $column, string $operatorOrValue, string $value=null, bool $no_filter = false) : static {
|
||||
public static function and(
|
||||
string $column,
|
||||
string $operatorOrValue,
|
||||
string $value = null,
|
||||
bool $no_filter = false
|
||||
): static
|
||||
{
|
||||
if (is_null($value)) {
|
||||
$value = $operatorOrValue;
|
||||
$operatorOrValue = '=';
|
||||
@ -465,7 +499,13 @@ class Model {
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function or(string $column, string $operatorOrValue, string $value=null, bool $no_filter = false) : static {
|
||||
public static function or(
|
||||
string $column,
|
||||
string $operatorOrValue,
|
||||
string $value = null,
|
||||
bool $no_filter = false
|
||||
): static
|
||||
{
|
||||
if (is_null($value)) {
|
||||
$value = $operatorOrValue;
|
||||
$operatorOrValue = '=';
|
||||
@ -496,7 +536,12 @@ class Model {
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function where_in(string $column, array $arr, bool $in = true) : static {
|
||||
public static function where_in(
|
||||
string $column,
|
||||
array $arr,
|
||||
bool $in = true
|
||||
): static
|
||||
{
|
||||
$arrIn = [];
|
||||
foreach($arr as $value) {
|
||||
$arrIn[] = static::bindValue($value);
|
||||
@ -527,7 +572,13 @@ class Model {
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function leftJoin(string $table, string $columnA, string $operatorOrColumnB, string $columnB = null) : static {
|
||||
public static function leftJoin(
|
||||
string $table,
|
||||
string $columnA,
|
||||
string $operatorOrColumnB,
|
||||
string $columnB = null
|
||||
): static
|
||||
{
|
||||
if (is_null($columnB)) {
|
||||
$columnB = $operatorOrColumnB;
|
||||
$operatorOrColumnB = '=';
|
||||
@ -555,7 +606,13 @@ class Model {
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function rightJoin(string $table, string $columnA, string $operatorOrColumnB, string $columnB = null) : static {
|
||||
public static function rightJoin(
|
||||
string $table,
|
||||
string $columnA,
|
||||
string $operatorOrColumnB,
|
||||
string $columnB = null
|
||||
): static
|
||||
{
|
||||
if (is_null($columnB)) {
|
||||
$columnB = $operatorOrColumnB;
|
||||
$operatorOrColumnB = '=';
|
||||
@ -583,7 +640,13 @@ class Model {
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function innerJoin(string $table, string $columnA, string $operatorOrColumnB, string $columnB = null) : static {
|
||||
public static function innerJoin(
|
||||
string $table,
|
||||
string $columnA,
|
||||
string $operatorOrColumnB,
|
||||
string $columnB = null
|
||||
): static
|
||||
{
|
||||
if (is_null($columnB)) {
|
||||
$columnB = $operatorOrColumnB;
|
||||
$operatorOrColumnB = '=';
|
||||
@ -602,7 +665,8 @@ class Model {
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function groupBy(array $arr) : static {
|
||||
public static function groupBy(array $arr): static
|
||||
{
|
||||
static::$querySelect['groupBy'] = join(', ', $arr);
|
||||
return new static();
|
||||
}
|
||||
@ -618,7 +682,8 @@ class Model {
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function limit(int $offsetOrQuantity, ?int $quantity = null) : static {
|
||||
public static function limit(int $offsetOrQuantity, ?int $quantity = null): static
|
||||
{
|
||||
if (is_null($quantity))
|
||||
static::$querySelect['limit'] = $offsetOrQuantity;
|
||||
else
|
||||
@ -639,7 +704,8 @@ class Model {
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function orderBy(string $value, string $order = 'ASC') : static {
|
||||
public static function orderBy(string $value, string $order = 'ASC'): static
|
||||
{
|
||||
if ($value == "RAND") {
|
||||
static::$querySelect['orderBy'] = 'RAND()';
|
||||
return new static();
|
||||
@ -665,7 +731,8 @@ class Model {
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function count(bool $resetQuery = true, bool $useLimit = false) : int {
|
||||
public static function count(bool $resetQuery = true, bool $useLimit = false): int
|
||||
{
|
||||
if (!$resetQuery)
|
||||
$backup = [
|
||||
'select' => static::$querySelect['select'],
|
||||
@ -707,7 +774,8 @@ class Model {
|
||||
*
|
||||
* @return static|null
|
||||
*/
|
||||
public static function getById(mixed $id): ?static {
|
||||
public static function getById(mixed $id): ?static
|
||||
{
|
||||
return static::where(static::$primaryKey, $id)->getFirst();
|
||||
}
|
||||
|
||||
@ -722,7 +790,8 @@ class Model {
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function search(string $search, array $in = null) : static {
|
||||
public static function search(string $search, array $in = null): static
|
||||
{
|
||||
if ($in == null) {
|
||||
$className = get_called_class();
|
||||
$in = array_keys((new $className())->getVars());
|
||||
@ -756,9 +825,10 @@ class Model {
|
||||
* (opcional) Indica si el query debe reiniciarse o no (por defecto es true).
|
||||
*
|
||||
* @return array
|
||||
* Contiene un arreglo de instancias de la clase actual.
|
||||
* Arreglo con instancias del la clase actual resultantes del query.
|
||||
*/
|
||||
public static function get(bool $resetQuery = true) : array { // Devuelve array vacío si no encuentra nada.
|
||||
public static function get(bool $resetQuery = true): array
|
||||
{
|
||||
$sql = static::buildQuery();
|
||||
$result = static::query($sql, $resetQuery);
|
||||
|
||||
@ -780,7 +850,8 @@ class Model {
|
||||
* @return static|null
|
||||
* Puede retornar un objeto static o null.
|
||||
*/
|
||||
public static function getFirst(bool $resetQuery = true): ?static { // Devuelve null si no encuentra nada.
|
||||
public static function getFirst(bool $resetQuery = true): ?static
|
||||
{
|
||||
static::limit(1);
|
||||
$instances = static::get($resetQuery);
|
||||
return empty($instances) ? null : $instances[0];
|
||||
@ -792,7 +863,8 @@ class Model {
|
||||
* @return array
|
||||
* Contiene un arreglo de instancias de la clase actual.
|
||||
*/
|
||||
public static function all() : array {
|
||||
public static function all(): array
|
||||
{
|
||||
$sql = 'SELECT * FROM '.static::table();
|
||||
$result = static::query($sql);
|
||||
|
||||
@ -813,7 +885,8 @@ class Model {
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setNull(string|array $atts): void {
|
||||
public function setNull(string|array $atts): void
|
||||
{
|
||||
if (is_array($atts)) {
|
||||
foreach ($atts as $att)
|
||||
if (!in_array($att, $this->toNull))
|
||||
|
Reference in New Issue
Block a user