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