Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 53 additions & 6 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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');
Expand Down
14 changes: 11 additions & 3 deletions src/Validation/Rules/NullableRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 4 additions & 0 deletions src/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading