diff --git a/app/Filtros/Filtro.php b/app/Filtros/Filtro.php index 737f27b..b78e038 100644 --- a/app/Filtros/Filtro.php +++ b/app/Filtros/Filtro.php @@ -46,17 +46,24 @@ class Filtro extends Model //Obtener nombre del método (snake_case a camelCase) $metodo = str_replace('_', '', lcfirst(ucwords($filtro, '_'))); - if(!method_exists($this, $metodo)) { continue; } + if (!method_exists($this, $metodo)) + continue; //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 try { $this->$metodo($valor); - } catch (Throwable $th) { - if (is_a($th,'TypeError') ) { throw new HttpException(400, sprintf($this->MENSAJES_ERROR['ARGUMENTO'],$filtro)); } - throw $th; + } catch (Throwable $error) { + if (is_a($error,'TypeError')) { + $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 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') { - if(!in_array($order,['asc','desc'])) { throw new TypeError(); } + if (!in_array($order,['asc','desc'])) + throw new TypeError(); + $this->builder->orderBy('nombre', $order); } }