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