1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace alkemann\h2l; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Message |
7
|
|
|
* |
8
|
|
|
* Container for requests and responses made with the Remote class |
9
|
|
|
* |
10
|
|
|
* @package alkemann\h2l |
11
|
|
|
*/ |
12
|
|
|
final class Message |
13
|
|
|
{ |
14
|
|
|
const CONTENT_JSON = 'application/json'; |
15
|
|
|
const CONTENT_FORM = 'application/x-www-form-urlencoded'; |
16
|
|
|
const CONTENT_HTML = 'text/html'; |
17
|
|
|
const CONTENT_TEXT = 'text/plain'; |
18
|
|
|
const CONTENT_XML = 'text/xml'; |
19
|
|
|
|
20
|
|
|
const REQUEST = "REQUEST"; |
21
|
|
|
const RESPONSE = "RESPONSE"; |
22
|
|
|
/** |
23
|
|
|
* @var string "REQUEST"|"RESPONSE" |
24
|
|
|
*/ |
25
|
|
|
private $type; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
private $code; |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $url = ''; |
35
|
|
|
/** |
36
|
|
|
* Enum with Request::GET, Request::POST etc |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $method = Request::GET; |
40
|
|
|
/** |
41
|
|
|
* @var ?string |
42
|
|
|
*/ |
|
|
|
|
43
|
|
|
private $body; |
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private $meta = []; |
48
|
|
|
/** |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
private $headers = []; |
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
private $options = []; |
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
private $content_type = 'text/html'; |
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
private $content_charset = 'utf-8'; |
64
|
|
|
|
65
|
|
|
// public function __construct() {} |
|
|
|
|
66
|
|
|
|
67
|
|
|
public function type(): ?string |
68
|
|
|
{ |
69
|
|
|
return $this->type; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function url(): string |
76
|
|
|
{ |
77
|
|
|
return $this->url; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function method(): string |
84
|
|
|
{ |
85
|
|
|
return $this->method; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return null|string |
90
|
|
|
*/ |
91
|
|
|
public function body(): ?string |
92
|
|
|
{ |
93
|
|
|
return $this->body; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return null|string|array|SimpleXMLElement body converted from raw format |
|
|
|
|
98
|
|
|
*/ |
99
|
|
|
public function content() |
100
|
|
|
{ |
101
|
|
|
switch ($this->contentType()) { |
102
|
|
|
case static::CONTENT_JSON: |
|
|
|
|
103
|
|
|
return json_decode($this->body, true); |
104
|
|
|
case static::CONTENT_XML: |
105
|
|
|
return new \SimpleXMLElement($this->body); |
106
|
|
|
case null: |
107
|
|
|
default: |
108
|
|
|
return $this->body; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function contentType(): string |
113
|
|
|
{ |
114
|
|
|
return $this->content_type; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function charset(): string |
118
|
|
|
{ |
119
|
|
|
return $this->content_charset; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $name |
124
|
|
|
* @return null|string |
125
|
|
|
*/ |
126
|
|
|
public function header(string $name): ?string |
127
|
|
|
{ |
128
|
|
|
foreach ($this->headers as $key => $value) { |
129
|
|
|
if (strcasecmp($key, $name) === 0) { |
130
|
|
|
return $value; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
return null; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $class name of class that must take data array as constructor |
138
|
|
|
* @return object body json decoded and sent to constructor of $class |
139
|
|
|
*/ |
140
|
|
|
public function as(string $class) |
141
|
|
|
{ |
142
|
|
|
return new $class($this->content()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
public function headers(): array |
149
|
|
|
{ |
150
|
|
|
return $this->headers; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
public function meta(): array |
157
|
|
|
{ |
158
|
|
|
return $this->meta; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
public function options(): array |
165
|
|
|
{ |
166
|
|
|
return $this->options; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return int|null |
171
|
|
|
*/ |
172
|
|
|
public function code(): ?int |
173
|
|
|
{ |
174
|
|
|
return $this->code; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string $type |
179
|
|
|
* @return Message |
180
|
|
|
*/ |
181
|
|
|
public function withType(string $type): Message |
182
|
|
|
{ |
183
|
|
|
$new = clone $this; |
184
|
|
|
$new->type = $type; |
185
|
|
|
return $new; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param int $code |
190
|
|
|
* @return Message |
191
|
|
|
*/ |
192
|
|
|
public function withCode(int $code): Message |
193
|
|
|
{ |
194
|
|
|
$new = clone $this; |
195
|
|
|
$new->code = $code; |
196
|
|
|
return $new; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param string $url |
201
|
|
|
* @return Message |
202
|
|
|
*/ |
203
|
|
|
public function withUrl(string $url): Message |
204
|
|
|
{ |
205
|
|
|
$new = clone $this; |
206
|
|
|
$new->url = $url; |
207
|
|
|
return $new; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string $method |
212
|
|
|
* @return Message |
213
|
|
|
*/ |
214
|
|
|
public function withMethod(string $method): Message |
215
|
|
|
{ |
216
|
|
|
$new = clone $this; |
217
|
|
|
$new->method = $method; |
218
|
|
|
return $new; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param string $body |
223
|
|
|
* @return Message |
224
|
|
|
*/ |
225
|
|
|
public function withBody(string $body): Message |
226
|
|
|
{ |
227
|
|
|
$new = clone $this; |
228
|
|
|
$new->body = $body; |
229
|
|
|
return $new; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param array $headers |
234
|
|
|
* @return Message |
235
|
|
|
*/ |
236
|
|
|
public function withHeaders(array $headers): Message |
237
|
|
|
{ |
238
|
|
|
$new = clone $this; |
239
|
|
|
$new->headers = $headers; |
240
|
|
|
$content_header = $new->header('Content-Type'); |
241
|
|
|
if ($content_header) { |
|
|
|
|
242
|
|
|
if (strpos($content_header, ';') === false) { |
243
|
|
|
$new->content_type = trim(strtolower($content_header)); |
244
|
|
|
} else { |
245
|
|
|
list($type, $other) = explode(';', $content_header, 2); |
246
|
|
|
$new->content_type = trim(strtolower($type)); |
247
|
|
|
list($key, $charset) = explode('=', $other, 2); |
248
|
|
|
if ('charset' === strtolower(trim($key))) { |
249
|
|
|
$new->content_charset = strtolower(trim(trim($charset, '"'))); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
return $new; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @param array $options |
258
|
|
|
* @return Message |
259
|
|
|
*/ |
260
|
|
|
public function withOptions(array $options): Message |
261
|
|
|
{ |
262
|
|
|
$new = clone $this; |
263
|
|
|
$new->options = $options; |
264
|
|
|
return $new; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @param array $meta |
269
|
|
|
* @return Message |
270
|
|
|
*/ |
271
|
|
|
public function withMeta(array $meta): Message |
272
|
|
|
{ |
273
|
|
|
$new = clone $this; |
274
|
|
|
$new->meta = $meta; |
275
|
|
|
return $new; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|