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