| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\Framework\Http\Response; |
| 4 |
|
| 5 |
use WP_Error; |
| 6 |
use WP_REST_Response; |
| 7 |
|
| 8 |
class Response |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Response Data |
| 12 |
* @var mixed |
| 13 |
*/ |
| 14 |
protected $data = null; |
| 15 |
|
| 16 |
/** |
| 17 |
* Response Status Code |
| 18 |
* @var integer |
| 19 |
*/ |
| 20 |
protected $code = 200; |
| 21 |
|
| 22 |
/** |
| 23 |
* Response Headers |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
protected $headers = []; |
| 27 |
|
| 28 |
/** |
| 29 |
* Response Cookies |
| 30 |
* @var array |
| 31 |
*/ |
| 32 |
protected $cookies = []; |
| 33 |
|
| 34 |
/** |
| 35 |
* Construct the response instance |
| 36 |
* |
| 37 |
* @param mixed $data |
| 38 |
*/ |
| 39 |
public function __construct($data = null, $code = 200) |
| 40 |
{ |
| 41 |
$this->data = $data; |
| 42 |
$this->code = $code; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Creates an instance of self |
| 47 |
* |
| 48 |
* @param mixed $data |
| 49 |
* @param integer $code |
| 50 |
* @return self |
| 51 |
*/ |
| 52 |
public static function make($data = null, $code = 200) |
| 53 |
{ |
| 54 |
return new static($data, $code = 200); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Send json response |
| 59 |
* @param array $data |
| 60 |
* @param integer $code |
| 61 |
* @return string|false The JSON encoded string, or false if it cannot be encoded. |
| 62 |
*/ |
| 63 |
public function json($data = null, $code = 200) |
| 64 |
{ |
| 65 |
return wp_send_json($data, $code); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Send json response |
| 70 |
* @param array $data |
| 71 |
* @param integer $code |
| 72 |
* @return \WP_REST_Response |
| 73 |
*/ |
| 74 |
public function send($data = null, $code = 200, $headers = []) |
| 75 |
{ |
| 76 |
return new WP_REST_Response($data, $code, $headers); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Send a success json response |
| 81 |
* @param array $data |
| 82 |
* @param integer $code |
| 83 |
* @return \WP_REST_Response |
| 84 |
*/ |
| 85 |
public function sendSuccess($data = null, $code = 200, $headers = []) |
| 86 |
{ |
| 87 |
return new WP_REST_Response($data, $code, $headers); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Send an error json response |
| 92 |
* @param array $data |
| 93 |
* @param integer $code |
| 94 |
* @return \WP_REST_Response |
| 95 |
*/ |
| 96 |
public function sendError($data = null, $code = 422, $headers = []) |
| 97 |
{ |
| 98 |
if (!$code || $code < 400 ) { |
| 99 |
$code = 422; |
| 100 |
} |
| 101 |
|
| 102 |
return new WP_REST_Response($data, $code, $headers); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Convert the WP_Error to WP_REST_Response |
| 107 |
* |
| 108 |
* @param \WP_Error $wpError |
| 109 |
* @return \WP_REST_Response |
| 110 |
*/ |
| 111 |
public function wpErrorToResponse(WP_Error $wpError) |
| 112 |
{ |
| 113 |
return rest_convert_error_to_response($wpError); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Set response headers |
| 118 |
* @param string|array $key |
| 119 |
* @param string|null $value |
| 120 |
* @return self |
| 121 |
*/ |
| 122 |
public function withHeader($key, $value = null) |
| 123 |
{ |
| 124 |
if (is_array($key) && !$value) { |
| 125 |
$this->headers = $key; |
| 126 |
} else { |
| 127 |
$this->headers = [$key => $value]; |
| 128 |
} |
| 129 |
|
| 130 |
return $this; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Set response cookie |
| 135 |
* |
| 136 |
* @param string $name |
| 137 |
* @param mixed $value |
| 138 |
* @param int $minutes |
| 139 |
* @param string $path |
| 140 |
* @param string|null $domain |
| 141 |
* @param bool $secure |
| 142 |
* @param bool $httpOnly |
| 143 |
* @return self |
| 144 |
*/ |
| 145 |
public function withCookie( |
| 146 |
$name, |
| 147 |
$value, |
| 148 |
$minutes, |
| 149 |
$path = '/', |
| 150 |
$domain = null, |
| 151 |
$secure = false, |
| 152 |
$httpOnly = true |
| 153 |
) |
| 154 |
{ |
| 155 |
$cookie = $this->buildCookie( |
| 156 |
$name, $value, $minutes, $path, $domain, $secure, $httpOnly |
| 157 |
); |
| 158 |
|
| 159 |
$this->cookies[] = $cookie; |
| 160 |
|
| 161 |
return $this; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Build cookie header for response |
| 166 |
* |
| 167 |
* @param string $name |
| 168 |
* @param mixed $value |
| 169 |
* @param int $minutes |
| 170 |
* @param string $path |
| 171 |
* @param string|null $domain |
| 172 |
* @param bool $secure |
| 173 |
* @param bool $httpOnly |
| 174 |
* @return string |
| 175 |
*/ |
| 176 |
protected function buildCookie($name, $value, $minutes, $path, $domain, $secure, $httpOnly) |
| 177 |
{ |
| 178 |
$expiration = time() + ($minutes * 60); |
| 179 |
|
| 180 |
$serializedValue = base64_encode(json_encode($value)); |
| 181 |
|
| 182 |
$cookieHeader = "{$name}=" . rawurlencode($serializedValue); |
| 183 |
|
| 184 |
$cookieHeader .= "; Expires=" . gmdate('D, d-M-Y H:i:s T', $expiration); |
| 185 |
|
| 186 |
$cookieHeader .= "; Path=" . $path; |
| 187 |
|
| 188 |
if ($domain) { |
| 189 |
$cookieHeader .= "; Domain=" . $domain; |
| 190 |
} |
| 191 |
|
| 192 |
if ($secure) { |
| 193 |
$cookieHeader .= "; Secure"; |
| 194 |
} |
| 195 |
|
| 196 |
if ($httpOnly) { |
| 197 |
$cookieHeader .= "; HttpOnly"; |
| 198 |
} |
| 199 |
|
| 200 |
return $cookieHeader; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Send the response |
| 205 |
* |
| 206 |
* @return \WP_REST_Response |
| 207 |
*/ |
| 208 |
public function toArray() |
| 209 |
{ |
| 210 |
$response = new WP_REST_Response( |
| 211 |
$this->data, $this->code, $this->headers |
| 212 |
); |
| 213 |
|
| 214 |
foreach ($this->cookies as $cookie) { |
| 215 |
$response->header('Set-Cookie', $cookie); |
| 216 |
} |
| 217 |
|
| 218 |
return $response; |
| 219 |
} |
| 220 |
} |
| 221 |
|