1
|
|
|
<?php |
2
|
|
|
namespace Economic\API; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* @class Economic_Response |
6
|
|
|
*/ |
7
|
|
|
class Response |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* HTTP status code of request. |
11
|
|
|
* |
12
|
|
|
* @var integer |
13
|
|
|
*/ |
14
|
|
|
protected $status_code; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The headers sent during the request. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $sent_headers; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The headers received during the request. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $received_headers; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Response body of last request. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $response_data; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* __construct |
39
|
|
|
* |
40
|
|
|
* Instantiates a new response object |
41
|
|
|
* |
42
|
|
|
* @param int $status_code the HTTP status code |
43
|
|
|
* @param string $sent_headers the headers sent |
44
|
|
|
* @param string $received_headers the headers received |
45
|
|
|
* @param string $response_data the http response body |
46
|
|
|
*/ |
47
|
4 |
|
public function __construct($status_code, $sent_headers, $received_headers, $response_data) |
48
|
|
|
{ |
49
|
4 |
|
$this->status_code = $status_code; |
50
|
4 |
|
$this->sent_headers = $sent_headers; |
51
|
4 |
|
$this->received_headers = $received_headers; |
52
|
4 |
|
$this->response_data = $response_data; |
53
|
4 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* asRaw |
57
|
|
|
* |
58
|
|
|
* Returns the HTTP status code, headers and response body. |
59
|
|
|
* Usage: list($status_code, $headers, $response_body) = $response->as_raw(). |
60
|
|
|
* |
61
|
|
|
* @param boolan $keep_authorization_value Normally the value of the |
62
|
|
|
* Authorization: header is masked. True keeps the sent value. |
63
|
|
|
* @return array [integer, string[], string] |
64
|
|
|
*/ |
65
|
|
|
public function asRaw($keep_authorization_value = false) |
66
|
|
|
{ |
67
|
|
|
// To avoid unintentional logging of credentials the default is to mask the value of the Authorization: header |
68
|
|
|
if ($keep_authorization_value) { |
69
|
|
|
$sent_headers = $this->sent_headers; |
70
|
|
|
} else { |
71
|
|
|
// Avoid dependency on mbstring |
72
|
|
|
$lines = explode("\n", $this->sent_headers); |
73
|
|
|
foreach ($lines as &$line) { |
74
|
|
|
if (strpos($line, 'Authorization: ') === 0) { |
75
|
|
|
$line = 'Authorization: <hidden by default>'; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
$sent_headers = implode("\n", $lines); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return array( |
82
|
|
|
$this->status_code, |
83
|
|
|
array( |
84
|
|
|
'sent' => $sent_headers, |
85
|
|
|
'received' => $this->received_headers, |
86
|
|
|
), |
87
|
|
|
$this->response_data, |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* asArray |
93
|
|
|
* |
94
|
|
|
* Returns the response body as an array |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
public function asArray() |
99
|
|
|
{ |
100
|
|
|
if ($response = json_decode($this->response_data, true)) { |
101
|
|
|
return $response; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return array(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* asObject |
109
|
|
|
* |
110
|
|
|
* Returns the response body as an array |
111
|
|
|
* |
112
|
|
|
* @return \stdClass |
113
|
|
|
*/ |
114
|
|
|
public function asObject() |
115
|
|
|
{ |
116
|
|
|
if ($response = json_decode($this->response_data)) { |
117
|
|
|
return $response; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return new \stdClass; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* httpStatus |
125
|
|
|
* |
126
|
|
|
* Returns the http_status code |
127
|
|
|
* |
128
|
|
|
* @return int |
129
|
|
|
*/ |
130
|
1 |
|
public function httpStatus() |
131
|
|
|
{ |
132
|
1 |
|
return $this->status_code; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* isSuccess |
137
|
|
|
* |
138
|
|
|
* Checks if the http status code indicates a succesful or an error response. |
139
|
|
|
* |
140
|
|
|
* @return boolean |
141
|
|
|
*/ |
142
|
2 |
|
public function isSuccess() |
143
|
|
|
{ |
144
|
2 |
|
if ($this->status_code > 299) { |
145
|
1 |
|
return false; |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
return true; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|