Skip to content

Commit 9286329

Browse files
authored
Merge pull request #375 from bowphp/refactor/code-base
Add query and post method to request and fix nullable validator
2 parents 2f0f2d0 + 7c39892 commit 9286329

3 files changed

Lines changed: 68 additions & 9 deletions

File tree

src/Http/Request.php

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ class Request
2929
*/
3030
private array $input = [];
3131

32+
/**
33+
* Define the post variables
34+
*
35+
* @var array
36+
*/
37+
private array $post = [];
38+
39+
/**
40+
* Define the query variables
41+
*
42+
* @var array
43+
*/
44+
private array $query = [];
45+
3246
/**
3347
* Define the bags instance
3448
*
@@ -91,19 +105,47 @@ public function capture()
91105
}
92106
}
93107

94-
$this->input = array_merge((array)$data, $_GET);
108+
$this->post = $data;
109+
$this->query = $_GET;
110+
$this->input = array_merge((array) $data, $this->query);
95111

96112
foreach ($this->input as $key => $value) {
97-
if (is_string($value) && strlen($value) == 0) {
98-
$value = null;
99-
}
100-
101-
$this->input[$key] = $value;
113+
$this->input[$key] = is_string($value) && strlen($value) == 0 ? null : $value;
102114
}
103115

104116
$this->capture = true;
105117
}
106118

119+
/**
120+
* Retrieve query variables
121+
*
122+
* @param string|null $key
123+
* @return array
124+
*/
125+
public function query(?string $key = null): array
126+
{
127+
if ($key === null) {
128+
return $this->query;
129+
}
130+
131+
return $this->query[$key] ?? [];
132+
}
133+
134+
/**
135+
* Get posted data
136+
*
137+
* @param string|null $key
138+
* @return array
139+
*/
140+
public function post(?string $key = null): array
141+
{
142+
if ($key === null) {
143+
return $this->post;
144+
}
145+
146+
return $this->post[$key] ?? [];
147+
}
148+
107149
/**
108150
* Get Request header
109151
*
@@ -430,6 +472,11 @@ public function isAjax(): bool
430472
return $content_type && str_contains($content_type, "application/json");
431473
}
432474

475+
/**
476+
* Determine if is accept application/json
477+
*
478+
* @return boolean
479+
*/
433480
public function wantsJson(): bool
434481
{
435482
$accept = $this->getHeader('accept');

src/Validation/Rules/NullableRule.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,20 @@ trait NullableRule
1515
*
1616
* @param string $key
1717
* @param string $masque
18-
* @return void
18+
* @return bool
1919
*/
20-
protected function compileNullable(string $key, string $masque): void
20+
protected function compileNullable(string $key, string $masque): bool
2121
{
2222
if (!preg_match("/^nullable$/", $masque, $match)) {
23-
return;
23+
return false;
2424
}
25+
26+
if (isset($this->inputs[$key]) && !Str::isEmpty($this->inputs[$key])) {
27+
return false;
28+
}
29+
30+
$this->inputs[$key] = null;
31+
32+
return true;
2533
}
2634
}

src/Validation/Validator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ private function checkRule(string $rule, string $field): void
176176
continue;
177177
}
178178

179+
if ($masque == "nullable" && $this->compileNullable($field, $masque)) {
180+
break;
181+
}
182+
179183
// Mask on the required rule
180184
foreach ($this->rules as $rule) {
181185
$this->{'compile' . $rule}($field, $masque);

0 commit comments

Comments
 (0)