1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mpyw\Cowitter; |
4
|
|
|
|
5
|
|
|
use mpyw\Cowitter\Helpers\ResponseBodyNormalizer; |
6
|
|
|
|
7
|
|
|
class Response implements ResponseInterface |
8
|
|
|
{ |
9
|
|
|
protected $version; |
10
|
|
|
protected $statusCode; |
11
|
|
|
protected $reasonPhrase; |
12
|
|
|
protected $headers = []; |
13
|
|
|
protected $handle; |
14
|
|
|
protected $rawContent; |
15
|
|
|
protected $content; |
16
|
|
|
|
17
|
6 |
|
public function __construct($buffer, $ch) |
18
|
6 |
|
{ |
19
|
6 |
|
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); |
20
|
6 |
|
$header_buffer = substr($buffer, 0, $header_size - 4); |
21
|
6 |
|
$body_buffer = substr($buffer, $header_size); |
22
|
6 |
|
$header_buffer = current(array_slice(explode("\r\n\r\n", $header_buffer), -1)); |
23
|
6 |
|
$lines = explode("\r\n", $header_buffer); |
24
|
6 |
|
if (!preg_match('@\AHTTP/([\d.]+)\s+(\d{3})\s+(.+)\z@i', array_shift($lines), $matches)) { |
25
|
|
|
throw new \UnexpectedValueException('Invalid response line.'); |
26
|
|
|
} |
27
|
6 |
|
$this->version = $matches[1]; |
28
|
6 |
|
$this->statusCode = (int)$matches[2]; |
29
|
6 |
|
$this->reasonPhrase = $matches[3]; |
30
|
6 |
|
foreach ($lines as $line) { |
31
|
6 |
|
list($key, $value) = explode(':', $line, 2) + [1 => '']; |
32
|
6 |
|
$this->headers[strtolower(trim($key))][] = trim($value); |
33
|
|
|
} |
34
|
6 |
|
$this->handle = $ch; |
35
|
6 |
|
$this->rawContent = $body_buffer; |
36
|
6 |
|
} |
37
|
|
|
|
38
|
|
|
public function getVersion() |
39
|
|
|
{ |
40
|
|
|
return $this->version; |
41
|
|
|
} |
42
|
|
|
|
43
|
6 |
|
public function getStatusCode() |
44
|
6 |
|
{ |
45
|
6 |
|
return $this->statusCode; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getReasonPhrase() |
49
|
|
|
{ |
50
|
|
|
return $this->reasonPhrase; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getHeaders() |
54
|
|
|
{ |
55
|
|
|
return $this->headers; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getHeader($name) |
59
|
|
|
{ |
60
|
|
|
$name = strtolower($name); |
61
|
|
|
return isset($this->headers[$name]) ? $this->headers[$name] : []; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getHeaderLine($name, $delimiter = ', ') |
65
|
|
|
{ |
66
|
|
|
$name = strtolower($name); |
67
|
|
|
return implode($delimiter, $this->getHeader($name)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getHandle() |
71
|
|
|
{ |
72
|
|
|
return $this->handle; |
73
|
|
|
} |
74
|
|
|
|
75
|
6 |
|
public function getRawContent() |
76
|
6 |
|
{ |
77
|
6 |
|
return $this->rawContent; |
78
|
|
|
} |
79
|
|
|
|
80
|
6 |
|
public function getContent() |
81
|
6 |
|
{ |
82
|
6 |
|
if ($this->content === null) { |
83
|
|
|
throw new \UnderflowException('Decoded content has not created yet.'); |
84
|
|
|
} |
85
|
6 |
|
return $this->content; |
86
|
|
|
} |
87
|
|
|
|
88
|
6 |
|
public function hasContent() |
89
|
6 |
|
{ |
90
|
6 |
|
return $this->content !== null; |
91
|
|
|
} |
92
|
|
|
|
93
|
6 |
|
public function withDecodedContent($content) |
94
|
6 |
|
{ |
95
|
6 |
|
$clone = clone $this; |
96
|
6 |
|
$clone->content = $content; |
97
|
6 |
|
return $clone; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|