Skip to content

Commit b40beb8

Browse files
authored
Merge pull request #8 from MaplePHP/develop
Develop
2 parents b6e8af9 + 425a53f commit b40beb8

File tree

3 files changed

+208
-1
lines changed

3 files changed

+208
-1
lines changed

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,78 @@ if ($response->getStatusCode() === 200) {
266266
echo 'Error: ' . $response->getReasonPhrase();
267267
}
268268
```
269+
---
270+
271+
272+
# Input
273+
274+
A simple, secure static helper class for reading and sanitizing `$_GET` and `$_POST` values in PHP. Part of the [MaplePHP HTTP](https://github.com/MaplePHP/Http) library.
275+
276+
### Checking if a key exists
277+
278+
```php
279+
// Check in either $_GET or $_POST
280+
Input::has('name');
281+
282+
// Check only in $_GET
283+
Input::hasGet('page');
284+
285+
// Check only in $_POST
286+
Input::hasPost('email');
287+
```
288+
289+
---
290+
291+
### Reading encoded (safe) values
292+
293+
Values are automatically HTML-encoded to prevent XSS. Returns `null` if the key does not exist.
294+
295+
```php
296+
// From $_GET
297+
$page = Input::get('page');
298+
299+
// From $_POST
300+
$email = Input::post('email');
301+
302+
// From $_GET or $_POST (GET takes priority)
303+
$id = Input::request('id');
304+
305+
// With a fallback default
306+
$page = Input::get('page', '1');
307+
```
308+
309+
---
310+
311+
### Reading raw (unencoded) values
312+
313+
Use raw methods when you need the original unmodified value, or when working with array inputs.
314+
315+
```php
316+
// Scalar raw value
317+
$name = Input::getRaw('name');
318+
319+
// Array input e.g. $_POST['tags'][]
320+
$tags = Input::postRaw('tags');
321+
322+
// With a fallback default
323+
$filters = Input::getRaw('filters', []);
324+
```
325+
326+
> **Note:** Raw values are not sanitized. Make sure to validate or sanitize them before use.
327+
328+
---
329+
330+
### Reading all input
331+
332+
```php
333+
// All raw merged input from $_GET and $_POST (POST takes priority)
334+
$data = Input::all();
335+
336+
// All encoded merged input, including nested arrays
337+
$data = Input::allEncoded();
338+
```
339+
340+
---
269341

270342
## Conclusion
271343

src/Input.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
namespace MaplePHP\Http;
4+
5+
use MaplePHP\DTO\Format\Arr;
6+
use MaplePHP\DTO\Format\Str;
7+
8+
class Input
9+
{
10+
11+
/**
12+
* Check if key exists in $_GET or $_POST
13+
*
14+
* @param string $key
15+
* @return bool
16+
*/
17+
public static function has(string $key): bool
18+
{
19+
return isset($_GET[$key]) || isset($_POST[$key]);
20+
}
21+
22+
/**
23+
* Check if key exists in $_GET
24+
*
25+
* @param string $key
26+
* @return bool
27+
*/
28+
public static function hasGet(string $key): bool
29+
{
30+
return isset($_GET[$key]);
31+
}
32+
33+
/**
34+
* Check if key exists in $_POST
35+
*
36+
* @param string $key
37+
* @return bool
38+
*/
39+
public static function hasPost(string $key): bool
40+
{
41+
return isset($_POST[$key]);
42+
}
43+
44+
/**
45+
* Get encoded value from $_GET
46+
*
47+
* @param string $key
48+
* @param string|null $default Fallback value if key does not exist
49+
* @param bool $raw Return raw unencoded value
50+
* @return string|null
51+
*/
52+
public static function get(string $key, ?string $default = null, bool $raw = false): ?string
53+
{
54+
$value = $_GET[$key] ?? $default;
55+
if ($value === null) return null;
56+
return $raw ? $value : Str::value($value)->encode();
57+
}
58+
59+
/**
60+
* Get encoded value from $_POST
61+
*
62+
* @param string $key
63+
* @param string|null $default Fallback value if key does not exist
64+
* @param bool $raw Return raw unencoded value
65+
* @return string|null
66+
*/
67+
public static function post(string $key, ?string $default = null, bool $raw = false): ?string
68+
{
69+
$value = $_POST[$key] ?? $default;
70+
if ($value === null) return null;
71+
return $raw ? $value : Str::value($value)->encode();
72+
}
73+
74+
/**
75+
* Get encoded value from $_GET or $_POST (GET takes priority)
76+
*
77+
* @param string $key
78+
* @param string|null $default Fallback value if key does not exist
79+
* @param bool $raw Return raw unencoded value
80+
* @return string|null
81+
*/
82+
public static function request(string $key, ?string $default = null, bool $raw = false): ?string
83+
{
84+
$value = $_GET[$key] ?? $_POST[$key] ?? $default;
85+
if ($value === null) return null;
86+
return $raw ? $value : Str::value($value)->encode();
87+
}
88+
89+
/**
90+
* Get raw unencoded value from $_GET, useful for arrays e.g. $_GET['key'][]
91+
*
92+
* @param string $key
93+
* @param mixed $default Fallback value if key does not exist
94+
* @return mixed
95+
*/
96+
public static function getRaw(string $key, mixed $default = null): mixed
97+
{
98+
return $_GET[$key] ?? $default;
99+
}
100+
101+
/**
102+
* Get raw unencoded value from $_POST, useful for arrays e.g. $_POST['key'][]
103+
*
104+
* @param string $key
105+
* @param mixed $default Fallback value if key does not exist
106+
* @return mixed
107+
*/
108+
public static function postRaw(string $key, mixed $default = null): mixed
109+
{
110+
return $_POST[$key] ?? $default;
111+
}
112+
113+
/**
114+
* Get all raw input from $_GET and $_POST merged (POST takes priority)
115+
*
116+
* @return array<string, mixed>
117+
*/
118+
public static function all(): array
119+
{
120+
return array_merge($_GET, $_POST);
121+
}
122+
123+
/**
124+
* Get all encoded input from $_GET and $_POST merged (POST takes priority)
125+
*
126+
* @return array<string, string>
127+
*/
128+
public static function allEncoded(): array
129+
{
130+
return Arr::value($_GET)
131+
->merge($_POST)
132+
->walkRecursive(fn($value) => Str::value($value)->encode()->get())
133+
->toArray();
134+
}
135+
}

src/Uri.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ public function getUri(): string
296296
*/
297297
public function getArgv(): array
298298
{
299-
return $this->argv;
299+
return $this->argv === null ? [] : $this->argv;
300300
}
301301

302302
/**

0 commit comments

Comments
 (0)