-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponse.php
More file actions
596 lines (516 loc) · 14.3 KB
/
Copy pathResponse.php
File metadata and controls
596 lines (516 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
<?php
/**
* Response class file.
*
* @package Mantle
*/
declare(strict_types=1);
namespace Mantle\Http_Client;
use ArrayAccess;
use LogicException;
use Mantle\Contracts\Http\Response as ResponseContract;
use Mantle\Support\Collection;
use Mantle\Support\HTML;
use Mantle\Support\Mixed_Data;
use Mantle\Support\Traits\Macroable;
use Mantle\Testing\Assertable_Json_String;
use SimpleXMLElement;
use WP_Error;
use WP_Http_Cookie;
use WpOrg\Requests\Utility\CaseInsensitiveDictionary;
use function Mantle\Support\Helpers\collect;
use function Mantle\Support\Helpers\data_get;
/**
* Response object from WordPress HTTP API.
*
* @todo Add assertions to the responses.
*
* @phpstan-type CoreResponse array{
* body?: string,
* cookies?: \WP_Http_Cookie[],
* filename?: string|null,
* headers?: \WpOrg\Requests\Utility\CaseInsensitiveDictionary,
* response?: array{
* code: int,
* message: string,
* },
* }
*
* @phpstan-type WpHttpRequestResponse array{
* body?: string,
* cookies?: \WP_Http_Cookie[],
* filename?: string|null,
* headers?: array<string, string>,
* is_wp_error?: bool,
* response?: array{
* code: int,
* message: string,
* },
* http_response?: \WP_HTTP_Requests_Response,
* }
*/
class Response implements ArrayAccess, ResponseContract {
use Concerns\Interacts_With_Feeds;
use Macroable;
/**
* The decoded JSON response.
*
* @var array<string, mixed>|null
*/
protected ?array $decoded = null;
/**
* The decoded XML Element response.
*/
protected ?SimpleXMLElement $element = null;
/**
* Processed response from `wp_remote_request()`.
*
* @var WpHttpRequestResponse
*/
protected array $response;
/**
* The request URL.
*/
protected ?string $url = null;
/**
* Determine if the response was created from the cache.
*
* This property uses a 'mixed' status to prevent a fatal error when upgrading
* projects. In the future, this should be changed to use the Cache_Status
* enum or false.
*
* @param Cache_Status|false $cached Cache status of the response.
*/
public mixed $cached = false;
/**
* Constructor.
*
* @param CoreResponse|WpHttpRequestResponse $response Raw response from `wp_remote_request()`.
*/
public function __construct( array $response ) {
// Serialize the headers from a CaseInsensitiveDictionary to an array.
if ( isset( $response['headers'] ) && $response['headers'] instanceof CaseInsensitiveDictionary ) {
$response['headers'] = $response['headers']->getAll();
}
// Format the headers to be lower-case.
$response['headers'] = array_change_key_case( (array) ( $response['headers'] ?? [] ) );
$this->response = $response;
// @phpstan-ignore instanceof.alwaysTrue
if ( isset( $response['http_response'] ) && $response['http_response'] instanceof \WP_HTTP_Requests_Response ) {
$this->url = $response['http_response']->get_response_object()->url;
} else {
$this->url = null;
}
}
/**
* Create a response object from a `wp_remote_request()` response.
*
* @param CoreResponse|WpHttpRequestResponse|WP_Error $response Raw response from `wp_remote_request()`.
*/
public static function create( array|WP_Error $response ): static {
if ( $response instanceof WP_Error ) {
return static::create_from_wp_error( $response );
}
return new static( $response );
}
/**
* Create a response from a WP_Error object.
*
* @param WP_Error $error WP_Error object.
*/
protected static function create_from_wp_error( WP_Error $error ): static {
return new static(
[
'body' => $error->get_error_message(),
'headers' => [],
'is_wp_error' => true,
'response' => [
'code' => $error->get_error_code() ?: 500,
],
],
);
}
/**
* Retrieve the raw response from `wp_remote_request()`.
*
* @return WpHttpRequestResponse
*/
public function response(): array {
return $this->response;
}
/**
* Retrieve all the headers from a response.
*
* @return array<string, string|string[]> Headers from the response.
*/
public function headers(): array {
return (array) ( $this->response['headers'] ?? [] );
}
/**
* Alias for headers().
*
* @return array<string, string|string[]> Headers from the response.
*/
public function get_headers(): array {
return $this->headers();
}
/**
* Retrieve a specific header (headers are case-insensitive).
*
* Similar to get_header().
*
* In the event of multiple headers with the same name, the first header will
* be returned. If you need all headers, use `get_headers()`.
*
* @param string $header Header to retrieve.
* @return string|null Header value(s), or null if not found.
*/
public function header( string $header ): ?string {
return $this->get_header( $header );
}
/**
* Retrieve a specific response header.
*
* @param string $name The header name to retrieve.
* @param bool $as_array Whether to return the header as an array.
* @return ($as_array is true ? array<string> : string|null) The header value(s), or null if not found.
*/
public function get_header( string $name, bool $as_array = false ): string|array|null {
$header = strtolower( $name );
$headers = $this->headers();
if ( ! array_key_exists( $header, $headers ) ) {
return null;
}
$value = $headers[ $header ];
if ( is_array( $value ) ) {
return $as_array ? $value : $value[0] ?? '';
}
return $as_array ? [ $value ] : $value;
}
/**
* Retrieve the status code for the response.
*/
public function status(): int {
return (int) ( $this->response['response']['code'] ?? 0 );
}
/**
* Retrieve the response status code.
*/
public function get_status_code(): int {
return $this->status();
}
/**
* Determine if the request was successful.
*/
public function successful(): bool {
return $this->status() >= 200 && $this->status() < 300;
}
/**
* Determine if the response code was "OK".
*/
public function ok(): bool {
return $this->status() === 200;
}
/**
* Determine if the response code was not found (404).
*/
public function not_found(): bool {
return $this->status() === 404;
}
/**
* Determine if the response was a redirect.
*/
public function redirect(): bool {
return $this->status() >= 300 && $this->status() < 400;
}
/**
* Determine if the response was a 401 "Unauthorized" response.
*/
public function unauthorized(): bool {
return $this->status() === 401;
}
/**
* Determine if the response was a 403 "Forbidden" response.
*/
public function forbidden(): bool {
return $this->status() === 403;
}
/**
* Determine if the response indicates a client or server error occurred.
*/
public function failed(): bool {
return $this->server_error() || $this->client_error() || $this->is_wp_error();
}
/**
* Determine if the response indicates a client error occurred.
*/
public function client_error(): bool {
return $this->status() >= 400 && $this->status() < 500;
}
/**
* Determine if the response indicates a server error occurred.
*/
public function server_error(): bool {
return $this->status() >= 500;
}
/**
* Check if the error was an WP_Error.
*/
public function is_wp_error(): bool {
return ! empty( $this->response['is_wp_error'] );
}
/**
* Check if the response is HTML.
*/
public function is_html(): bool {
return false !== strpos( (string) $this->header( 'content-type' ), 'text/html' );
}
/**
* Check if the response is JSON.
*/
public function is_json(): bool {
if ( false !== strpos( (string) $this->header( 'content-type' ), 'application/json' ) ) {
return true;
}
return ! empty( $this->json() );
}
/**
* Check if the response is XML. Does not validate if the response is a valid
* XML document.
*/
public function is_xml(): bool {
if ( false !== strpos( (string) $this->header( 'content-type' ), 'application/xml' ) ) {
return true;
}
return str_starts_with( trim( strtolower( $this->body() ) ), '<?xml' );
}
/**
* Check if the response body is a file download (a Binary Large OBject).
*/
public function is_blob(): bool {
return false === mb_detect_encoding( $this->body(), 'UTF-8', true ) && ! ctype_print( $this->body() );
}
/**
* Check if the response is a file download.
*/
public function is_file(): bool {
return ! empty( $this->response['filename'] ) && $this->is_blob();
}
/**
* Get the raw body of the response.
*/
public function body(): string {
return (string) ( $this->response['body'] ?? '' );
}
/**
* Alias for body().
*
* @return string|null The response body.
*/
public function get_body(): ?string {
return $this->body();
}
/**
* Retrieve the file path to the downloaded file.
*/
public function file(): ?string {
return $this->response['filename'] ?? null;
}
/**
* Retrieve the file contents of the downloaded file.
*/
public function file_contents(): ?string {
$file = $this->file();
return empty( $file ) ? null : (string) file_get_contents( $file ); // phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown
}
/**
* Get the JSON decoded body of the response as an array or scalar value.
*
* @param string|null $key
* @param mixed $default
* @return mixed
*/
public function json( ?string $key = null, mixed $default = null ) {
$this->decoded ??= json_decode( $this->body(), true );
if ( is_null( $key ) ) {
return $this->decoded;
}
return data_get( $this->decoded, $key, $default );
}
/**
* Get the JSON decoded body of the response as a Mixed_Data instance.
*
* @param string|null $key
* @param mixed $default
*/
public function mixed_json( ?string $key = null, mixed $default = null ): Mixed_Data {
return Mixed_Data::of( $this->json( $key, $default ) );
}
/**
* Retrieve an instance of Assertable_Json_String to perform fluent JSON assertions.
*
* @param string|null $key Optional key to pass to `json()` to scope the JSON data.
*/
public function assertable_json( ?string $key = null ): Assertable_Json_String {
return new Assertable_Json_String( $this->json( $key ) );
}
/**
* Get the body of the response as an HTML object.
*/
public function html(): HTML {
return new HTML( $this->body() );
}
/**
* Get the XML body of the response.
*
* @template TDefault
*
* @param string|null $xpath Path to pass to `SimpleXMLElement::xpath()`, optional.
* @param mixed $default Default value to return if the path does not exist.
* @return SimpleXMLElement|string|null Returns a specific SimpleXMLElement if path is specified, otherwise the entire document.
*
* @phpstan-param TDefault $default
* @phpstan-return ($xpath is null ? SimpleXMLElement : (array<SimpleXMLElement>|TDefault))
*/
public function xml( ?string $xpath = null, mixed $default = null ) {
if ( ! $this->element instanceof \SimpleXMLElement ) {
$previous = libxml_use_internal_errors( true );
$this->element = new SimpleXMLElement( $this->body() );
// Restore the former error level.
libxml_use_internal_errors( $previous );
}
if ( ! $xpath ) {
return $this->element;
}
return $this->element->xpath( $xpath ) ?: $default;
}
/**
* Get the JSON decoded body of the response as an object.
*
* @return object
*/
public function object(): mixed {
return json_decode( $this->body(), false );
}
/**
* Get the JSON decoded body of the response as a collection.
*
* @param string|null $key
* @return Collection<array-key, mixed>
*/
public function collect( ?string $key = null ): \Mantle\Support\Collection {
return new Collection( $this->json( $key ) );
}
/**
* Retrieve the cookies from the response.
*
* @return WP_Http_Cookie[]
*/
public function cookies(): array {
return $this->response['cookies'] ?? [];
}
/**
* Retrieve a specific cookie by name.
*
* @param string $name Cookie name.
*/
public function cookie( string $name ): ?WP_Http_Cookie {
return collect( $this->cookies() )
->key_by( 'name' )
->get( $name );
}
/**
* Dump the response to the screen.
*/
public function dump(): static {
dump( $this->response );
return $this;
}
/**
* Dump the response to the screen and exit.
*/
public function dd(): never {
$this->dump();
exit( 1 );
}
/**
* Check if an attribute exists on the response.
*
* @param mixed $offset Offset to check.
*/
public function offsetExists( mixed $offset ): bool {
if ( $this->is_xml() ) {
return isset( $this->xml()[ $offset ] );
}
return isset( $this->json()[ $offset ] );
}
/**
* Retrieve an attribute from the response.
*
* @param mixed $offset Offset to get.
*/
public function offsetGet( mixed $offset ): mixed {
if ( is_null( $offset ) ) {
return null;
}
if ( $this->is_xml() ) {
return $this->xml()->{ $offset };
}
return $this->json()[ $offset ];
}
/**
* Set an attribute on the response.
*
* @throws LogicException Not supported on responses.
*
* @param mixed $offset Offset.
* @param mixed $value Value.
*/
public function offsetSet( mixed $offset, mixed $value ): void {
throw new LogicException( 'Response values are read-only.' );
}
/**
* Remove an attribute from the response.
*
* @throws LogicException Not supported on responses.
* @param mixed $offset Offset.
*/
public function offsetUnset( mixed $offset ): void {
throw new LogicException( 'Response values are read-only.' );
}
/**
* Prepare the object for serialization.
*
* @return array<string, mixed>
*/
public function __serialize(): array {
// Purge some data from the response for lighter serialization.
unset( $this->response['http_response'] );
foreach ( [ 'cookies', 'filename', 'headers' ] as $key ) {
if ( empty( $this->response[ $key ] ) ) {
unset( $this->response[ $key ] );
}
}
return [
'url' => $this->url,
'response' => $this->response,
];
}
/**
* Restore the object from serialized data.
*
* @throws LogicException If the serialized data is invalid.
*
* @param array<string, mixed> $data Serialized data.
*/
public function __unserialize( array $data ): void {
if ( ! isset( $data['response'] ) || ! is_array( $data['response'] ) ) {
throw new LogicException( 'Invalid serialized response data.' );
}
if ( isset( $data['url'] ) ) {
$this->url = is_string( $data['url'] ) ? $data['url'] : null;
}
$this->response = $data['response'];
$this->cached = true;
}
}