Add validation on Request.
This commit is contained in:
@ -13,35 +13,22 @@
|
||||
namespace Libs;
|
||||
|
||||
class Request extends Neuron {
|
||||
/**
|
||||
* @var Neuron $get Objeto con todos los valores de $_GET.
|
||||
*/
|
||||
public Neuron $get;
|
||||
/**
|
||||
* @var Neuron $post Objeto con todos los valores de $_POST.
|
||||
*/
|
||||
public Neuron $post;
|
||||
/**
|
||||
* @var Neuron $json Objeto con todos los valores json enviados.
|
||||
*/
|
||||
public Neuron $json;
|
||||
/**
|
||||
* @var mixed $params Objeto con todos los valores pseudovariables de la uri.
|
||||
*/
|
||||
public Neuron $params;
|
||||
/**
|
||||
* @var mixed $path Ruta actual tomando como raíz la instalación de DuckBrain.
|
||||
*/
|
||||
public string $path;
|
||||
public string $error;
|
||||
public array $next;
|
||||
|
||||
/**
|
||||
* __construct
|
||||
*
|
||||
* @param string $path Ruta actual tomando como raíz la instalación de DuckBrain.
|
||||
*/
|
||||
public function __construct(string $path = '/')
|
||||
public function __construct()
|
||||
{
|
||||
$this->path = $path;
|
||||
$this->path = Router::currentPath();
|
||||
$this->get = new Neuron($_GET);
|
||||
$this->post = new Neuron($_POST);
|
||||
|
||||
@ -55,4 +42,80 @@ class Request extends Neuron {
|
||||
|
||||
$this->params = new Neuron();
|
||||
}
|
||||
|
||||
/**
|
||||
* Inicia la validación que se haya configurado.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function validate(): mixed
|
||||
{
|
||||
if ($_SERVER['REQUEST_METHOD'] == 'GET')
|
||||
$actual = $this->get;
|
||||
else
|
||||
$actual = $this->post;
|
||||
|
||||
if (Validator::validateList(static::paramRules(), $this->params) &&
|
||||
Validator::validateList(static::getRules(), $this->get ) &&
|
||||
Validator::validateList(static::rules(), $actual))
|
||||
return Middleware::next($this);
|
||||
|
||||
if (isset(static::messages()[Validator::$lastFailed]))
|
||||
$error = static::messages()[Validator::$lastFailed];
|
||||
else {
|
||||
|
||||
$error = 'Error: validation failed of '.preg_replace('/\./', ' as ', Validator::$lastFailed, 1);
|
||||
}
|
||||
|
||||
return static::onInvalid($error);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reglas para el método actual.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function rules(): array {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Reglas para los parámetros por URL.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function paramRules(): array {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Reglas para los parámetros GET.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getRules(): array {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Mensajes de error en caso de fallar una validación.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function messages(): array {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Función a ejecutar cuando se ha detectado un valor no válido.
|
||||
*
|
||||
* @param string $error
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function onInvalid(string $error): mixed
|
||||
{
|
||||
print($error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user