Skip to content

Commit 934c532

Browse files
committed
feat: update request object
1 parent 2ab3671 commit 934c532

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

src/Request.php

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,13 @@ public static function hasHeader(String $header): bool
6767
*/
6868
public static function isAjax(): bool
6969
{
70-
if (static::params('isajax')) return true;
70+
if (static::params('isajax')) {
71+
return true;
72+
}
7173

72-
if (Headers::get('X_REQUESTED_WITH') && Headers::get('X_REQUESTED_WITH') === 'XMLHttpRequest') return true;
74+
if (Headers::get('X_REQUESTED_WITH') && Headers::get('X_REQUESTED_WITH') === 'XMLHttpRequest') {
75+
return true;
76+
}
7377

7478
return false;
7579
}
@@ -85,7 +89,7 @@ public static function isXhr(): bool
8589

8690
/**
8791
* Access stream that allows you to read raw data from the request body. **This is not for form data**
88-
*
92+
*
8993
* @param boolean $safeData Sanitize data?
9094
*/
9195
public static function input($safeData = true)
@@ -102,10 +106,15 @@ public static function input($safeData = true)
102106
$data[$param[0]] = $param[1];
103107
}
104108
} else {
105-
if (!$data) $data = json_encode([]);
109+
if (!$data) {
110+
$data = json_encode([]);
111+
}
106112

107113
$parsedData = json_decode($data, true);
108-
if (is_array($parsedData)) $data = $parsedData;
114+
if (is_array($parsedData)) {
115+
$data = $parsedData;
116+
}
117+
109118
}
110119

111120
return $safeData ? \Leaf\Anchor::sanitize($data) : $data;
@@ -127,7 +136,9 @@ public static function params(string $key = null, mixed $default = null): mixed
127136
{
128137
$union = static::body();
129138

130-
if ($key) return $union[$key] ?? $default;
139+
if ($key) {
140+
return $union[$key] ?? $default;
141+
}
131142

132143
return $union;
133144
}
@@ -144,8 +155,7 @@ public static function params(string $key = null, mixed $default = null): mixed
144155
* @param bool $safeData Sanitize output?
145156
* @param bool $noEmptyString Remove empty strings from return data?
146157
*/
147-
public static function try(array $params, bool $safeData = true, bool $noEmptyString = false)
148-
{
158+
function try (array $params, bool $safeData = true, bool $noEmptyString = false) {
149159
$data = static::get($params, $safeData);
150160
$dataKeys = array_keys($data);
151161

@@ -165,22 +175,18 @@ public static function try(array $params, bool $safeData = true, bool $noEmptySt
165175

166176
/**
167177
* Get raw request data
168-
*
178+
*
169179
* @param string|array $item The items to output
170180
* @param mixed $default The default value to return if no data is available
171181
*/
172182
public static function rawData($item = null, $default = null)
173183
{
174-
$handler = fopen('php://input', 'r');
175-
$decoded = json_decode(stream_get_contents($handler), true);
176-
$data = is_array($decoded) ? $decoded : [];
177-
178-
return \Leaf\Anchor::deepGet($data, $item) ?? $default;
184+
return \Leaf\Anchor::deepGet(static::input(false), $item) ?? $default;
179185
}
180186

181187
/**
182188
* Return only get request data
183-
*
189+
*
184190
* @param string|array $item The items to output
185191
* @param mixed $default The default value to return if no data is available
186192
*/
@@ -191,7 +197,7 @@ public static function urlData($item = null, $default = null)
191197

192198
/**
193199
* Return only get request data
194-
*
200+
*
195201
* @param string|array $item The items to output
196202
* @param mixed $default The default value to return if no data is available
197203
*/
@@ -212,7 +218,9 @@ public static function postData($item = null, $default = null)
212218
*/
213219
public static function get($params, bool $safeData = true)
214220
{
215-
if (is_string($params)) return static::body($safeData)[$params] ?? null;
221+
if (is_string($params)) {
222+
return static::body($safeData)[$params] ?? null;
223+
}
216224

217225
$data = [];
218226

@@ -230,11 +238,11 @@ public static function get($params, bool $safeData = true)
230238
*/
231239
public static function body(bool $safeData = true)
232240
{
233-
$finalData = array_merge($_GET, $_FILES, $_POST, static::input($safeData));
241+
$finalData = array_merge(static::urlData(), $_FILES, static::postData(), static::input($safeData));
234242

235243
return $safeData ?
236-
\Leaf\Anchor::sanitize($finalData) :
237-
$finalData;
244+
\Leaf\Anchor::sanitize($finalData) :
245+
$finalData;
238246
}
239247

240248
/**
@@ -244,8 +252,13 @@ public static function body(bool $safeData = true)
244252
*/
245253
public static function files($filenames = null)
246254
{
247-
if ($filenames == null) return $_FILES;
248-
if (is_string($filenames)) return $_FILES[$filenames] ?? null;
255+
if ($filenames == null) {
256+
return $_FILES;
257+
}
258+
259+
if (is_string($filenames)) {
260+
return $_FILES[$filenames] ?? null;
261+
}
249262

250263
$files = [];
251264
foreach ($filenames as $filename) {
@@ -266,8 +279,8 @@ public static function files($filenames = null)
266279
public static function cookies(string $key = null)
267280
{
268281
return $key === null ?
269-
Cookie::all() :
270-
Cookie::get($key);
282+
Cookie::all() :
283+
Cookie::get($key);
271284
}
272285

273286
/**
@@ -295,8 +308,8 @@ public static function isFormData(): bool
295308
public static function headers($key = null, bool $safeData = true)
296309
{
297310
return ($key === null) ?
298-
Headers::all($safeData) :
299-
Headers::get($key, $safeData);
311+
Headers::all($safeData) :
312+
Headers::get($key, $safeData);
300313
}
301314

302315
/**

0 commit comments

Comments
 (0)