| 1 |
<?php |
| 2 |
|
| 3 |
class WooMailerLiteApiResponse |
| 4 |
{ |
| 5 |
public $status = 200; |
| 6 |
public $success = true; |
| 7 |
public $data = null; |
| 8 |
public $message = ''; |
| 9 |
public $links = null; |
| 10 |
public function __construct($response, $status = null) |
| 11 |
{ |
| 12 |
$this->generateResponse($response, $status); |
| 13 |
} |
| 14 |
|
| 15 |
public function generateResponse($response) |
| 16 |
{ |
| 17 |
$this->status = wp_remote_retrieve_response_code($response); |
| 18 |
$this->data = json_decode(wp_remote_retrieve_body($response)) ?? null; |
| 19 |
if (isset($this->data->links)) { |
| 20 |
$this->links = $this->data->links; |
| 21 |
} |
| 22 |
if (isset($this->data->data)) { |
| 23 |
$this->data = $this->data->data; |
| 24 |
} |
| 25 |
if (isset($this->data->errors)) { |
| 26 |
$this->success = false; |
| 27 |
$this->status = 400; |
| 28 |
$this->message = $this->data->errors; |
| 29 |
} |
| 30 |
if (isset($response->errors) || $this->status >= 400) { |
| 31 |
$this->success = false; |
| 32 |
$this->data = $response->errors ?? $this->data; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
public function setResponse($response, $status = 200) |
| 37 |
{ |
| 38 |
$this->data = $response; |
| 39 |
$this->status = $status; |
| 40 |
return $this; |
| 41 |
} |
| 42 |
|
| 43 |
public function addData($key, $value) |
| 44 |
{ |
| 45 |
if (isset($this->data)) { |
| 46 |
$this->data->$key = $value; |
| 47 |
} |
| 48 |
return $this; |
| 49 |
} |
| 50 |
} |