|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace alkemann\h2l; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Request |
|
7
|
|
|
* |
|
8
|
|
|
* @TODO : $locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']); |
|
9
|
|
|
* @package alkemann\h2l |
|
10
|
|
|
*/ |
|
11
|
|
|
class Request extends Message |
|
12
|
|
|
{ |
|
13
|
|
|
const GET = 'GET'; |
|
14
|
|
|
const HEAD = 'HEAD'; |
|
15
|
|
|
const POST = 'POST'; |
|
16
|
|
|
const PUT = 'PUT'; |
|
17
|
|
|
const DELETE = 'DELETE'; |
|
18
|
|
|
const CONNECT = 'CONNECT'; |
|
19
|
|
|
const OPTIONS = 'OPTIONS'; |
|
20
|
|
|
const TRACE = 'TRACE'; |
|
21
|
|
|
const PATCH = 'PATCH'; |
|
22
|
|
|
|
|
23
|
|
|
protected $parameters = []; |
|
24
|
|
|
protected $request = []; |
|
25
|
|
|
protected $server = []; |
|
26
|
|
|
protected $get = []; |
|
27
|
|
|
protected $post = []; |
|
28
|
|
|
protected $route = null; |
|
29
|
|
|
protected $content_type = ''; |
|
30
|
|
|
protected $accept_type = Message::CONTENT_HTML; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get request parameters from url as url params, get queries or post, in that order |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $name the name of the parameter |
|
36
|
|
|
* @return mixed|null the value or null if not set |
|
37
|
|
|
*/ |
|
38
|
|
|
public function param(string $name) |
|
39
|
|
|
{ |
|
40
|
|
|
if (isset($this->parameters[$name])) { |
|
41
|
|
|
return $this->parameters[$name]; |
|
42
|
|
|
} |
|
43
|
|
|
if (isset($this->get[$name])) { |
|
44
|
|
|
return $this->get[$name]; |
|
45
|
|
|
} |
|
46
|
|
|
if (isset($this->post[$name])) { |
|
47
|
|
|
return $this->post[$name]; |
|
48
|
|
|
} |
|
49
|
|
|
return null; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param array $request |
|
54
|
|
|
* @return Request |
|
55
|
|
|
*/ |
|
56
|
|
|
public function withRequestParams(array $request): Request |
|
57
|
|
|
{ |
|
58
|
|
|
$new = clone $this; |
|
59
|
|
|
$new->url = $request['url'] ?? '/'; |
|
60
|
|
|
unset($request['url']); |
|
61
|
|
|
$new->request = $request; |
|
62
|
|
|
return $new; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getRequestParams(): array |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->request; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param array $server |
|
75
|
|
|
* @return Request |
|
76
|
|
|
*/ |
|
77
|
|
|
public function withServerParams(array $server): Request |
|
78
|
|
|
{ |
|
79
|
|
|
$new = clone $this; |
|
80
|
|
|
$new->server = $server; |
|
81
|
|
|
$new->setContentTypeFromServerParams($server['HTTP_CONTENT_TYPE'] ?? ''); |
|
82
|
|
|
$new->setAcceptTypeFromServerParams($server['HTTP_ACCEPT'] ?? ''); |
|
83
|
|
|
$new->method = $server['REQUEST_METHOD'] ?? Request::GET; |
|
84
|
|
|
$new->headers = Util::getRequestHeadersFromServerArray($server); |
|
85
|
|
|
return $new; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
private function setContentTypeFromServerParams(string $content_type): void |
|
89
|
|
|
{ |
|
90
|
|
|
$known_content_types = [ |
|
91
|
|
|
Message::CONTENT_JSON, |
|
92
|
|
|
Message::CONTENT_XML, |
|
93
|
|
|
Message::CONTENT_TEXT_XML, |
|
94
|
|
|
Message::CONTENT_FORM |
|
95
|
|
|
]; |
|
96
|
|
|
foreach ($known_content_types as $t) { |
|
97
|
|
|
if (strpos($content_type, $t) !== false) { |
|
98
|
|
|
$this->content_type = $t; |
|
99
|
|
|
return; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private function setAcceptTypeFromServerParams(string $accept_type): void |
|
105
|
|
|
{ |
|
106
|
|
|
$known_accept_types = [ |
|
107
|
|
|
Message::CONTENT_JSON, |
|
108
|
|
|
Message::CONTENT_HTML, |
|
109
|
|
|
Message::CONTENT_XML, |
|
110
|
|
|
Message::CONTENT_TEXT_XML, |
|
111
|
|
|
]; |
|
112
|
|
|
foreach ($known_accept_types as $t) { |
|
113
|
|
|
if (strpos($accept_type, $t) !== false) { |
|
114
|
|
|
$this->accept_type = $t; |
|
115
|
|
|
return; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function acceptType(): string |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->accept_type; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return array |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getServerParam(): array |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->server; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param array $post |
|
135
|
|
|
* @return Request |
|
136
|
|
|
*/ |
|
137
|
|
|
public function withPostData(array $post): Request |
|
138
|
|
|
{ |
|
139
|
|
|
$new = clone $this; |
|
140
|
|
|
$new->post = $post; |
|
141
|
|
|
return $new; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return array |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getPostData(): array |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->post; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param array $get |
|
154
|
|
|
* @return Request |
|
155
|
|
|
*/ |
|
156
|
|
|
public function withGetData(array $get): Request |
|
157
|
|
|
{ |
|
158
|
|
|
$new = clone $this; |
|
159
|
|
|
$new->get = $get; |
|
160
|
|
|
return $new; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return array |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getGetData(): array |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->get; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @return array |
|
173
|
|
|
*/ |
|
174
|
|
|
public function query(): array |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->get; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param array $parameters |
|
181
|
|
|
* @return Request |
|
182
|
|
|
*/ |
|
183
|
|
|
public function withUrlParams(array $parameters): Request |
|
184
|
|
|
{ |
|
185
|
|
|
$new = clone $this; |
|
186
|
|
|
$new->parameters = $parameters; |
|
187
|
|
|
return $new; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @return array |
|
192
|
|
|
*/ |
|
193
|
|
|
public function getUrlParams(): array |
|
194
|
|
|
{ |
|
195
|
|
|
return $this->parameters; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param interfaces\Route $route |
|
200
|
|
|
* @return Request |
|
201
|
|
|
*/ |
|
202
|
|
|
public function withRoute(interfaces\Route $route): Request |
|
203
|
|
|
{ |
|
204
|
|
|
$new = clone $this; |
|
205
|
|
|
$new->route = $route; |
|
206
|
|
|
$new->parameters = $route->parameters(); |
|
207
|
|
|
return $new; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @return interfaces\Route|null |
|
212
|
|
|
*/ |
|
213
|
|
|
public function route(): ?interfaces\Route |
|
214
|
|
|
{ |
|
215
|
|
|
return $this->route; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|