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+ }
0 commit comments