1 | <?php |
||
13 | class Request |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * @var string Request method |
||
18 | */ |
||
19 | protected $method; |
||
20 | |||
21 | /** |
||
22 | * @var string Full URL |
||
23 | */ |
||
24 | protected $url; |
||
25 | |||
26 | /** |
||
27 | * @var array Request headers |
||
28 | */ |
||
29 | protected $headers; |
||
30 | |||
31 | /** |
||
32 | * @var Cookie Cookie |
||
33 | */ |
||
34 | protected $cookie; |
||
35 | |||
36 | /** |
||
37 | * @var array POST data |
||
38 | */ |
||
39 | public $post; |
||
40 | |||
41 | /** |
||
42 | * Request constructor. |
||
43 | * |
||
44 | * @param array $server |
||
45 | * @param array $post |
||
46 | * @param Cookie $cookie |
||
47 | */ |
||
48 | 1 | public function __construct(Cookie $cookie, array $server = [], array $post = []) |
|
49 | { |
||
50 | 1 | $this->method = !empty($server['REQUEST_METHOD']) ? $server['REQUEST_METHOD'] : 'GET'; |
|
51 | 1 | $this->url = $this->createUrlFromServerArray($server, !empty($server['HTTPS'])); |
|
52 | 1 | $this->headers = $this->parseHeaders($server); |
|
53 | 1 | $this->cookie = $cookie; |
|
54 | 1 | $this->post = $post; |
|
55 | 1 | } |
|
56 | |||
57 | 2 | public function getMethod(): string |
|
58 | { |
||
59 | 2 | return $this->method; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * Get request URL path |
||
64 | * |
||
65 | * @return string URL path |
||
66 | */ |
||
67 | 3 | public function getPath(): string |
|
68 | { |
||
69 | 3 | $path = parse_url($this->url, PHP_URL_PATH); |
|
70 | |||
71 | 3 | if (is_string($path) === false) { |
|
72 | 2 | $path = ''; |
|
73 | } |
||
74 | |||
75 | 3 | return $path; |
|
76 | } |
||
77 | |||
78 | 2 | public function getAcceptHeader(): string |
|
79 | { |
||
80 | 2 | return $this->headers['Accept']; |
|
81 | } |
||
82 | |||
83 | 2 | public function getAuthorizationHeader(): string |
|
84 | { |
||
85 | 2 | return $this->headers['Authorization']; |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * Get cookie |
||
90 | * |
||
91 | * @return Cookie |
||
92 | */ |
||
93 | 2 | public function getCookie(): Cookie |
|
97 | |||
98 | /** |
||
99 | * Get request POST data |
||
100 | * |
||
101 | * @return array POST data |
||
102 | */ |
||
103 | 2 | public function getPost(): array |
|
107 | |||
108 | /** |
||
109 | * Create URL from _SERVER array |
||
110 | * |
||
111 | * @param array $server _SERVER array |
||
112 | * @param bool $secure |
||
113 | * |
||
114 | * @return string URL |
||
115 | */ |
||
116 | 1 | protected function createUrlFromServerArray(array $server, bool $secure = true): string |
|
126 | |||
127 | 1 | protected function parseHeaders(array $server): array |
|
128 | { |
||
129 | $headers = [ |
||
146 | } |
||
147 |