Refactor request library.

This commit is contained in:
KJ 2024-09-08 14:43:56 -04:00
parent 3d2a607768
commit 7baad428ec
2 changed files with 20 additions and 7 deletions

View File

@ -53,11 +53,24 @@ class Request extends Neuron {
}
/**
* Inicia la validación que se haya configurado.
* Corre las validaciones e intenta continuar con la pila de callbacks.
*
* @return mixed
*/
public function validate(): mixed
public function handle(): mixed
{
if ($this->validate())
return Middleware::next($this);
return null;
}
/**
* Inicia la validación que se haya configurado.
*
* @return bool
*/
public function validate(): bool
{
$actual = match($_SERVER['REQUEST_METHOD']) {
'GET', 'DELETE' => $this->get,
@ -67,7 +80,7 @@ class Request extends Neuron {
if (Validator::validateList(static::paramRules(), $this->params) &&
Validator::validateList(static::getRules(), $this->get ) &&
Validator::validateList(static::rules(), $actual))
return Middleware::next($this);
return true;
if (isset(static::messages()[Validator::$lastFailed]))
$error = static::messages()[Validator::$lastFailed];
@ -120,12 +133,12 @@ class Request extends Neuron {
*
* @param string $error
*
* @return mixed
* @return false
*/
protected function onInvalid(string $error): mixed
protected function onInvalid(string $error): false
{
http_response_code(422);
print($error);
return null;
return false;
}
}

View File

@ -356,7 +356,7 @@ class Router {
// Llama a la validación y luego procesa la cola de callbacks
$request->next = $router['callback'];
$data = $request->validate();
$data = $request->handle();
// Por defecto imprime como JSON si se retorna algo
if (isset($data)) {