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
|
|
|
$raw_headers = trim(substr($response, 0, $info['header_size'])); |
96
|
|
|
|
97
|
|
|
//If follow locations enabled, headers of all redirects is returned. |
98
|
|
|
//But we need headers of last request only. |
99
|
|
|
if(strstr($raw_headers, "\r\n\r\n")) { |
100
|
|
|
$ex = explode("\r\n\r\n", $raw_headers); |
101
|
|
|
$raw_headers = end($ex); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$ex_headers = explode("\r\n", trim($raw_headers)); |
105
|
|
|
$headers = []; |
106
|
|
|
|
107
|
|
|
foreach ($ex_headers as $header_line) { |
108
|
|
|
if(strstr($header_line, ': ')) { |
109
|
|
|
[$header_key, $header_value] = explode(': ', $header_line); |
|
|
|
|
110
|
|
|
|
111
|
|
|
$headers[$header_key] = $header_value; |
112
|
|
|
} else { |
113
|
|
|
$headers[] = $header_line; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $headers; |
118
|
|
|
} |
119
|
|
|
} |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.