This commit is contained in:
Alejandro Tasistro 2025-06-19 23:37:44 -03:00
parent 6627118dce
commit 639f6cff77

View file

@ -46,17 +46,24 @@ class Filtro extends Model
//Obtener nombre del método (snake_case a camelCase) //Obtener nombre del método (snake_case a camelCase)
$metodo = str_replace('_', '', lcfirst(ucwords($filtro, '_'))); $metodo = str_replace('_', '', lcfirst(ucwords($filtro, '_')));
if(!method_exists($this, $metodo)) { continue; } if (!method_exists($this, $metodo))
continue;
//Llamar métodos sin argumentos //Llamar métodos sin argumentos
if ($valor === null|| (is_a($valor,'String') && trim($valor)=='')){ $this->$metodo(); continue; } if ($valor === null || (is_a($valor,'String') && trim($valor)=='')) {
$this->$metodo();
continue;
}
//Llamar métodos con argumentos //Llamar métodos con argumentos
try { try {
$this->$metodo($valor); $this->$metodo($valor);
} catch (Throwable $th) { } catch (Throwable $error) {
if (is_a($th,'TypeError') ) { throw new HttpException(400, sprintf($this->MENSAJES_ERROR['ARGUMENTO'],$filtro)); } if (is_a($error,'TypeError')) {
throw $th; $mensaje = sprintf($this->MENSAJES_ERROR['ARGUMENTO'], $filtro);
throw new HttpException(400, $mensaje);
}
throw $error;
} }
} }
@ -66,12 +73,16 @@ class Filtro extends Model
//Buscar un término en el nombre //Buscar un término en el nombre
public function nombre(String $valor) public function nombre(String $valor)
{ {
$this->builder->where('nombre', "LIKE", "%" . $valor . "%")->orderByRaw("IF(nombre = '$valor',2,IF(nombre LIKE '$valor%',1,0)) DESC"); $this->builder
->where('nombre', "LIKE", "%" . $valor . "%")
->orderByRaw("IF(nombre = '$valor',2,IF(nombre LIKE '$valor%',1,0)) DESC");
} }
public function alfabetico(String $order = 'asc') public function alfabetico(String $order = 'asc')
{ {
if(!in_array($order,['asc','desc'])) { throw new TypeError(); } if (!in_array($order,['asc','desc']))
throw new TypeError();
$this->builder->orderBy('nombre', $order); $this->builder->orderBy('nombre', $order);
} }
} }