|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace MaplePHP\Foundation\Http; |
| 6 | + |
| 7 | +use MaplePHP\Foundation\Http\Interfaces\JsonInterface; |
| 8 | + |
| 9 | +class Json implements JsonInterface |
| 10 | +{ |
| 11 | + public const ERROR_MESSAGES = [ |
| 12 | + JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded', |
| 13 | + JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON', |
| 14 | + JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', |
| 15 | + JSON_ERROR_SYNTAX => 'Syntax error', |
| 16 | + JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded', |
| 17 | + JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded', |
| 18 | + JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded', |
| 19 | + JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given', |
| 20 | + JSON_ERROR_INVALID_PROPERTY_NAME => 'A property name that cannot be encoded was given', |
| 21 | + JSON_ERROR_UTF16 => 'Malformed UTF-16 characters, possibly incorrectly encoded' |
| 22 | + ]; |
| 23 | + |
| 24 | + |
| 25 | + public $data = array("status" => 0, "error" => 0); |
| 26 | + public $fields = array(); |
| 27 | + |
| 28 | + public function __construct() |
| 29 | + { |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + public function __toString(): string |
| 34 | + { |
| 35 | + return (string)$this->encode(); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * New json instance |
| 40 | + * @param array|string $data |
| 41 | + * @return self |
| 42 | + */ |
| 43 | + public function withData(array|string $data): self |
| 44 | + { |
| 45 | + $inst = new self(); |
| 46 | + if(is_array($data)) { |
| 47 | + $inst->data = array_merge($this->data, $data); |
| 48 | + } else { |
| 49 | + $inst->data = $this->decode($data); |
| 50 | + } |
| 51 | + return $inst; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Merge array to json array |
| 56 | + * @param array $array |
| 57 | + * @return self |
| 58 | + */ |
| 59 | + public function merge(array $array): self |
| 60 | + { |
| 61 | + $this->data = array_merge($this->data, $array); |
| 62 | + return $this; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Overwrite whole json array |
| 67 | + * @param array $array |
| 68 | + * @return self |
| 69 | + */ |
| 70 | + public function set($array): self |
| 71 | + { |
| 72 | + $this->data = $array; |
| 73 | + return $this; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Merge array to json array |
| 78 | + * @param array $array |
| 79 | + * @return self |
| 80 | + */ |
| 81 | + public function mergeTo(string $key, array $array): self |
| 82 | + { |
| 83 | + if (empty($this->data[$key])) { |
| 84 | + $this->data = array_merge($this->data, [$key => $array]); |
| 85 | + } else { |
| 86 | + $this->data[$key] = array_merge($this->data[$key], $array); |
| 87 | + } |
| 88 | + return $this; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Merge string to json array |
| 93 | + * @param string $key Set array key |
| 94 | + * @param mixed $value Set array value |
| 95 | + * @return self |
| 96 | + */ |
| 97 | + public function add(string $key, $value): self |
| 98 | + { |
| 99 | + $this->data = array_merge($this->data, [$key => $value]); |
| 100 | + return $this; |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + /** |
| 105 | + * Merge string to json array |
| 106 | + * @param string $key Set array key |
| 107 | + * @param mixed $value Set array value |
| 108 | + * @return array |
| 109 | + */ |
| 110 | + public function item(...$args): array |
| 111 | + { |
| 112 | + $key = null; |
| 113 | + if (isset($args[0]) && !is_array($args[0])) { |
| 114 | + $key = array_shift($args); |
| 115 | + if (count($args) === 1) { |
| 116 | + $args = $args[0]; |
| 117 | + } |
| 118 | + } |
| 119 | + $argumnets = (!is_null($key)) ? [[$key => $args]] : $args; |
| 120 | + $this->data = array_merge($this->data, $argumnets); |
| 121 | + return reset($argumnets); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Merge string to json array |
| 126 | + * @param string|array $key Set array key |
| 127 | + * @param array $args Set array value |
| 128 | + * @return self |
| 129 | + */ |
| 130 | + public function field(string|array $key, array $args): self |
| 131 | + { |
| 132 | + if (is_array($key) && count($key) > 0) { |
| 133 | + $key = key($key); |
| 134 | + |
| 135 | + if (!is_string($key)) { |
| 136 | + throw new \Exception("The key need to be string value", 1); |
| 137 | + } |
| 138 | + |
| 139 | + $this->fields = array_merge($this->fields, [$key => [ |
| 140 | + "type" => $key, |
| 141 | + ...$args |
| 142 | + ]]); |
| 143 | + } else { |
| 144 | + if (!is_string($key)) { |
| 145 | + throw new \Exception("The key need to be string value", 1); |
| 146 | + } |
| 147 | + $this->fields = array_merge($this->fields, [$key => $args]); |
| 148 | + } |
| 149 | + return $this; |
| 150 | + } |
| 151 | + |
| 152 | + public function form($fields): self |
| 153 | + { |
| 154 | + $this->fields = array_merge($this->fields, $fields); |
| 155 | + return $this; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Reset |
| 160 | + * @return void |
| 161 | + */ |
| 162 | + public function reset(?array $new = null): void |
| 163 | + { |
| 164 | + if (is_null($new)) { |
| 165 | + $new = array("status" => 0, "error" => 0); |
| 166 | + } |
| 167 | + |
| 168 | + $this->data = $new; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * same as @data method |
| 173 | + * @return mixed |
| 174 | + */ |
| 175 | + public function get(?string $key = null) |
| 176 | + { |
| 177 | + return $this->data($key); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Get current added json array data |
| 182 | + * @return mixed |
| 183 | + */ |
| 184 | + public function data(?string $key = null) |
| 185 | + { |
| 186 | + if (!is_null($key)) { |
| 187 | + return $this->select($key); |
| 188 | + } |
| 189 | + return $this->data; |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Get data has HTML friendly string |
| 194 | + * @param string $key select key (you can use comma sep. to traverse array) |
| 195 | + * @return string |
| 196 | + */ |
| 197 | + public function output(string $key): ?string |
| 198 | + { |
| 199 | + $arr = $this->select($key); |
| 200 | + if ($get = self::encodeData($arr)) { |
| 201 | + return htmlentities($get); |
| 202 | + } |
| 203 | + return null; |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Convert json array to json string |
| 208 | + * @param int $options Bitmask |
| 209 | + * @param int $depth Set the maximum depth. Must be greater than zero |
| 210 | + * @return string|null (bool if could not load json data) |
| 211 | + */ |
| 212 | + public function encode(int $options = JSON_UNESCAPED_UNICODE, int $depth = 512): ?string |
| 213 | + { |
| 214 | + return self::encodeData($this->data, $options, $depth); |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * Decode json data |
| 219 | + * @param string $json Json data |
| 220 | + * @param boolean $assoc When TRUE, returned objects will be converted into associative arrays. |
| 221 | + * @return object|array|false Resturns as array or false if error occoured. |
| 222 | + */ |
| 223 | + public function decode($json, $assoc = true): object|array|false |
| 224 | + { |
| 225 | + if ($array = json_decode($json, $assoc)) { |
| 226 | + return $array; |
| 227 | + } |
| 228 | + return false; |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Validate output |
| 233 | + * @return void |
| 234 | + */ |
| 235 | + public function validate(): void |
| 236 | + { |
| 237 | + $error = (static::ERROR_MESSAGES[self::error()] ?? null); |
| 238 | + if (!is_null($error)) { |
| 239 | + throw new \Exception($error, self::error()); |
| 240 | + } |
| 241 | + //throw new \Exception('An unexpected Json error has occurred', self::error()); |
| 242 | + } |
| 243 | + |
| 244 | + /** |
| 245 | + * Travers slect data |
| 246 | + * @param string $key |
| 247 | + * @return mixed |
| 248 | + */ |
| 249 | + private function select(string $key) |
| 250 | + { |
| 251 | + $set = $this->data; |
| 252 | + $exp = explode(",", $key); |
| 253 | + foreach ($exp as $key) { |
| 254 | + if (isset($set[$key])) { |
| 255 | + $set = $set[$key]; |
| 256 | + } else { |
| 257 | + return null; |
| 258 | + } |
| 259 | + } |
| 260 | + return $set; |
| 261 | + } |
| 262 | + |
| 263 | + /** |
| 264 | + * Json encode data |
| 265 | + * @param array $json array to json |
| 266 | + * @param int $flag read php.net (or use the default) |
| 267 | + * @param int $depth read php.net |
| 268 | + * @return string|null |
| 269 | + */ |
| 270 | + final protected static function encodeData(array $json, int $flag = JSON_UNESCAPED_UNICODE, int $depth = 512): ?string |
| 271 | + { |
| 272 | + if (!($depth > 0 && $depth <= 2147483647)) { |
| 273 | + throw new \Exception("The json encode depth need to be min 1 and max 2147483647!", 1); |
| 274 | + } |
| 275 | + if (count($json) > 0 && ($encode = json_encode($json, $flag, $depth))) { |
| 276 | + return $encode; |
| 277 | + } |
| 278 | + return null; |
| 279 | + } |
| 280 | + |
| 281 | + /** |
| 282 | + * Get last json error |
| 283 | + * @return int |
| 284 | + */ |
| 285 | + final protected static function error(): int |
| 286 | + { |
| 287 | + return json_last_error(); |
| 288 | + } |
| 289 | +} |
0 commit comments