|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JSON; |
|
4
|
|
|
|
|
5
|
|
|
use DomainException; |
|
6
|
|
|
use MVC\Exception; |
|
7
|
|
|
use stdClass; |
|
8
|
|
|
|
|
9
|
|
|
class json |
|
10
|
|
|
{ |
|
11
|
|
|
public $result; |
|
12
|
|
|
private $file; |
|
13
|
|
|
|
|
14
|
|
|
public static function headerJSON() |
|
15
|
|
|
{ |
|
16
|
|
|
return self::json([], true, false); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public static $static; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* chaining. |
|
23
|
|
|
* |
|
24
|
|
|
* @return json |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function init() |
|
27
|
|
|
{ |
|
28
|
|
|
if (!self::$static) { |
|
29
|
|
|
self::$static = new self(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return self::$static; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Load JSON. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function load(string $file, bool $assoc = false) |
|
39
|
|
|
{ |
|
40
|
|
|
if (is_file($file)) { |
|
41
|
|
|
$this->file = $file; |
|
42
|
|
|
$this->result = json_decode(file_get_contents($file), $assoc); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Save JSON from `load(string $file, bool $assoc = false)`. |
|
50
|
|
|
* |
|
51
|
|
|
* @return json |
|
52
|
|
|
*/ |
|
53
|
|
|
public function save() |
|
54
|
|
|
{ |
|
55
|
|
|
if ($this->file && file_exists($this->file)) { |
|
56
|
|
|
file_put_contents($this->file, json_encode($this->result, JSON_PRETTY_PRINT)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Force Assoc. |
|
64
|
|
|
* |
|
65
|
|
|
* @param object|array $arr |
|
66
|
|
|
* |
|
67
|
|
|
* @return json_decode |
|
|
|
|
|
|
68
|
|
|
*/ |
|
69
|
|
|
public static function assoc($arr) |
|
70
|
|
|
{ |
|
71
|
|
|
$str = json_encode($arr); |
|
72
|
|
|
|
|
73
|
|
|
return json_decode($str, true); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* JSON formatter. |
|
78
|
|
|
* |
|
79
|
|
|
* @param mixed $data |
|
80
|
|
|
* @param bool $header |
|
81
|
|
|
* @param bool $print |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function json($data = [], $header = true, $print = true) |
|
84
|
|
|
{ |
|
85
|
|
|
if ($header && !headers_sent()) { |
|
86
|
|
|
header('Content-Type: application/json'); |
|
87
|
|
|
} |
|
88
|
|
|
if ('string' == strtolower(gettype($data)) && self::is_json($data)) { |
|
|
|
|
|
|
89
|
|
|
$data = json_decode($data, true); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$json = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); |
|
93
|
|
|
|
|
94
|
|
|
if ($print) { |
|
95
|
|
|
echo $json; |
|
96
|
|
|
} else { |
|
97
|
|
|
return $json; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* beautify JSON. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string|array|object|stdClass $data |
|
105
|
|
|
* |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
|
|
public static function beautify($data) |
|
109
|
|
|
{ |
|
110
|
|
|
if (ctype_alnum($data) && is_string($data)) { |
|
111
|
|
|
$is_json = self::is_json($data); |
|
112
|
|
|
if ($is_json) { |
|
113
|
|
|
$data = json_decode($data); |
|
114
|
|
|
} else { |
|
115
|
|
|
throw new \MVC\Exception("INVALID JSON (\JSON\json::beautify)", 1); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Validate json string. |
|
124
|
|
|
* |
|
125
|
|
|
* @return bool |
|
126
|
|
|
*/ |
|
127
|
|
|
public static function is_json(string $string) |
|
128
|
|
|
{ |
|
129
|
|
|
json_decode($string); |
|
130
|
|
|
|
|
131
|
|
|
return JSON_ERROR_NONE == json_last_error(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* JSON decode with verification. |
|
136
|
|
|
* |
|
137
|
|
|
* @param string $str |
|
138
|
|
|
*/ |
|
139
|
|
|
public static function jsond($str, $callback = null) |
|
140
|
|
|
{ |
|
141
|
|
|
if (self::is_json($str)) { |
|
142
|
|
|
return json_decode($str); |
|
143
|
|
|
} elseif (is_callable($callback)) { |
|
144
|
|
|
if (!self::is_json($str)) { |
|
145
|
|
|
return call_user_func($callback, $str); |
|
146
|
|
|
} else { |
|
147
|
|
|
return call_user_func($callback, json_decode($str)); |
|
148
|
|
|
} |
|
149
|
|
|
} else { |
|
150
|
|
|
throw new Exception('Not valid JSON string', 1); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* JSON error explanator. |
|
156
|
|
|
*/ |
|
157
|
|
|
public static function json_last_error_e() |
|
158
|
|
|
{ |
|
159
|
|
|
switch (json_last_error()) { |
|
160
|
|
|
case JSON_ERROR_NONE: |
|
161
|
|
|
echo ' - No errors'; |
|
162
|
|
|
break; |
|
163
|
|
|
case JSON_ERROR_DEPTH: |
|
164
|
|
|
echo ' - Maximum stack depth exceeded'; |
|
165
|
|
|
break; |
|
166
|
|
|
case JSON_ERROR_STATE_MISMATCH: |
|
167
|
|
|
echo ' - Underflow or the modes mismatch'; |
|
168
|
|
|
break; |
|
169
|
|
|
case JSON_ERROR_CTRL_CHAR: |
|
170
|
|
|
echo ' - Unexpected control character found'; |
|
171
|
|
|
break; |
|
172
|
|
|
case JSON_ERROR_SYNTAX: |
|
173
|
|
|
echo ' - Syntax error, malformed JSON'; |
|
174
|
|
|
break; |
|
175
|
|
|
case JSON_ERROR_UTF8: |
|
176
|
|
|
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded'; |
|
177
|
|
|
break; |
|
178
|
|
|
default: |
|
179
|
|
|
echo ' - Unknown error'; |
|
180
|
|
|
break; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* json_decode default assoc. |
|
186
|
|
|
* |
|
187
|
|
|
* @param bool $assoc |
|
188
|
|
|
* |
|
189
|
|
|
* @return \json_decode |
|
|
|
|
|
|
190
|
|
|
*/ |
|
191
|
|
|
public static function json_decode(string $str, $err_callback = null, $assoc = true) |
|
192
|
|
|
{ |
|
193
|
|
|
if (!self::isJson($str)) { |
|
194
|
|
|
if (ctype_alnum($err_callback)) { |
|
195
|
|
|
return $err_callback; |
|
196
|
|
|
} else { |
|
197
|
|
|
throw new Exception('str must be a valid JSON format, instead of ' . gettype($str), 1); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return json_decode($str, $assoc); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
public static function jsonDecode($input) |
|
205
|
|
|
{ |
|
206
|
|
|
if (!self::isJson($input)) { |
|
207
|
|
|
throw new Exception('input must be a valid JSON format', 1); |
|
208
|
|
|
} |
|
209
|
|
|
if (version_compare(PHP_VERSION, '5.4.0', '>=') && !(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) { |
|
210
|
|
|
/** In PHP >=5.4.0, json_decode() accepts an options parameter, that allows you |
|
211
|
|
|
* to specify that large ints (like Steam Transaction IDs) should be treated as |
|
212
|
|
|
* strings, rather than the PHP default behaviour of converting them to floats. |
|
213
|
|
|
*/ |
|
214
|
|
|
$obj = json_decode($input, false, 512, JSON_BIGINT_AS_STRING); |
|
215
|
|
|
} else { |
|
216
|
|
|
/** Not all servers will support that, however, so for older versions we must |
|
217
|
|
|
* manually detect large ints in the JSON string and quote them (thus converting |
|
218
|
|
|
*them to strings) before decoding, hence the preg_replace() call. |
|
219
|
|
|
*/ |
|
220
|
|
|
$max_int_length = strlen((string) PHP_INT_MAX) - 1; |
|
221
|
|
|
$json_without_bigints = preg_replace('/:\s*(-?\d{' . $max_int_length . ',})/', ': "$1"', $input); |
|
222
|
|
|
$obj = json_decode($json_without_bigints); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
if (function_exists('json_last_error') && $errno = json_last_error()) { |
|
|
|
|
|
|
226
|
|
|
//static::handleJsonError($errno); |
|
227
|
|
|
self::json_last_error_e(); |
|
228
|
|
|
} elseif (null === $obj && 'null' !== $input) { |
|
229
|
|
|
throw new DomainException('Null result with non-null input'); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
return $obj; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
public static function isJson($string) |
|
236
|
|
|
{ |
|
237
|
|
|
json_decode($string); |
|
238
|
|
|
|
|
239
|
|
|
return JSON_ERROR_NONE == json_last_error(); |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths