24
24
* @property-read UriInterface $url
25
25
* @SuppressWarnings("UnusedPrivateMethod")
26
26
* @SuppressWarnings("TooManyPublicMethods")
27
+ * @SuppressWarnings("TooManyMethods")
28
+ * @SuppressWarnings("ExcessiveClassComplexity")
27
29
*/
28
30
class Response implements ResponseInterface {
29
31
use Message;
@@ -196,11 +198,7 @@ public function endDeferredResponse(?string $integrity = null):void {
196
198
public function arrayBuffer ():Promise {
197
199
$ promise = $ this ->getPromise ();
198
200
$ promise ->then (function (string $ responseText ) {
199
- $ bytes = strlen ($ responseText );
200
- $ arrayBuffer = new ArrayBuffer ($ bytes );
201
- for ($ i = 0 ; $ i < $ bytes ; $ i ++) {
202
- $ arrayBuffer ->offsetSet ($ i , ord ($ responseText [$ i ]));
203
- }
201
+ $ arrayBuffer = $ this ->arrayBufferFromResponseText ($ responseText );
204
202
205
203
$ this ->deferred ->resolve ($ arrayBuffer );
206
204
});
@@ -210,12 +208,8 @@ public function arrayBuffer():Promise {
210
208
211
209
public function awaitArrayBuffer ():ArrayBuffer {
212
210
$ arrayBuffer = null ;
213
- if ($ bodyText = $ this ->getBody ()->getContents ()) {
214
- $ bytes = strlen ($ bodyText );
215
- $ arrayBuffer = new ArrayBuffer ($ bytes );
216
- for ($ i = 0 ; $ i < $ bytes ; $ i ++) {
217
- $ arrayBuffer ->offsetSet ($ i , ord ($ bodyText [$ i ]));
218
- }
211
+ if ($ responseText = $ this ->getBody ()->getContents ()) {
212
+ $ arrayBuffer = $ this ->arrayBufferFromResponseText ($ responseText );
219
213
}
220
214
221
215
$ this ->arrayBuffer ()->then (function (ArrayBuffer $ resolved ) use (&$ arrayBuffer ) {
@@ -225,6 +219,14 @@ public function awaitArrayBuffer():ArrayBuffer {
225
219
return $ arrayBuffer ;
226
220
}
227
221
222
+ private function arrayBufferFromResponseText (string $ responseText ):ArrayBuffer {
223
+ $ bytes = strlen ($ responseText );
224
+ $ arrayBuffer = new ArrayBuffer ($ bytes );
225
+ for ($ i = 0 ; $ i < $ bytes ; $ i ++) {
226
+ $ arrayBuffer ->offsetSet ($ i , ord ($ responseText [$ i ]));
227
+ }
228
+ }
229
+
228
230
/**
229
231
* Takes the Response's stream and reads it to completion. Returns a Promise which resolves with the result
230
232
* as a Gt\Http\Blob.
@@ -235,22 +237,17 @@ public function awaitArrayBuffer():ArrayBuffer {
235
237
public function blob ():Promise {
236
238
$ promise = $ this ->getPromise ();
237
239
$ promise ->then (function (string $ responseText ) {
238
- $ blobOptions = [
239
- "type " => $ this ->getResponseHeaders ()->get ("content-type " )?->getValues()[0 ],
240
- ];
241
- $ this ->deferred ->resolve (new Blob ([$ responseText ], $ blobOptions ));
240
+ $ blob = $ this ->blobFromResponseText ($ responseText );
241
+ $ this ->deferred ->resolve ($ blob );
242
242
});
243
243
244
244
return $ promise ;
245
245
}
246
246
247
247
public function awaitBlob ():Blob {
248
248
$ blob = null ;
249
- if ($ bodyText = $ this ->getBody ()->getContents ()) {
250
- $ blobOptions = [
251
- "type " => $ this ->getResponseHeaders ()->get ("content-type " )?->getValues()[0 ],
252
- ];
253
- $ blob = new Blob ([$ bodyText ], $ blobOptions );
249
+ if ($ responseText = $ this ->getBody ()->getContents ()) {
250
+ $ blob = $ this ->blobFromResponseText ($ responseText );
254
251
}
255
252
256
253
$ this ->blob ()->then (function (Blob $ resolved ) use (&$ blob ) {
@@ -260,6 +257,13 @@ public function awaitBlob():Blob {
260
257
return $ blob ;
261
258
}
262
259
260
+ private function blobFromResponseText (string $ responseText ):Blob {
261
+ $ blobOptions = [
262
+ "type " => $ this ->getResponseHeaders ()->get ("content-type " )?->getValues()[0 ],
263
+ ];
264
+ return new Blob ([$ responseText ], $ blobOptions );
265
+ }
266
+
263
267
/**
264
268
* Takes the Response's stream and reads it to completion. Returns a Promise which resolves with the result
265
269
* as a Gt\Http\FormData.
@@ -272,16 +276,9 @@ public function formData():Promise {
272
276
$ newPromise = $ newDeferred ->getPromise ();
273
277
274
278
$ deferredPromise = $ this ->getPromise ();
275
- $ deferredPromise ->then (function (string $ resolvedValue )
279
+ $ deferredPromise ->then (function (string $ responseText )
276
280
use ($ newDeferred ) {
277
- parse_str ($ resolvedValue , $ bodyData );
278
- $ formData = new FormData ();
279
- foreach ($ bodyData as $ key => $ value ) {
280
- if (is_array ($ value )) {
281
- $ value = implode (", " , $ value );
282
- }
283
- $ formData ->set ((string )$ key , (string )$ value );
284
- }
281
+ $ formData = $ this ->formDataFromResponseText ($ responseText );
285
282
$ newDeferred ->resolve ($ formData );
286
283
});
287
284
@@ -290,15 +287,8 @@ public function formData():Promise {
290
287
291
288
public function awaitFormData ():FormData {
292
289
$ formData = null ;
293
- if ($ bodyText = $ this ->getBody ()->getContents ()) {
294
- parse_str ($ bodyText , $ bodyData );
295
- $ formData = new FormData ();
296
- foreach ($ bodyData as $ key => $ value ) {
297
- if (is_array ($ value )) {
298
- $ value = implode (", " , $ value );
299
- }
300
- $ formData ->set ((string )$ key , (string )$ value );
301
- }
290
+ if ($ responseText = $ this ->getBody ()->getContents ()) {
291
+ $ formData = $ this ->formDataFromResponseText ($ responseText );
302
292
}
303
293
304
294
$ this ->blob ()->then (function (FormData $ resolved ) use (&$ formData ) {
@@ -308,6 +298,18 @@ public function awaitFormData():FormData {
308
298
return $ formData ;
309
299
}
310
300
301
+ private function formDataFromResponseText (string $ responseText ):FormData {
302
+ parse_str ($ responseText , $ bodyData );
303
+ $ formData = new FormData ();
304
+ foreach ($ bodyData as $ key => $ value ) {
305
+ if (is_array ($ value )) {
306
+ $ value = implode (", " , $ value );
307
+ }
308
+ $ formData ->set ((string )$ key , (string )$ value );
309
+ }
310
+ return $ formData ;
311
+ }
312
+
311
313
/**
312
314
* Takes the Response's stream and reads it to completion. Returns a Promise which resolves with the result
313
315
* as a Gt\Json\JsonObject.
@@ -320,8 +322,7 @@ public function awaitFormData():FormData {
320
322
public function json (int $ depth = 512 , int $ options = 0 ):Promise {
321
323
$ promise = $ this ->getPromise ();
322
324
$ promise ->then (function (string $ responseText )use ($ depth , $ options ) {
323
- $ builder = new JsonObjectBuilder ($ depth , $ options );
324
- $ json = $ builder ->fromJsonString ($ responseText );
325
+ $ json = $ this ->jsonFromResponseText ($ responseText , $ depth , $ options );
325
326
$ this ->deferred ->resolve ($ json );
326
327
});
327
328
@@ -331,9 +332,8 @@ public function json(int $depth = 512, int $options = 0):Promise {
331
332
/** @param int<1, max> $depth */
332
333
public function awaitJson (int $ depth = 512 , int $ options = 0 ):JsonObject {
333
334
$ jsonObject = null ;
334
- if ($ bodyText = $ this ->getBody ()->getContents ()) {
335
- $ builder = new JsonObjectBuilder ();
336
- $ jsonObject = $ builder ->fromJsonString ($ bodyText );
335
+ if ($ responseText = $ this ->getBody ()->getContents ()) {
336
+ $ jsonObject = $ this ->jsonFromResponseText ($ responseText );
337
337
}
338
338
339
339
$ this ->json ($ depth , $ options )->then (function (JsonObject $ resolved ) use (&$ jsonObject ) {
@@ -343,6 +343,11 @@ public function awaitJson(int $depth = 512, int $options = 0):JsonObject {
343
343
return $ jsonObject ;
344
344
}
345
345
346
+ private function jsonFromResponseText (string $ responseText , int $ depth = 512 , int $ options = 0 ):JsonObject {
347
+ $ builder = new JsonObjectBuilder ($ depth , $ options );
348
+ return $ builder ->fromJsonString ($ responseText );
349
+ }
350
+
346
351
/**
347
352
* Takes the Response's stream and reads it to completion. Returns a Promise which resolves with the result
348
353
* as a string.
0 commit comments