Response.php
67 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Unirest; |
| 6 | |
| 7 | use CoreInterfaces\Core\Response\ResponseInterface; |
| 8 | use CoreInterfaces\Sdk\ConverterInterface; |
| 9 | |
| 10 | class Response implements ResponseInterface |
| 11 | { |
| 12 | private $code; |
| 13 | private $raw_body; |
| 14 | private $body; |
| 15 | private $headers; |
| 16 | |
| 17 | /** |
| 18 | * @param int $code response code of the cURL request |
| 19 | * @param string $raw_body the raw body of the cURL response |
| 20 | * @param array $headers parsed headers array from cURL response |
| 21 | * @param array $json_args arguments to pass to json_decode function |
| 22 | */ |
| 23 | public function __construct(int $code, string $raw_body, array $headers, array $json_args = []) |
| 24 | { |
| 25 | $this->code = $code; |
| 26 | $this->headers = $headers; |
| 27 | $this->raw_body = $raw_body; |
| 28 | $this->body = $raw_body; |
| 29 | |
| 30 | // make sure raw_body is the first argument |
| 31 | array_unshift($json_args, $raw_body); |
| 32 | |
| 33 | if (function_exists('json_decode')) { |
| 34 | $json = call_user_func_array('json_decode', $json_args); |
| 35 | |
| 36 | if (json_last_error() === JSON_ERROR_NONE) { |
| 37 | $this->body = $json; |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | public function getStatusCode(): int |
| 43 | { |
| 44 | return $this->code; |
| 45 | } |
| 46 | |
| 47 | public function getHeaders(): array |
| 48 | { |
| 49 | return $this->headers; |
| 50 | } |
| 51 | |
| 52 | public function getRawBody(): string |
| 53 | { |
| 54 | return $this->raw_body; |
| 55 | } |
| 56 | |
| 57 | public function getBody() |
| 58 | { |
| 59 | return $this->body; |
| 60 | } |
| 61 | |
| 62 | public function convert(ConverterInterface $converter) |
| 63 | { |
| 64 | return $converter->createHttpResponse($this); |
| 65 | } |
| 66 | } |
| 67 |