1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Air\HTTP\Request; |
4
|
|
|
|
5
|
|
|
class Request implements RequestInterface |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @param string $uri The request URI. |
9
|
|
|
*/ |
10
|
|
|
protected $uri; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param array $uriComponents An array of components that make up the URI. |
15
|
|
|
*/ |
16
|
|
|
protected $uriComponents; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $method The request method. |
21
|
|
|
*/ |
22
|
|
|
protected $method; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array $requestData An array of request data (from POST, PUT, etc). |
27
|
|
|
*/ |
28
|
|
|
protected $requestData; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array $queryData An array of query data (i.e. a GET request). |
33
|
|
|
*/ |
34
|
|
|
protected $queryData; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array $serverData An array of server data (i.e. HTTP_REFERER, DOC_ROOT etc). |
39
|
|
|
*/ |
40
|
|
|
protected $serverData; |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array $cookies An array of cookies the current request holds |
45
|
|
|
*/ |
46
|
|
|
protected $cookies; |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array $fileData An array of file data. |
51
|
|
|
*/ |
52
|
|
|
protected $fileData; |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Class constructor. |
57
|
|
|
* |
58
|
|
|
* @param string $uri The request URI. |
59
|
|
|
* @param string $method The request method. |
60
|
|
|
* @param array $requestData The request data. |
61
|
|
|
* @param array $queryData The query data. |
62
|
|
|
* @param array $serverData The server data. |
63
|
|
|
* @param array $cookies The cookies for the current request |
64
|
|
|
* @param array $fileData An array of file data. |
65
|
|
|
* @throws \InvalidArgumentException |
66
|
|
|
*/ |
67
|
|
|
public function __construct( |
68
|
|
|
$uri, |
69
|
|
|
$method = self::METHOD_GET, |
70
|
|
|
array $requestData = [], |
71
|
|
|
array $queryData = [], |
72
|
|
|
array $serverData = [], |
73
|
|
|
array $cookies = [], |
74
|
|
|
array $fileData = [] |
75
|
|
|
) { |
76
|
|
|
$this->uri = $uri; |
77
|
|
|
$this->method = $method; |
78
|
|
|
$this->requestData = $requestData; |
79
|
|
|
$this->queryData = $queryData; |
80
|
|
|
$this->serverData = $serverData; |
81
|
|
|
$this->cookies = $cookies; |
82
|
|
|
$this->fileData = $fileData; |
83
|
|
|
|
84
|
|
|
// Parse the URI. |
85
|
|
|
$parsed_uri = parse_url($uri); |
86
|
|
|
|
87
|
|
|
// Ensure the URI is valid, and set it. |
88
|
|
|
if ($parsed_uri) { |
89
|
|
|
$this->uriComponents = $parsed_uri; |
90
|
|
|
} else { |
91
|
|
|
throw new \InvalidArgumentException('The URI provided was malformed.'); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the request method. |
98
|
|
|
* |
99
|
|
|
* @return string The request method. |
100
|
|
|
*/ |
101
|
|
|
public function getMethod() |
102
|
|
|
{ |
103
|
|
|
return $this->method; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the URI. |
109
|
|
|
* |
110
|
|
|
* @return string The URI. |
111
|
|
|
*/ |
112
|
|
|
public function getUri() |
113
|
|
|
{ |
114
|
|
|
return $this->uri; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the URI path. |
120
|
|
|
* |
121
|
|
|
* @return string The URI path. |
122
|
|
|
*/ |
123
|
|
|
public function getUriPath() |
124
|
|
|
{ |
125
|
|
|
return $this->uriComponents['path']; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get the request data. |
131
|
|
|
* |
132
|
|
|
* @return array The request data. |
133
|
|
|
*/ |
134
|
|
|
public function getRequestData() |
135
|
|
|
{ |
136
|
|
|
return $this->requestData; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get the query data. |
142
|
|
|
* |
143
|
|
|
* @return array The query data. |
144
|
|
|
*/ |
145
|
|
|
public function getQueryData() |
146
|
|
|
{ |
147
|
|
|
return $this->queryData; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the server data. |
153
|
|
|
* |
154
|
|
|
* @return array The server data. |
155
|
|
|
*/ |
156
|
|
|
public function getServerData() |
157
|
|
|
{ |
158
|
|
|
return $this->serverData; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get the HTTP REFERER from the server data. |
164
|
|
|
* |
165
|
|
|
* @return string|null The referer or null if not found. |
166
|
|
|
*/ |
167
|
|
|
public function getReferer() |
168
|
|
|
{ |
169
|
|
|
$referer = null; |
170
|
|
|
|
171
|
|
|
if (array_key_exists(self::REFERER_KEY, $this->serverData)) { |
172
|
|
|
$referer = $this->serverData[self::REFERER_KEY]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $referer; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get file data. |
181
|
|
|
* |
182
|
|
|
* @return array File data. |
183
|
|
|
*/ |
184
|
|
|
public function getFileData() |
185
|
|
|
{ |
186
|
|
|
return $this->fileData; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Add data to the request. |
192
|
|
|
* |
193
|
|
|
* @param array $data An array of data. |
194
|
|
|
*/ |
195
|
|
|
public function addRequestData(array $data) |
196
|
|
|
{ |
197
|
|
|
$this->requestData = array_merge($this->requestData, $data); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return bool Whether the method is post or not. |
203
|
|
|
*/ |
204
|
|
|
public function isPost() |
205
|
|
|
{ |
206
|
|
|
return $this->method === self::METHOD_POST; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return bool Whether the method is get or not. |
212
|
|
|
*/ |
213
|
|
|
public function isGet() |
214
|
|
|
{ |
215
|
|
|
return $this->method === self::METHOD_GET; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Get cookie value by name |
221
|
|
|
* |
222
|
|
|
* @param string $name |
223
|
|
|
* |
224
|
|
|
* @return string|null returns the cookie value for the index name provided |
225
|
|
|
*/ |
226
|
|
|
public function getCookie($name) |
227
|
|
|
{ |
228
|
|
|
return (isset($this->cookies[$name])) ? $this->cookies[$name] : null; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Returns true if the cookie is set for the name provided else false |
234
|
|
|
* |
235
|
|
|
* @param string @name |
236
|
|
|
* |
237
|
|
|
* @return boolean |
238
|
|
|
*/ |
239
|
|
|
public function issetCookie($name) |
240
|
|
|
{ |
241
|
|
|
return ($this->getCookie($name) !== null); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|