| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Requests_Response class |
| 5 |
* |
| 6 |
* wrapper object for a Requests_Response for standardisation. |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace AliNext_Lite;; |
| 10 |
|
| 11 |
use AliNext_Lite\Lib\Requests_Utility_CaseInsensitiveDictionary; |
| 12 |
use AliNext_Lite\Lib\Requests_Response_Headers; |
| 13 |
|
| 14 |
class Requests_Response { |
| 15 |
|
| 16 |
protected $response; |
| 17 |
|
| 18 |
/** |
| 19 |
* Constructor. |
| 20 |
* |
| 21 |
* @param \Requests_Response $response HTTP response. |
| 22 |
*/ |
| 23 |
public function __construct(\AliNext_Lite\Lib\Requests_Response $response) { |
| 24 |
$this->response = $response; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Retrieves the response object for the request. |
| 29 |
* |
| 30 |
* @return \Requests_Response HTTP response. |
| 31 |
*/ |
| 32 |
public function get_response_object() { |
| 33 |
return $this->response; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Retrieves headers associated with the response. |
| 38 |
* |
| 39 |
* @return array Map of header name to header value. |
| 40 |
*/ |
| 41 |
public function get_headers() { |
| 42 |
// Ensure headers remain case-insensitive |
| 43 |
$converted = new Requests_Utility_CaseInsensitiveDictionary(); |
| 44 |
|
| 45 |
foreach ($this->response->headers->getAll() as $key => $value) { |
| 46 |
if (count($value) === 1) { |
| 47 |
$converted[$key] = $value[0]; |
| 48 |
} else { |
| 49 |
$converted[$key] = $value; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
return $converted; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Sets all header values. |
| 58 |
|
| 59 |
* @param array $headers Map of header name to header value. |
| 60 |
*/ |
| 61 |
public function set_headers($headers) { |
| 62 |
$this->response->headers = new Requests_Response_Headers($headers); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Sets a single HTTP header. |
| 67 |
* |
| 68 |
* @param string $key Header name. |
| 69 |
* @param string $value Header value. |
| 70 |
* @param bool $replace Optional. Whether to replace an existing header of the same name. |
| 71 |
* Default true. |
| 72 |
*/ |
| 73 |
public function header($key, $value, $replace = true) { |
| 74 |
if ($replace) { |
| 75 |
unset($this->response->headers[$key]); |
| 76 |
} |
| 77 |
|
| 78 |
$this->response->headers[$key] = $value; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Retrieves the HTTP return code for the response. |
| 83 |
* |
| 84 |
* @return int The 3-digit HTTP status code. |
| 85 |
*/ |
| 86 |
public function get_status() { |
| 87 |
return $this->response->status_code; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Sets the 3-digit HTTP status code. |
| 92 |
* |
| 93 |
* @param int $code HTTP status. |
| 94 |
*/ |
| 95 |
public function set_status($code) { |
| 96 |
$this->response->status_code = absint($code); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Retrieves the response data. |
| 101 |
* |
| 102 |
* @return mixed Response data. |
| 103 |
*/ |
| 104 |
public function get_data() { |
| 105 |
return $this->response->body; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Sets the response data. |
| 110 |
* |
| 111 |
* @param mixed $data Response data. |
| 112 |
*/ |
| 113 |
public function set_data($data) { |
| 114 |
$this->response->body = $data; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Retrieves cookies from the response. |
| 119 |
* |
| 120 |
*/ |
| 121 |
public function get_cookies() { |
| 122 |
return $this->response->cookies; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Converts the object to array |
| 127 |
* |
| 128 |
* @return array |
| 129 |
*/ |
| 130 |
public function to_array() { |
| 131 |
return array( |
| 132 |
'headers' => $this->get_headers(), |
| 133 |
'body' => $this->get_data(), |
| 134 |
'response' => array( |
| 135 |
'code' => $this->get_status(), |
| 136 |
'message' => get_status_header_desc($this->get_status()), |
| 137 |
), |
| 138 |
'cookies' => $this->get_cookies() |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
} |
| 143 |
|