| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\App\Modules\ImportExportCustomization\Data; |
| 4 |
|
| 5 |
class Response { |
| 6 |
private array $data; |
| 7 |
private array $meta; |
| 8 |
|
| 9 |
public function __construct( array $data, array $meta = [] ) { |
| 10 |
$this->data = $data; |
| 11 |
$this->meta = $meta; |
| 12 |
} |
| 13 |
|
| 14 |
public static function success( array $data, array $meta = [] ): \WP_REST_Response { |
| 15 |
$response = new self( $data, $meta ); |
| 16 |
return $response->to_wp_rest_response( 200 ); |
| 17 |
} |
| 18 |
|
| 19 |
public static function error( string $code, $message, array $meta = [] ): \WP_REST_Response { |
| 20 |
$response = new self([ |
| 21 |
'code' => $code, |
| 22 |
'message' => $message, |
| 23 |
], $meta); |
| 24 |
|
| 25 |
return $response->to_wp_rest_response( 500 ); |
| 26 |
} |
| 27 |
|
| 28 |
private function to_array(): array { |
| 29 |
return [ |
| 30 |
'data' => $this->data, |
| 31 |
'meta' => $this->meta, |
| 32 |
]; |
| 33 |
} |
| 34 |
|
| 35 |
private function to_wp_rest_response( int $status_code = 200 ): \WP_REST_Response { |
| 36 |
return new \WP_REST_Response( $this->to_array(), $status_code ); |
| 37 |
} |
| 38 |
} |
| 39 |
|