Client
1 week ago
Concerns
1 week ago
Cookie.php
1 week ago
JsonResponse.php
1 week ago
RedirectResponse.php
1 week ago
Request.php
1 week ago
Response.php
1 week ago
Response.php
161 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Handles standardized REST API responses for the plugin. |
| 5 | * |
| 6 | * @package Framework |
| 7 | * @subpackage Http |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | namespace Kirki\Framework\Http; |
| 11 | |
| 12 | \defined('ABSPATH') || exit; |
| 13 | use Kirki\Framework\Http\Concerns\InteractsWithCookies; |
| 14 | class Response |
| 15 | { |
| 16 | use InteractsWithCookies; |
| 17 | /** |
| 18 | * HTTP status code for a successful request. |
| 19 | * |
| 20 | * @since 1.0.0 |
| 21 | * @var int |
| 22 | */ |
| 23 | public const OK = 200; |
| 24 | /** |
| 25 | * HTTP status code for a successfully created resource. |
| 26 | * |
| 27 | * @since 1.0.0 |
| 28 | * @var int |
| 29 | */ |
| 30 | public const CREATED = 201; |
| 31 | /** |
| 32 | * HTTP status code for a successful request with no content. |
| 33 | * |
| 34 | * @since 1.0.0 |
| 35 | * @var int |
| 36 | */ |
| 37 | public const NO_CONTENT = 204; |
| 38 | /** |
| 39 | * HTTP status code for a multi-status response. |
| 40 | * |
| 41 | * @since 1.0.0 |
| 42 | * @var int |
| 43 | */ |
| 44 | public const MULTI_STATUS = 207; |
| 45 | /** |
| 46 | * HTTP status code for a bad request. |
| 47 | * |
| 48 | * @since 1.0.0 |
| 49 | * @var int |
| 50 | */ |
| 51 | public const BAD_REQUEST = 400; |
| 52 | /** |
| 53 | * HTTP status code for an unauthorized request. |
| 54 | * |
| 55 | * @since 1.0.0 |
| 56 | * @var int |
| 57 | */ |
| 58 | public const UNAUTHORIZED = 401; |
| 59 | /** |
| 60 | * HTTP status code when authentication is required and has failed or has not yet been provided. |
| 61 | * |
| 62 | * @since 1.0.0 |
| 63 | * @var int |
| 64 | */ |
| 65 | public const FORBIDDEN = 403; |
| 66 | /** |
| 67 | * HTTP status code when a requested resource is not found. |
| 68 | * |
| 69 | * @since 1.0.0 |
| 70 | * @var int |
| 71 | */ |
| 72 | public const NOT_FOUND = 404; |
| 73 | /** |
| 74 | * HTTP status code when a request method is not allowed. |
| 75 | * |
| 76 | * @since 1.0.0 |
| 77 | * @var int |
| 78 | */ |
| 79 | public const METHOD_NOT_ALLOWED = 405; |
| 80 | /** |
| 81 | * HTTP status code when a conflict occurs. |
| 82 | * |
| 83 | * @since 1.0.0 |
| 84 | * @var int |
| 85 | */ |
| 86 | public const CONFLICT = 409; |
| 87 | /** |
| 88 | * HTTP status code when a request is unprocessable. |
| 89 | * |
| 90 | * @since 1.0.0 |
| 91 | * @var int |
| 92 | */ |
| 93 | public const UNPROCESSABLE_ENTITY = 422; |
| 94 | /** |
| 95 | * HTTP status code for too many requests (rate limiting). |
| 96 | * |
| 97 | * @since 1.0.0 |
| 98 | * @var int |
| 99 | */ |
| 100 | public const TOO_MANY_REQUESTS = 429; |
| 101 | /** |
| 102 | * HTTP status code for internal server error. |
| 103 | * |
| 104 | * @since 1.0.0 |
| 105 | * @var int |
| 106 | */ |
| 107 | public const INTERNAL_SERVER_ERROR = 500; |
| 108 | /** |
| 109 | * HTTP status code for not implemented. |
| 110 | * |
| 111 | * @since 1.0.0 |
| 112 | * @var int |
| 113 | */ |
| 114 | public const NOT_IMPLEMENTED = 501; |
| 115 | /** |
| 116 | * HTTP status code for service unavailable. |
| 117 | * |
| 118 | * @since 1.0.0 |
| 119 | * @var int |
| 120 | */ |
| 121 | public const SERVICE_UNAVAILABLE = 503; |
| 122 | /** |
| 123 | * The request headers |
| 124 | * |
| 125 | * @var array |
| 126 | * |
| 127 | * @since 1.0.0 |
| 128 | */ |
| 129 | protected $headers = []; |
| 130 | /** |
| 131 | * Send a JSON response. |
| 132 | * |
| 133 | * @param array $data The data to send. |
| 134 | * @param int $status The HTTP status code. |
| 135 | * @param array $headers The headers to send. |
| 136 | * @param int $options The options to use when encoding the data. |
| 137 | * |
| 138 | * @return JsonResponse |
| 139 | * |
| 140 | * @since 1.0.0 |
| 141 | */ |
| 142 | public function json($data = [], $status = 200, array $headers = [], int $options = 0) |
| 143 | { |
| 144 | return new JsonResponse($data, $status, \array_merge($this->headers, $headers), $options); |
| 145 | } |
| 146 | /** |
| 147 | * Set the headers for the response. |
| 148 | * |
| 149 | * @param array $headers The headers to set. |
| 150 | * |
| 151 | * @return $this |
| 152 | * |
| 153 | * @since 1.0.0 |
| 154 | */ |
| 155 | public function with_headers(array $headers) |
| 156 | { |
| 157 | $this->headers = $headers; |
| 158 | return $this; |
| 159 | } |
| 160 | } |
| 161 |