@@ -43,25 +43,26 @@ class Response
43
43
44
44
/**
45
45
* @param string|array|null $data
46
- * @param bool $expectInvalid
46
+ * @param bool $expectInvalid
47
47
*/
48
48
public function __construct ($ data = null , $ expectInvalid = false )
49
49
{
50
- if (! is_array ($ data )) {
50
+ if ( ! is_array ($ data )) {
51
51
$ data = [
52
- 'response ' => $ data ,
52
+ 'response ' => $ data ,
53
53
'err_response ' => null ,
54
54
'out_response ' => $ data ,
55
55
];
56
56
}
57
57
58
- $ this ->data = $ data ;
58
+ $ this ->data = $ data ;
59
59
$ this ->expectInvalid = $ expectInvalid ;
60
60
}
61
61
62
62
/**
63
- * @param mixed $body
63
+ * @param mixed $body
64
64
* @param string $contentType
65
+ *
65
66
* @return Response
66
67
*/
67
68
public function expectBody ($ body , $ contentType = 'text/html ' )
@@ -99,22 +100,28 @@ class Response
99
100
}
100
101
101
102
/**
102
- * @param string $name
103
- * @param string $value
103
+ * Expect header in the response.
104
+ *
105
+ * @param string $name Header name.
106
+ * @param string $value Header value.
107
+ *
104
108
* @return Response
105
109
*/
106
- public function expectHeader ($ name , $ value )
110
+ public function expectHeader ($ name , $ value ): Response
107
111
{
108
112
$ this ->checkHeader ($ name , $ value );
109
113
110
114
return $ this ;
111
115
}
112
116
113
117
/**
114
- * @param string|null $errorMessage
118
+ * Expect error in the response.
119
+ *
120
+ * @param string|null $errorMessage Expected error message.
121
+ *
115
122
* @return Response
116
123
*/
117
- public function expectError ($ errorMessage )
124
+ public function expectError ($ errorMessage ): Response
118
125
{
119
126
$ errorData = $ this ->getErrorData ();
120
127
if ($ errorData !== $ errorMessage ) {
@@ -128,19 +135,23 @@ class Response
128
135
}
129
136
130
137
/**
131
- * @param string $errorMessage
138
+ * Expect no error in the response.
139
+ *
132
140
* @return Response
133
141
*/
134
- public function expectNoError ()
142
+ public function expectNoError (): Response
135
143
{
136
144
return $ this ->expectError (null );
137
145
}
138
146
139
147
/**
140
- * @param string $contentType
148
+ * Get response body.
149
+ *
150
+ * @param string $contentType Expect body to have specified content type.
151
+ *
141
152
* @return string|null
142
153
*/
143
- public function getBody ($ contentType = 'text/html ' )
154
+ public function getBody (string $ contentType = 'text/html ' ): ? string
144
155
{
145
156
if ($ this ->checkIfValid () && $ this ->checkDefaultHeaders ($ contentType )) {
146
157
return $ this ->rawBody ;
@@ -150,15 +161,15 @@ class Response
150
161
}
151
162
152
163
/**
153
- * Print raw body
164
+ * Print raw body.
154
165
*/
155
166
public function dumpBody ()
156
167
{
157
168
var_dump ($ this ->getBody ());
158
169
}
159
170
160
171
/**
161
- * Print raw body
172
+ * Print raw body.
162
173
*/
163
174
public function printBody ()
164
175
{
@@ -181,7 +192,7 @@ class Response
181
192
/**
182
193
* @return string|null
183
194
*/
184
- public function getErrorData ()
195
+ public function getErrorData (): ? string
185
196
{
186
197
return $ this ->data ['err_response ' ];
187
198
}
@@ -191,24 +202,27 @@ class Response
191
202
*
192
203
* @return bool
193
204
*/
194
- private function checkIfValid ()
205
+ private function checkIfValid (): bool
195
206
{
196
207
if ($ this ->isValid ()) {
197
208
return true ;
198
209
}
199
210
200
- if (! $ this ->expectInvalid ) {
211
+ if ( ! $ this ->expectInvalid ) {
201
212
$ this ->error ("The response is invalid: $ this ->rawData " );
202
213
}
203
214
204
215
return false ;
205
216
}
206
217
207
218
/**
219
+ * Check default headers that should be present.
220
+ *
208
221
* @param string $contentType
222
+ *
209
223
* @return bool
210
224
*/
211
- private function checkDefaultHeaders ($ contentType )
225
+ private function checkDefaultHeaders ($ contentType ): bool
212
226
{
213
227
// check default headers
214
228
return (
@@ -218,40 +232,46 @@ class Response
218
232
}
219
233
220
234
/**
221
- * @param string $name
222
- * @param string $value
223
- * @param bool $useRegex
235
+ * Check a specified header.
236
+ *
237
+ * @param string $name Header name.
238
+ * @param string $value Header value.
239
+ * @param bool $useRegex Whether value is regular expression.
240
+ *
224
241
* @return bool
225
242
*/
226
- private function checkHeader (string $ name , string $ value , $ useRegex = false )
243
+ private function checkHeader (string $ name , string $ value , $ useRegex = false ): bool
227
244
{
228
- $ lcName = strtolower ($ name );
245
+ $ lcName = strtolower ($ name );
229
246
$ headers = $ this ->getHeaders ();
230
- if (! isset ($ headers [$ lcName ])) {
247
+ if ( ! isset ($ headers [$ lcName ])) {
231
248
return $ this ->error ("The header $ name is not present " );
232
249
}
233
250
$ header = $ headers [$ lcName ];
234
251
235
- if (! $ useRegex ) {
252
+ if ( ! $ useRegex ) {
236
253
if ($ header === $ value ) {
237
254
return true ;
238
255
}
256
+
239
257
return $ this ->error ("The header $ name value ' $ header' is not the same as ' $ value' " );
240
258
}
241
259
242
- if (! preg_match ($ value , $ header )) {
260
+ if ( ! preg_match ($ value , $ header )) {
243
261
return $ this ->error ("The header $ name value ' $ header' does not match RegExp ' $ value' " );
244
262
}
245
263
246
264
return true ;
247
265
}
248
266
249
267
/**
268
+ * Get all headers.
269
+ *
250
270
* @return array|null
251
271
*/
252
- private function getHeaders ()
272
+ private function getHeaders (): ? array
253
273
{
254
- if (! $ this ->isValid ()) {
274
+ if ( ! $ this ->isValid ()) {
255
275
return null ;
256
276
}
257
277
@@ -260,7 +280,7 @@ class Response
260
280
}
261
281
262
282
$ headerRows = explode ("\r\n" , $ this ->rawHeaders );
263
- $ headers = [];
283
+ $ headers = [];
264
284
foreach ($ headerRows as $ headerRow ) {
265
285
$ colonPosition = strpos ($ headerRow , ': ' );
266
286
if ($ colonPosition === false ) {
@@ -292,8 +312,8 @@ class Response
292
312
private function processData ()
293
313
{
294
314
$ this ->rawData = $ this ->data ['out_response ' ];
295
- $ this ->valid = (
296
- !is_null ($ this ->rawData ) &&
315
+ $ this ->valid = (
316
+ ! is_null ($ this ->rawData ) &&
297
317
strpos ($ this ->rawData , self ::HEADER_SEPARATOR )
298
318
);
299
319
if ($ this ->valid ) {
@@ -308,9 +328,10 @@ class Response
308
328
* Emit error message
309
329
*
310
330
* @param string $message
331
+ *
311
332
* @return bool
312
333
*/
313
- private function error ($ message )
334
+ private function error ($ message ): bool
314
335
{
315
336
echo "ERROR: $ message \n" ;
316
337
0 commit comments