1 | <?php |
||
2 | /** |
||
3 | * This file is part of the O2System Framework package. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | * |
||
8 | * @author Steeve Andrian Salim |
||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
10 | */ |
||
11 | |||
12 | // ------------------------------------------------------------------------ |
||
13 | |||
14 | namespace O2System\Curl; |
||
15 | |||
16 | // ------------------------------------------------------------------------ |
||
17 | |||
18 | use O2System\Curl\Response\Error; |
||
19 | use O2System\Curl\Response\Headers; |
||
20 | use O2System\Curl\Response\Info; |
||
21 | use O2System\Curl\Response\SimpleJSONElement; |
||
22 | use O2System\Curl\Response\SimpleQueryElement; |
||
23 | use O2System\Curl\Response\SimpleSerializeElement; |
||
24 | use O2System\Curl\Response\SimpleXMLElement; |
||
25 | |||
26 | /** |
||
27 | * Class Response |
||
28 | * |
||
29 | * @package O2System\Curl |
||
30 | */ |
||
31 | class Response |
||
32 | { |
||
33 | /** |
||
34 | * Response::$string |
||
35 | * |
||
36 | * Raw string response. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $string; |
||
41 | |||
42 | /** |
||
43 | * Response::$info |
||
44 | * |
||
45 | * Response info object. |
||
46 | * |
||
47 | * @var Info |
||
48 | */ |
||
49 | protected $info; |
||
50 | |||
51 | /** |
||
52 | * Response::$error |
||
53 | * |
||
54 | * Response error object. |
||
55 | * |
||
56 | * @var Error |
||
57 | */ |
||
58 | protected $error; |
||
59 | |||
60 | /** |
||
61 | * Response::$headers |
||
62 | * |
||
63 | * Response headers object. |
||
64 | * |
||
65 | * @var Headers |
||
66 | */ |
||
67 | protected $headers; |
||
68 | |||
69 | /** |
||
70 | * Response::$body |
||
71 | * |
||
72 | * Response body. |
||
73 | * |
||
74 | * @var SimpleJSONElement|SimpleQueryElement|SimpleXMLElement|SimpleSerializeElement|\DOMDocument|string |
||
75 | */ |
||
76 | protected $body; |
||
77 | |||
78 | // ------------------------------------------------------------------------ |
||
79 | |||
80 | /** |
||
81 | * Response::__construct |
||
82 | * |
||
83 | * @param resource $curlHandle Curl handle resource. |
||
84 | */ |
||
85 | public function __construct($curlHandle) |
||
86 | { |
||
87 | if (($errorNumber = curl_errno($curlHandle)) != 0) { |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
88 | $this->error = new Error([ |
||
89 | 'code' => curl_errno($curlHandle), |
||
90 | 'message' => curl_error($curlHandle), |
||
91 | ]); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | // ------------------------------------------------------------------------ |
||
96 | |||
97 | /** |
||
98 | * Response::setContent |
||
99 | * |
||
100 | * Sets response content manualy. |
||
101 | * |
||
102 | * @param string $content |
||
103 | * |
||
104 | * @return static |
||
105 | */ |
||
106 | public function setContent($content) |
||
107 | { |
||
108 | $this->string = $content; |
||
109 | $this->fetchHeader($content); |
||
110 | $this->fetchBody($content); |
||
111 | |||
112 | return $this; |
||
113 | } |
||
114 | |||
115 | // ------------------------------------------------------------------------ |
||
116 | |||
117 | /** |
||
118 | * Response::fetchHeader |
||
119 | * |
||
120 | * Fetch response header. |
||
121 | * |
||
122 | * @param string $response |
||
123 | */ |
||
124 | protected function fetchHeader($response) |
||
125 | { |
||
126 | $headers = []; |
||
127 | $headerSize = 0; |
||
128 | $headerParts = explode(PHP_EOL, $response); |
||
129 | |||
130 | foreach ($headerParts as $headerString) { |
||
131 | |||
132 | $headerSize += strlen($headerString); |
||
133 | |||
134 | $headerString = trim($headerString); |
||
135 | if (empty($headerString)) { |
||
136 | break; |
||
137 | } |
||
138 | |||
139 | if (strpos($headerString, ':') !== false) { |
||
140 | $headerSize += strlen(PHP_EOL); |
||
141 | $headerString = str_replace('"', '', $headerString); |
||
142 | $headerStringParts = explode(':', $headerString); |
||
143 | $headerStringParts = array_map('trim', $headerStringParts); |
||
144 | |||
145 | $headers[ $headerStringParts[ 0 ] ] = $headerStringParts[ 1 ]; |
||
146 | } elseif (preg_match("/(HTTP\/[0-9].[0-9])\s([0-9]+)\s([a-zA-Z]+)/", $headerString, $matches)) { |
||
147 | $headerSize += strlen(PHP_EOL); |
||
148 | |||
149 | $this->info->httpVersion = $matches[ 1 ]; |
||
0 ignored issues
–
show
|
|||
150 | $this->info->httpCode = $matches[ 2 ]; |
||
0 ignored issues
–
show
|
|||
151 | $this->info->httpCodeDescription = $matches[ 3 ]; |
||
0 ignored issues
–
show
|
|||
152 | } |
||
153 | } |
||
154 | |||
155 | $this->headers = new Headers($headers); |
||
156 | |||
157 | // Update info |
||
158 | if ($this->headers->offsetExists('contentType')) { |
||
159 | $this->info->contentType = $this->headers->offsetGet('contentType'); |
||
0 ignored issues
–
show
|
|||
160 | } |
||
161 | |||
162 | $this->info->headerSize = $headerSize; |
||
0 ignored issues
–
show
|
|||
163 | } |
||
164 | |||
165 | // ------------------------------------------------------------------------ |
||
166 | |||
167 | /** |
||
168 | * Response::fetchBody |
||
169 | * |
||
170 | * Fetch response body. |
||
171 | * |
||
172 | * @param string $response |
||
173 | */ |
||
174 | protected function fetchBody($response) |
||
175 | { |
||
176 | $body = substr($response, $this->info->headerSize); |
||
0 ignored issues
–
show
The property
headerSize does not exist on O2System\Curl\Response\Info . Since you implemented __get , consider adding a @property annotation.
![]() |
|||
177 | $body = trim($body); |
||
178 | $jsonBody = json_decode($body, true); |
||
179 | |||
180 | if (is_array($jsonBody) AND json_last_error() === JSON_ERROR_NONE) { |
||
181 | $this->body = new SimpleJSONElement($jsonBody); |
||
182 | } elseif (strpos($body, '?xml') !== false) { |
||
183 | $this->body = new SimpleXMLElement($body); |
||
184 | } elseif (strpos($body, '!DOCTYPE') !== false or strpos($body, '!doctype') !== false) { |
||
185 | $DomDocument = new \DOMDocument(); |
||
186 | $DomDocument->loadHTML($body); |
||
187 | $this->body = $DomDocument; |
||
188 | } elseif (false !== ($serializeArray = unserialize($body))) { |
||
189 | $this->body = new SimpleSerializeElement($serializeArray); |
||
190 | } else { |
||
191 | parse_str($body, $queryString); |
||
192 | |||
193 | if (isset($queryString[ 0 ])) { |
||
194 | $this->body = $body; |
||
195 | } else { |
||
196 | $this->body = new SimpleQueryElement($queryString); |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | |||
201 | // ------------------------------------------------------------------------ |
||
202 | |||
203 | /** |
||
204 | * Response::getInfo |
||
205 | * |
||
206 | * Gets response info object. |
||
207 | * |
||
208 | * @return \O2System\Curl\Response\Info |
||
209 | */ |
||
210 | public function getInfo() |
||
211 | { |
||
212 | return $this->info; |
||
213 | } |
||
214 | |||
215 | // ------------------------------------------------------------------------ |
||
216 | |||
217 | /** |
||
218 | * Response::setInfo |
||
219 | * |
||
220 | * Sets response info manualy. |
||
221 | * |
||
222 | * @param array $info |
||
223 | * |
||
224 | * @return static |
||
225 | */ |
||
226 | public function setInfo(array $info) |
||
227 | { |
||
228 | $this->info = new Info($info); |
||
229 | |||
230 | return $this; |
||
231 | } |
||
232 | |||
233 | // ------------------------------------------------------------------------ |
||
234 | |||
235 | /** |
||
236 | * Response::getHeaders |
||
237 | * |
||
238 | * Gets response headers object. |
||
239 | * |
||
240 | * @return \O2System\Curl\Response\Headers |
||
241 | */ |
||
242 | public function getHeaders() |
||
243 | { |
||
244 | return $this->headers; |
||
245 | } |
||
246 | |||
247 | // ------------------------------------------------------------------------ |
||
248 | |||
249 | /** |
||
250 | * Response::getBody |
||
251 | * |
||
252 | * Gets response body. |
||
253 | * |
||
254 | * @return SimpleJSONElement|SimpleQueryElement|SimpleXMLElement|SimpleSerializeElement|\DOMDocument|string |
||
255 | */ |
||
256 | public function getBody() |
||
257 | { |
||
258 | return $this->body; |
||
259 | } |
||
260 | |||
261 | // ------------------------------------------------------------------------ |
||
262 | |||
263 | /** |
||
264 | * Response::getError |
||
265 | * |
||
266 | * Gets response error. |
||
267 | * |
||
268 | * @return Error|false |
||
269 | */ |
||
270 | public function getError() |
||
271 | { |
||
272 | if ($this->error instanceof Error) { |
||
0 ignored issues
–
show
|
|||
273 | return $this->error; |
||
274 | } |
||
275 | |||
276 | return false; |
||
277 | } |
||
278 | } |