BREAKING CHANGE: Adhere to PSR-12 coding standards.

- Model: where_in method was renamed as whereIn.
This commit is contained in:
kj
2025-09-07 11:07:07 -03:00
parent 0f46848d15
commit c9f467345b
10 changed files with 480 additions and 401 deletions

View File

@@ -1,4 +1,7 @@
<?php
namespace Libs;
/**
* Request - DuckBrain
*
@@ -9,10 +12,8 @@
* @website https://kj2.me
* @licence MIT
*/
namespace Libs;
class Request extends Neuron {
class Request extends Neuron
{
public Neuron $get;
public Neuron $post;
public Neuron $put;
@@ -23,12 +24,12 @@ class Request extends Neuron {
public string $path;
public string $error;
public string $body;
public array $next;
public array $next;
/**
* __construct
*
* @param string $path Ruta actual tomando como raíz la instalación de DuckBrain.
* @param string $path Ruta actual tomando como raíz la instalación de DuckBrain.
*/
public function __construct()
{
@@ -41,14 +42,19 @@ class Request extends Neuron {
$this->body = file_get_contents("php://input");
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
if ($contentType === "application/json")
if ($contentType === "application/json") {
$this->json = new Neuron(
(object) json_decode(trim($this->body), false)
);
else {
} else {
$this->json = new Neuron();
if (in_array($_SERVER['REQUEST_METHOD'], ['PUT', 'PATCH', 'DELETE']) &&
preg_match('/^[^;?\/:@&=+$,]{1,255}[=]/', $this->body, $matches)) { // Con la expresión regular verificamos que sea un http query string válido y evitamos errores de memoria en caso de que el body tenga algo más grande que eso.
if (
in_array($_SERVER['REQUEST_METHOD'], ['PUT', 'PATCH', 'DELETE']) &&
preg_match('/^[^;?\/:@&=+$,]{1,255}[=]/', $this->body, $matches)
) {
// Con la expresión regular verificamos que sea un http
// query string válido y evitamos errores de memoria en caso
// de que el body tenga algo más grande que eso.
parse_str($this->body, $input_vars);
$this->{strtolower($_SERVER['REQUEST_METHOD'])} = new Neuron($input_vars);
}
@@ -64,8 +70,9 @@ class Request extends Neuron {
*/
public function handle(): mixed
{
if ($this->validate())
if ($this->validate()) {
return Middleware::next($this);
}
return null;
}
@@ -77,21 +84,23 @@ class Request extends Neuron {
*/
public function validate(): bool
{
$actual = match($_SERVER['REQUEST_METHOD']) {
$actual = match ($_SERVER['REQUEST_METHOD']) {
'POST', 'PUT', 'PATCH', 'DELETE' => $this->{strtolower($_SERVER['REQUEST_METHOD'])},
default => $this->get
};
if (Validator::validateList(static::paramRules(), $this->params) &&
Validator::validateList(static::getRules(), $this->get ) &&
Validator::validateList(static::rules(), $actual))
if (
Validator::validateList(static::paramRules(), $this->params) &&
Validator::validateList(static::getRules(), $this->get) &&
Validator::validateList(static::rules(), $actual)
) {
return true;
}
if (isset(static::messages()[Validator::$lastFailed]))
if (isset(static::messages()[Validator::$lastFailed])) {
$error = static::messages()[Validator::$lastFailed];
else {
$error = 'Error: validation failed of '.preg_replace('/\./', ' as ', Validator::$lastFailed, 1);
} else {
$error = 'Error: validation failed of ' . preg_replace('/\./', ' as ', Validator::$lastFailed, 1);
}
static::onInvalid($error);
@@ -103,7 +112,8 @@ class Request extends Neuron {
*
* @return array
*/
public function rules(): array {
public function rules(): array
{
return [];
}
@@ -112,7 +122,8 @@ class Request extends Neuron {
*
* @return array
*/
public function paramRules(): array {
public function paramRules(): array
{
return [];
}
@@ -121,7 +132,8 @@ class Request extends Neuron {
*
* @return array
*/
public function getRules(): array {
public function getRules(): array
{
return [];
}
@@ -130,7 +142,8 @@ class Request extends Neuron {
*
* @return array
*/
public function messages(): array {
public function messages(): array
{
return [];
}