| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Core\Utils\Api; |
| 4 |
|
| 5 |
class Response_Builder { |
| 6 |
private $data; |
| 7 |
private int $status; |
| 8 |
private array $meta = []; |
| 9 |
|
| 10 |
const NO_CONTENT = 204; |
| 11 |
|
| 12 |
private function __construct( $data, $status ) { |
| 13 |
$this->data = $data; |
| 14 |
$this->status = $status; |
| 15 |
} |
| 16 |
|
| 17 |
public static function make( $data = null, $status = 200 ) { |
| 18 |
return new self( $data, $status ); |
| 19 |
} |
| 20 |
|
| 21 |
public function set_meta( array $meta ) { |
| 22 |
$this->meta = $meta; |
| 23 |
|
| 24 |
return $this; |
| 25 |
} |
| 26 |
|
| 27 |
public function set_status( int $status ) { |
| 28 |
$this->status = $status; |
| 29 |
|
| 30 |
return $this; |
| 31 |
} |
| 32 |
|
| 33 |
public function no_content() { |
| 34 |
return $this->set_status( static::NO_CONTENT ); |
| 35 |
} |
| 36 |
|
| 37 |
public function build() { |
| 38 |
$res_data = static::NO_CONTENT === $this->status |
| 39 |
? null |
| 40 |
: [ |
| 41 |
'data' => $this->data, |
| 42 |
'meta' => $this->meta, |
| 43 |
]; |
| 44 |
|
| 45 |
return new \WP_REST_Response( $res_data, $this->status ); |
| 46 |
} |
| 47 |
} |
| 48 |
|