1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Vadim Bova <[email protected]> |
5
|
|
|
* @link https://bova.io |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Folour\Oxide\Components\Response; |
9
|
|
|
|
10
|
|
|
use Folour\Oxide\Interfaces\ResponseInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @inheritdoc |
14
|
|
|
*/ |
15
|
|
|
class Response implements ResponseInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $headers; |
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
private $code; |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $body; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @inheritdoc |
32
|
|
|
*/ |
33
|
|
|
public function __construct(string $response, array $info) |
34
|
|
|
{ |
35
|
|
|
$this->headers = $this->parseHeaders($response, $info); |
36
|
|
|
$this->body = $this->parseBody($response, $info); |
37
|
|
|
$this->code = (int)$info['http_code']; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
|
|
public function headers(): array |
44
|
|
|
{ |
45
|
|
|
return $this->headers; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
*/ |
51
|
|
|
public function body(): string |
52
|
|
|
{ |
53
|
|
|
return $this->body; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
|
|
public function code(): int |
60
|
|
|
{ |
61
|
|
|
return $this->code; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritdoc |
66
|
|
|
*/ |
67
|
|
|
public function __toString(): string |
68
|
|
|
{ |
69
|
|
|
return $this->body(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Parse body from response |
74
|
|
|
* |
75
|
|
|
* @param string $response |
76
|
|
|
* @param array $info |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
protected function parseBody(string $response, array $info): string |
80
|
|
|
{ |
81
|
|
|
$body = substr($response, $info['header_size']); |
82
|
|
|
|
83
|
|
|
return trim($body); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Parse headers from response |
88
|
|
|
* |
89
|
|
|
* @param string $response |
90
|
|
|
* @param array $info |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
protected function parseHeaders(string $response, array $info): array |
94
|
|
|
{ |
95
|
|
|
$ex_headers = explode("\r\n", $this->getLastHeaders($response, $info)); |
96
|
|
|
$headers = []; |
97
|
|
|
|
98
|
|
|
foreach (array_slice($ex_headers, 1) as $k => $line) { |
99
|
|
|
[$headers[0][$k], $headers[1][$k]] = explode(': ', $line); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return array_combine($headers[0], $headers[1]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $response |
108
|
|
|
* @param array $info |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
protected function getLastHeaders(string $response, array $info): string |
112
|
|
|
{ |
113
|
|
|
$raw_headers = trim(substr($response, 0, $info['header_size'])); |
114
|
|
|
$parts = explode("\r\n\r\n", $raw_headers); |
115
|
|
|
|
116
|
|
|
return trim(end($parts)); |
117
|
|
|
} |
118
|
|
|
} |