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