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
|
|
|
protected $request; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $body = ''; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $info; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
protected $isError = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $errorMessage = ''; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var integer |
46
|
|
|
*/ |
47
|
|
|
protected $errorCode = 0; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param RequestHelper $helper |
51
|
|
|
* @param array $info The CURL info array from curl_getinfo(). |
52
|
|
|
*/ |
53
|
|
|
public function __construct(RequestHelper $helper, array $info) |
54
|
|
|
{ |
55
|
|
|
$this->request = $helper; |
56
|
|
|
$this->info = $info; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setError(int $code, string $message) : RequestHelper_Response |
60
|
|
|
{ |
61
|
|
|
$this->errorMessage = $message; |
62
|
|
|
$this->errorCode = $code; |
63
|
|
|
$this->isError = true; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function setBody(string $body) : RequestHelper_Response |
69
|
|
|
{ |
70
|
|
|
$this->body = $body; |
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Whether an error occurred in the request. |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function isError() : bool |
79
|
|
|
{ |
80
|
|
|
return $this->isError; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Retrieves the native error message, if an error occurred. |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getErrorMessage() : string |
88
|
|
|
{ |
89
|
|
|
return $this->errorMessage; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Retrieves the native error code, if an error occurred. |
94
|
|
|
* @return int |
95
|
|
|
*/ |
96
|
|
|
public function getErrorCode() : int |
97
|
|
|
{ |
98
|
|
|
return $this->errorCode; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Retrieves the full body of the request. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getRequestBody() : string |
108
|
|
|
{ |
109
|
|
|
return $this->request->getBody(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Retrieves the body of the response, if any. |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function getResponseBody() : string |
118
|
|
|
{ |
119
|
|
|
return $this->body; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* The response HTTP code. |
124
|
|
|
* |
125
|
|
|
* @return int The code, or 0 if none was sent (on error). |
126
|
|
|
*/ |
127
|
|
|
public function getCode() : int |
128
|
|
|
{ |
129
|
|
|
return intval($this->getInfoKey('http_code')); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Retrieves the actual headers that were sent in the request: |
134
|
|
|
* one header by entry in the indexed array. |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public function getHeaders() : array |
139
|
|
|
{ |
140
|
|
|
return ConvertHelper::explodeTrim("\n", $this->getInfoKey('request_header')); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function getInfoKey(string $name) : string |
144
|
|
|
{ |
145
|
|
|
if(isset($this->info[$name])) { |
146
|
|
|
return (string)$this->info[$name]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return ''; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|