1 | <?php |
||
15 | class Response implements ResponseInterface |
||
16 | { |
||
17 | private $httpVersion; |
||
18 | private $httpCode; |
||
19 | private $httpCodeString; |
||
20 | private $response; |
||
21 | private $info; |
||
22 | private $errno; |
||
23 | private $errorString; |
||
24 | private $headers = array(); |
||
25 | private $body; |
||
26 | |||
27 | 19 | public function __construct($response, array $info, $errno, $errorString) |
|
28 | { |
||
29 | 19 | $this->response = $response; |
|
30 | 19 | $this->info = $info; |
|
31 | 19 | $this->errno = $errno; |
|
32 | 19 | $this->errorString = $errorString; |
|
33 | |||
34 | 19 | list($headers, $body) = $this->buildHeaderParts($response); |
|
35 | |||
36 | 19 | $this->buildHeadersArray($headers); |
|
37 | 19 | $this->body = $body; |
|
38 | 19 | } |
|
39 | |||
40 | 19 | private function buildHeaderParts($response) |
|
41 | { |
||
42 | 19 | $parts = explode("\r\n\r\nHTTP/", $response); |
|
43 | 19 | $parts = (count($parts) > 1 ? 'HTTP/' : '') . array_pop($parts); |
|
44 | |||
45 | 19 | return explode("\r\n\r\n", $parts, 2); |
|
46 | } |
||
47 | |||
48 | 19 | private function buildHeadersArray($headers) |
|
49 | { |
||
50 | 19 | $headers = explode("\n", $headers); |
|
51 | 19 | $httHead = array_shift($headers); |
|
52 | 19 | list($this->httpVersion, $this->httpCode, $this->httpCodeString) = explode(" ", $httHead, 3); |
|
53 | |||
54 | 19 | foreach ($headers as $header) { |
|
55 | 19 | list($key, $value) = explode(": ", $header, 2); |
|
56 | 19 | $this->headers[trim($key)] = trim($value); |
|
57 | } |
||
58 | 19 | } |
|
59 | |||
60 | 1 | public function getHeaders() |
|
64 | |||
65 | 9 | public function hasHeader($header) |
|
66 | { |
||
67 | 9 | return array_key_exists($header, $this->headers); |
|
68 | } |
||
69 | |||
70 | 8 | public function getHeader($header) |
|
71 | { |
||
72 | 8 | return $this->hasHeader($header) ? $this->headers[$header] : null; |
|
|
|||
73 | } |
||
74 | |||
75 | 9 | public function bodyRaw() |
|
76 | { |
||
77 | 9 | return $this->body; |
|
78 | } |
||
79 | |||
80 | 5 | public function bodyObject() |
|
81 | { |
||
82 | 5 | $contentType = $this->getHeader('Content-Type'); |
|
83 | 5 | if (stripos($contentType, 'application/json') !== false) { |
|
84 | 4 | return json_decode($this->body); |
|
85 | } |
||
86 | |||
87 | 1 | throw new \OutOfBoundsException("Last Request Content-Type isn't application/json."); |
|
88 | } |
||
89 | |||
90 | 2 | public function bodyArray() |
|
91 | { |
||
92 | 2 | $body = $this->bodyRaw(); |
|
93 | |||
94 | 2 | return json_decode($body, true); |
|
95 | } |
||
96 | |||
97 | 1 | public function getHttpVersion() |
|
98 | { |
||
99 | 1 | return $this->httpVersion; |
|
100 | } |
||
101 | |||
102 | 1 | public function getHttpCode() |
|
106 | |||
107 | 1 | public function getInfo() |
|
111 | |||
112 | public function getErrorNo() |
||
113 | { |
||
114 | return $this->errno; |
||
115 | } |
||
116 | |||
117 | public function getErrorString() |
||
118 | { |
||
119 | return $this->errorString; |
||
120 | } |
||
121 | |||
122 | public function getRaw() |
||
126 | |||
127 | public function getHttpCodeString() |
||
131 | } |
||
132 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: