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