|
4 | 4 |
|
5 | 5 | class Request |
6 | 6 | { |
7 | | - /** |
8 | | - * Get data from input |
9 | | - */ |
10 | | - public static function input(string $key, $default = null) |
11 | | - { |
12 | | - return $_POST[$key] ?? $_GET[$key] ?? $default; |
13 | | - } |
| 7 | + public readonly array $query; |
| 8 | + public readonly array $data; |
| 9 | + public readonly array $server; |
14 | 10 |
|
15 | | - /** |
16 | | - * Get data from query string |
17 | | - */ |
18 | | - public static function query(string $key, $default = null) |
| 11 | + public function __construct() |
19 | 12 | { |
20 | | - return $_GET[$key] ?? $default; |
| 13 | + $this->query = $_GET; |
| 14 | + $this->data = $_POST; |
| 15 | + $this->server = $_SERVER; |
21 | 16 | } |
22 | 17 |
|
23 | 18 | /** |
24 | | - * Get all data sent to the server |
| 19 | + * Get the request URI path. |
25 | 20 | */ |
26 | | - public static function all(): array |
| 21 | + public function getPath(): string |
27 | 22 | { |
28 | | - return array_merge($_GET, $_POST); |
| 23 | + return trim(parse_url($this->server['REQUEST_URI'], PHP_URL_PATH), '/'); |
29 | 24 | } |
30 | 25 |
|
31 | 26 | /** |
32 | | - * Get the request method |
| 27 | + * Get the request method. |
33 | 28 | */ |
34 | | - public static function method(): string |
| 29 | + public function getMethod(): string |
35 | 30 | { |
36 | | - return strtoupper($_SERVER['REQUEST_METHOD'] ?? 'GET'); |
37 | | - } |
38 | | - |
39 | | - /** |
40 | | - * Get the URI |
41 | | - */ |
42 | | - public static function uri(): string |
43 | | - { |
44 | | - return $_SERVER['REQUEST_URI'] ?? '/'; |
45 | | - } |
46 | | - |
47 | | - /** |
48 | | - * Get the sanitized data |
49 | | - */ |
50 | | - public static function sanitize(string $key, $default = null) |
51 | | - { |
52 | | - $value = self::input($key, $default); |
53 | | - if (is_string($value)) { |
54 | | - return htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); |
55 | | - } |
56 | | - |
57 | | - return $value; |
| 31 | + return $this->server['REQUEST_METHOD']; |
58 | 32 | } |
59 | 33 |
|
60 | 34 | /** |
61 | | - * Check if the request is an Ajax request |
| 35 | + * Get a value from the POST data. |
62 | 36 | */ |
63 | | - public static function isAjax(): bool |
| 37 | + public function get(string $key, $default = null) |
64 | 38 | { |
65 | | - return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && |
66 | | - strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'; |
| 39 | + return $this->data[$key] ?? $default; |
67 | 40 | } |
68 | 41 |
|
69 | 42 | /** |
70 | | - * Get the IP address of the user |
| 43 | + * Get a value from the GET query string. |
71 | 44 | */ |
72 | | - public static function ip(): string |
| 45 | + public function query(string $key, $default = null) |
73 | 46 | { |
74 | | - return $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0'; |
| 47 | + return $this->query[$key] ?? $default; |
75 | 48 | } |
76 | 49 | } |
0 commit comments