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
JsonResponse.php
183 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * WP_REST_Response subclass that encodes data as JSON with configurable encoding options. |
| 5 | * Preserves original data alongside the encoded content string. |
| 6 | * Used by the response helper for consistent API JSON output. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @subpackage Http |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | namespace Kirki\Framework\Http; |
| 13 | |
| 14 | \defined('ABSPATH') || exit; |
| 15 | use Kirki\Framework\Contracts\Support\Arrayable; |
| 16 | use Kirki\Framework\Contracts\Support\Jsonable; |
| 17 | use Kirki\Framework\Http\Concerns\InteractsWithCookies; |
| 18 | use Kirki\Framework\Supports\Arr; |
| 19 | use InvalidArgumentException; |
| 20 | use JsonSerializable; |
| 21 | use WP_REST_Response; |
| 22 | class JsonResponse extends WP_REST_Response |
| 23 | { |
| 24 | use InteractsWithCookies; |
| 25 | /** |
| 26 | * The original data that was passed to the response. |
| 27 | * |
| 28 | * @var mixed |
| 29 | * |
| 30 | * @since 1.0.0 |
| 31 | */ |
| 32 | protected $original; |
| 33 | /** |
| 34 | * The content of the response. |
| 35 | * |
| 36 | * @var string |
| 37 | * |
| 38 | * @since 1.0.0 |
| 39 | */ |
| 40 | protected $content; |
| 41 | /** |
| 42 | * The encoding options for the response. |
| 43 | * |
| 44 | * @var int |
| 45 | * |
| 46 | * @since 1.0.0 |
| 47 | */ |
| 48 | protected $encoding_options = 0; |
| 49 | /** |
| 50 | * Create a new JSON response instance. |
| 51 | * |
| 52 | * @param mixed $data The data to be encoded as JSON |
| 53 | * @param int $status The HTTP status code |
| 54 | * @param array $headers The headers to be sent with the response |
| 55 | * @param int $options The JSON encoding options |
| 56 | * |
| 57 | * @return void |
| 58 | * |
| 59 | * @since 1.0.0 |
| 60 | */ |
| 61 | public function __construct($data = [], $status = 200, array $headers = [], int $options = 0) |
| 62 | { |
| 63 | $this->original = $data; |
| 64 | $this->status = $status; |
| 65 | $this->headers = $headers; |
| 66 | $this->encoding_options = $options; |
| 67 | $this->set_content($data); |
| 68 | $this->set_status($status); |
| 69 | $this->set_headers($headers); |
| 70 | $this->set_data($this->get_content()); |
| 71 | } |
| 72 | /** |
| 73 | * Set the encoding options for the response. |
| 74 | * |
| 75 | * @param int $option The JSON encoding options |
| 76 | * |
| 77 | * @return $this The response instance for method chaining |
| 78 | * |
| 79 | * @since 1.0.0 |
| 80 | */ |
| 81 | public function set_encoding_options($option) |
| 82 | { |
| 83 | $this->encoding_options = (int) $option; |
| 84 | return $this->set_content($this->get_content()); |
| 85 | } |
| 86 | /** |
| 87 | * Get the encoding options for the response. |
| 88 | * |
| 89 | * @return int The JSON encoding options |
| 90 | * |
| 91 | * @since 1.0.0 |
| 92 | */ |
| 93 | public function get_encoding_options() |
| 94 | { |
| 95 | return $this->encoding_options; |
| 96 | } |
| 97 | /** |
| 98 | * Get the content of the response. |
| 99 | * |
| 100 | * @param bool $assoc Whether to return the content as an associative array |
| 101 | * @param int $depth The maximum depth of the content |
| 102 | * |
| 103 | * @return mixed The content of the response |
| 104 | * |
| 105 | * @since 1.0.0 |
| 106 | */ |
| 107 | public function get_content($assoc = \false, $depth = 512) |
| 108 | { |
| 109 | $status_code = (int) $this->get_status(); |
| 110 | $success = $status_code >= 200 && $status_code < 300; |
| 111 | $content = \json_decode($this->content, $assoc, $depth); |
| 112 | if (!$assoc) { |
| 113 | $content->success = $success; |
| 114 | return $content; |
| 115 | } |
| 116 | return \array_merge(['success' => $success], $content); |
| 117 | } |
| 118 | /** |
| 119 | * Set the content of the response. |
| 120 | * |
| 121 | * @param mixed $data The data to be encoded as JSON |
| 122 | * |
| 123 | * @return $this The response instance for method chaining |
| 124 | * |
| 125 | * @throws \InvalidArgumentException |
| 126 | * |
| 127 | * @since 1.0.0 |
| 128 | */ |
| 129 | public function set_content($data) |
| 130 | { |
| 131 | $this->original = $data; |
| 132 | // Clear the json error stack |
| 133 | \json_decode('[]'); |
| 134 | switch (\true) { |
| 135 | case $data instanceof Jsonable: |
| 136 | $this->content = $data->to_json($this->encoding_options); |
| 137 | break; |
| 138 | case $data instanceof JsonSerializable: |
| 139 | $this->content = Arr::json_encode($data->jsonSerialize(), $this->encoding_options); |
| 140 | break; |
| 141 | case $data instanceof Arrayable: |
| 142 | $this->content = Arr::json_encode($data->to_array(), $this->encoding_options); |
| 143 | break; |
| 144 | default: |
| 145 | $this->content = Arr::json_encode($data, $this->encoding_options); |
| 146 | break; |
| 147 | } |
| 148 | if (!$this->is_valid_json(\json_last_error())) { |
| 149 | throw new InvalidArgumentException(\json_last_error_msg()); |
| 150 | } |
| 151 | return $this; |
| 152 | } |
| 153 | /** |
| 154 | * Check if the JSON is valid. |
| 155 | * |
| 156 | * @param int $json_error The JSON error code |
| 157 | * |
| 158 | * @return bool Whether the JSON is valid |
| 159 | * |
| 160 | * @since 1.0.0 |
| 161 | */ |
| 162 | protected function is_valid_json($json_error) |
| 163 | { |
| 164 | if ($json_error === \JSON_ERROR_NONE) { |
| 165 | return \true; |
| 166 | } |
| 167 | return $this->has_encoding_options(\JSON_PARTIAL_OUTPUT_ON_ERROR) && \in_array($json_error, [\JSON_ERROR_RECURSION, \JSON_ERROR_INF_OR_NAN, \JSON_ERROR_UNSUPPORTED_TYPE]); |
| 168 | } |
| 169 | /** |
| 170 | * Check if the encoding options have a specific option set. |
| 171 | * |
| 172 | * @param int $option The option to check |
| 173 | * |
| 174 | * @return bool Whether the option is set |
| 175 | * |
| 176 | * @since 1.0.0 |
| 177 | */ |
| 178 | protected function has_encoding_options($option) |
| 179 | { |
| 180 | return (bool) ($this->encoding_options & $option); |
| 181 | } |
| 182 | } |
| 183 |