-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRequest.php
61 lines (49 loc) · 1.41 KB
/
Request.php
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
<?php
final class Request
{
public function __construct(
private readonly string $uri,
private readonly string $method,
private readonly array $queryParams
)
{
}
public static function createFromGlobal(): self
{
/** Add # to start of the string to replace only beginning of the URI */
$scriptName = '#' . $_SERVER['SCRIPT_NAME'];
$requestUri = '#' . $_SERVER['REQUEST_URI'];
$baseDir = dirname($scriptName);
$queryString = $_SERVER['QUERY_STRING'];
$requestMethod = $_SERVER['REQUEST_METHOD'];
$queryParams = [];
parse_str($queryString, $queryParams);
$url = '/' . str_replace([$scriptName, $baseDir, '?' . $queryString], '', $requestUri);
$url = str_replace('//', '/', $url);
return new Request($url, $requestMethod, $queryParams);
}
/**
* @return string
*/
public function getUri(): string
{
return $this->uri;
}
/**
* @return string
*/
public function getMethod(): string
{
return $this->method;
}
public function get(string $name, mixed $default = null): mixed
{
if (isset($this->queryParams[$name])) {
return $this->queryParams[$name];
}
if (filter_has_var(INPUT_POST, $name)) {
return filter_input(INPUT_POST, $name);
}
return $default;
}
}