|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the {@link RequestHelper_Response} class. |
|
4
|
|
|
* @package Application Utils |
|
5
|
|
|
* @subpackage RequestHelper |
|
6
|
|
|
* @see RequestHelper_Response |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace AppUtils; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Stores information on the response to a request that was sent. |
|
15
|
|
|
* |
|
16
|
|
|
* @package Application Utils |
|
17
|
|
|
* @subpackage RequestHelper |
|
18
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class RequestHelper_Response |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var RequestHelper |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $request; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $body = ''; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $info; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var bool |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $isError = false; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $errorMessage = ''; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var integer |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $errorCode = 0; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param RequestHelper $helper |
|
54
|
|
|
* @param array $info The CURL info array from curl_getinfo(). |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct(RequestHelper $helper, array $info) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->request = $helper; |
|
59
|
|
|
$this->info = $info; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Retrieves the request instance that initiated the request. |
|
64
|
|
|
* |
|
65
|
|
|
* @return RequestHelper |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getRequest() : RequestHelper |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->request; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function setError(int $code, string $message) : RequestHelper_Response |
|
73
|
|
|
{ |
|
74
|
|
|
$this->errorMessage = $message; |
|
75
|
|
|
$this->errorCode = $code; |
|
76
|
|
|
$this->isError = true; |
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function setBody(string $body) : RequestHelper_Response |
|
82
|
|
|
{ |
|
83
|
|
|
$this->body = $body; |
|
84
|
|
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Whether an error occurred in the request. |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
public function isError() : bool |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->isError; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Whether the request timed out. |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
|
|
public function isTimeout() : bool |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->errorCode === RequestHelper_CURL::OPERATION_TIMEDOUT; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Retrieves the native error message, if an error occurred. |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getErrorMessage() : string |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->errorMessage; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Retrieves the native CURL error code, if an error occurred. |
|
116
|
|
|
* @return int |
|
117
|
|
|
* @see RequestHelper_CURL For a list of error codes. |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getErrorCode() : int |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->errorCode; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Retrieves the full body of the request. |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getRequestBody() : string |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->request->getBody(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Retrieves the body of the response, if any. |
|
136
|
|
|
* |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getResponseBody() : string |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->body; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* The response HTTP code. |
|
146
|
|
|
* |
|
147
|
|
|
* @return int The code, or 0 if none was sent (on error). |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getCode() : int |
|
150
|
|
|
{ |
|
151
|
|
|
return intval($this->getInfoKey('http_code')); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Retrieves the actual headers that were sent in the request: |
|
156
|
|
|
* one header by entry in the indexed array. |
|
157
|
|
|
* |
|
158
|
|
|
* @return array |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getHeaders() : array |
|
161
|
|
|
{ |
|
162
|
|
|
return ConvertHelper::explodeTrim("\n", $this->getInfoKey('request_header')); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
protected function getInfoKey(string $name) : string |
|
166
|
|
|
{ |
|
167
|
|
|
if(isset($this->info[$name])) { |
|
168
|
|
|
return (string)$this->info[$name]; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return ''; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|