|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gacela\Router\Entities; |
|
6
|
|
|
|
|
7
|
|
|
final class Request |
|
8
|
|
|
{ |
|
9
|
|
|
public const METHOD_CONNECT = 'CONNECT'; |
|
10
|
|
|
public const METHOD_DELETE = 'DELETE'; |
|
11
|
|
|
public const METHOD_GET = 'GET'; |
|
12
|
|
|
public const METHOD_HEAD = 'HEAD'; |
|
13
|
|
|
public const METHOD_OPTIONS = 'OPTIONS'; |
|
14
|
|
|
public const METHOD_PATCH = 'PATCH'; |
|
15
|
|
|
public const METHOD_POST = 'POST'; |
|
16
|
|
|
public const METHOD_PUT = 'PUT'; |
|
17
|
|
|
public const METHOD_TRACE = 'TRACE'; |
|
18
|
|
|
|
|
19
|
|
|
public const ALL_METHODS = [ |
|
20
|
|
|
self::METHOD_CONNECT, |
|
21
|
|
|
self::METHOD_DELETE, |
|
22
|
|
|
self::METHOD_GET, |
|
23
|
|
|
self::METHOD_HEAD, |
|
24
|
|
|
self::METHOD_OPTIONS, |
|
25
|
|
|
self::METHOD_PATCH, |
|
26
|
|
|
self::METHOD_POST, |
|
27
|
|
|
self::METHOD_PUT, |
|
28
|
|
|
self::METHOD_TRACE, |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
112 |
|
private function __construct( |
|
32
|
|
|
private array $query, |
|
33
|
|
|
private array $request, |
|
34
|
|
|
private array $server, |
|
35
|
|
|
) { |
|
36
|
112 |
|
} |
|
37
|
|
|
|
|
38
|
112 |
|
public static function fromGlobals(): self |
|
39
|
|
|
{ |
|
40
|
112 |
|
return new self($_GET, $_POST, $_SERVER); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
102 |
|
public function isMethod(string $method): bool |
|
44
|
|
|
{ |
|
45
|
|
|
/** @var string $requestMethod */ |
|
46
|
102 |
|
$requestMethod = $this->server['REQUEST_METHOD']; |
|
47
|
|
|
|
|
48
|
102 |
|
return $requestMethod === $method; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
97 |
|
public function path(): string |
|
52
|
|
|
{ |
|
53
|
|
|
/** @var string $requestUri */ |
|
54
|
97 |
|
$requestUri = $this->server['REQUEST_URI']; |
|
55
|
|
|
/** @var string $parsedUrl */ |
|
56
|
97 |
|
$parsedUrl = parse_url($requestUri, PHP_URL_PATH); |
|
57
|
|
|
|
|
58
|
97 |
|
return $parsedUrl; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
public function get(string $key, mixed $default = null): mixed |
|
62
|
|
|
{ |
|
63
|
|
|
/** @var mixed $result */ |
|
64
|
2 |
|
$result = $this->request[$key] ?? $this->query[$key] ?? null; |
|
65
|
|
|
|
|
66
|
2 |
|
if ($result !== null) { |
|
67
|
2 |
|
return $result; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $default; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|