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