1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- class RestResponse
- {
-
- private $error;
-
- private $errorMessage;
-
- private $data;
-
- public function __construct()
- {
- $this->error = true;
- $this->errorMessage = null;
- $this->data = [];
- }
-
- public function setData(array $data)
- {
- $this->error = false;
- $this->data = $data;
- }
-
- public function setErrorMessage($message)
- {
- $this->error = true;
- $this->errorMessage = $message;
- }
-
- public function format()
- {
- $json = ['error' => $this->error];
- if ($this->error) {
- $json['message'] = $this->errorMessage;
- } else {
- $json['data'] = $this->data;
- }
- return json_encode($json, JSON_PRETTY_PRINT);
- }
- }
|