diff --git a/src/Http/Request.php b/src/Http/Request.php index 6f6ec861..97813caa 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -29,6 +29,20 @@ class Request */ private array $input = []; + /** + * Define the post variables + * + * @var array + */ + private array $post = []; + + /** + * Define the query variables + * + * @var array + */ + private array $query = []; + /** * Define the bags instance * @@ -91,19 +105,47 @@ public function capture() } } - $this->input = array_merge((array)$data, $_GET); + $this->post = $data; + $this->query = $_GET; + $this->input = array_merge((array) $data, $this->query); foreach ($this->input as $key => $value) { - if (is_string($value) && strlen($value) == 0) { - $value = null; - } - - $this->input[$key] = $value; + $this->input[$key] = is_string($value) && strlen($value) == 0 ? null : $value; } $this->capture = true; } + /** + * Retrieve query variables + * + * @param string|null $key + * @return array + */ + public function query(?string $key = null): array + { + if ($key === null) { + return $this->query; + } + + return $this->query[$key] ?? []; + } + + /** + * Get posted data + * + * @param string|null $key + * @return array + */ + public function post(?string $key = null): array + { + if ($key === null) { + return $this->post; + } + + return $this->post[$key] ?? []; + } + /** * Get Request header * @@ -430,6 +472,11 @@ public function isAjax(): bool return $content_type && str_contains($content_type, "application/json"); } + /** + * Determine if is accept application/json + * + * @return boolean + */ public function wantsJson(): bool { $accept = $this->getHeader('accept'); diff --git a/src/Validation/Rules/NullableRule.php b/src/Validation/Rules/NullableRule.php index 514dbfa1..7b9ced40 100644 --- a/src/Validation/Rules/NullableRule.php +++ b/src/Validation/Rules/NullableRule.php @@ -15,12 +15,20 @@ trait NullableRule * * @param string $key * @param string $masque - * @return void + * @return bool */ - protected function compileNullable(string $key, string $masque): void + protected function compileNullable(string $key, string $masque): bool { if (!preg_match("/^nullable$/", $masque, $match)) { - return; + return false; } + + if (isset($this->inputs[$key]) && !Str::isEmpty($this->inputs[$key])) { + return false; + } + + $this->inputs[$key] = null; + + return true; } } diff --git a/src/Validation/Validator.php b/src/Validation/Validator.php index 57876a1a..687acdb5 100644 --- a/src/Validation/Validator.php +++ b/src/Validation/Validator.php @@ -176,6 +176,10 @@ private function checkRule(string $rule, string $field): void continue; } + if ($masque == "nullable" && $this->compileNullable($field, $masque)) { + break; + } + // Mask on the required rule foreach ($this->rules as $rule) { $this->{'compile' . $rule}($field, $masque);