| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikWP - Libraries |
| 4 |
* @subpackage adapter.application |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* HTTP response data object class. |
| 16 |
* |
| 17 |
* @since 10.1.23 |
| 18 |
*/ |
| 19 |
class JHttpResponse |
| 20 |
{ |
| 21 |
/** |
| 22 |
* The server response code. |
| 23 |
* |
| 24 |
* @var integer |
| 25 |
*/ |
| 26 |
public $code; |
| 27 |
|
| 28 |
/** |
| 29 |
* Response headers. |
| 30 |
* |
| 31 |
* @var array |
| 32 |
*/ |
| 33 |
public $headers = array(); |
| 34 |
|
| 35 |
/** |
| 36 |
* Server response body. |
| 37 |
* |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
public $body; |
| 41 |
|
| 42 |
/** |
| 43 |
* Class constructor. |
| 44 |
* |
| 45 |
* @param mixed $response The response in WP format. |
| 46 |
*/ |
| 47 |
public function __construct($response) |
| 48 |
{ |
| 49 |
// check if we have a response error |
| 50 |
if (is_wp_error($response)) |
| 51 |
{ |
| 52 |
$this->code = (int) $response->get_error_code(); |
| 53 |
$this->body = (string) $response->get_error_message(); |
| 54 |
} |
| 55 |
else |
| 56 |
{ |
| 57 |
// cast response to array |
| 58 |
$response = (array) $response; |
| 59 |
|
| 60 |
// look for an HTTP code |
| 61 |
if (isset($response['response']['code'])) |
| 62 |
{ |
| 63 |
$this->code = (int) $response['response']['code']; |
| 64 |
} |
| 65 |
|
| 66 |
// look for the response body |
| 67 |
if (isset($response['body'])) |
| 68 |
{ |
| 69 |
$this->body = (string) $response['body']; |
| 70 |
} |
| 71 |
else |
| 72 |
{ |
| 73 |
// otherwise use a stringified version of the whole response |
| 74 |
$this->body = print_r($response, true); |
| 75 |
} |
| 76 |
|
| 77 |
// look for the response headers |
| 78 |
if (isset($response['headers'])) |
| 79 |
{ |
| 80 |
// check if we have an array or a traversable object |
| 81 |
if (is_array($response['headers']) || (is_object($response['headers']) && $response['headers'] instanceof Traversable)) |
| 82 |
{ |
| 83 |
// iterate all the headers keys and copy them within the internal property |
| 84 |
foreach ($response['headers'] as $k => $v) |
| 85 |
{ |
| 86 |
$this->headers[$k] = $v; |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|