1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace cse\helpers; |
6
|
|
|
|
7
|
|
|
use cse\helpers\Exceptions\CSEHelpersJsonException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Json |
11
|
|
|
* |
12
|
|
|
* @package cse\helpers |
13
|
|
|
*/ |
14
|
|
|
class Json |
15
|
|
|
{ |
16
|
|
|
const JSON_DEFAULT_UNESCAPED = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE; |
17
|
|
|
const PRETTY_PRINT = self::JSON_DEFAULT_UNESCAPED | JSON_PRETTY_PRINT; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
protected static $checkException = false; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Json encode |
26
|
|
|
* |
27
|
|
|
* @param array $array |
28
|
|
|
* @param int $options |
29
|
|
|
* |
30
|
|
|
* @return string |
31
|
|
|
* |
32
|
|
|
* @throws CSEHelpersJsonException |
33
|
|
|
*/ |
34
|
|
|
public static function encode(array $array, int $options = self::JSON_DEFAULT_UNESCAPED): string |
35
|
|
|
{ |
36
|
|
|
$result = json_encode($array, $options); |
37
|
|
|
|
38
|
|
|
if (self::$checkException) self::errorToException(); |
39
|
|
|
|
40
|
|
|
return $result; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Print JSON data |
45
|
|
|
* |
46
|
|
|
* @param $data |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
* |
50
|
|
|
* @throws CSEHelpersJsonException |
51
|
|
|
*/ |
52
|
|
|
public static function prettyPrint($data): string |
53
|
|
|
{ |
54
|
|
|
$result = json_encode($data, self::PRETTY_PRINT); |
55
|
|
|
|
56
|
|
|
if (self::$checkException) self::errorToException(); |
57
|
|
|
|
58
|
|
|
return $result; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Json decode |
63
|
|
|
* |
64
|
|
|
* @param string $json |
65
|
|
|
* @param bool $assoc |
66
|
|
|
* |
67
|
|
|
* @return mixed |
68
|
|
|
* |
69
|
|
|
* @throws CSEHelpersJsonException |
70
|
|
|
*/ |
71
|
|
|
public static function decode(string $json, bool $assoc = true) |
72
|
|
|
{ |
73
|
|
|
$result = json_decode($json, $assoc); |
74
|
|
|
|
75
|
|
|
if (self::$checkException) self::errorToException(); |
76
|
|
|
|
77
|
|
|
return $result; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get json data |
82
|
|
|
* |
83
|
|
|
* @param string $json |
84
|
|
|
* @param string $key |
85
|
|
|
* @param null $default |
|
|
|
|
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
* |
89
|
|
|
* @throws CSEHelpersJsonException |
90
|
|
|
*/ |
91
|
|
|
public static function get(string $json, string $key, $default = null) |
92
|
|
|
{ |
93
|
|
|
return self::decode($json)[$key] ?? $default; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set check Exception |
98
|
|
|
* |
99
|
|
|
* @param bool $status |
100
|
|
|
*/ |
101
|
|
|
public static function setCheckException(bool $status = true): void |
102
|
|
|
{ |
103
|
|
|
self::$checkException = $status; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Check error last json transform |
108
|
|
|
* |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
public static function isNoteError(): bool |
112
|
|
|
{ |
113
|
|
|
return empty(json_last_error()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get error msg |
118
|
|
|
* |
119
|
|
|
* @param $msg |
120
|
|
|
* |
121
|
|
|
* @return string|null |
122
|
|
|
*/ |
123
|
|
|
public static function getErrorMsg($msg = null): ?string |
124
|
|
|
{ |
125
|
|
|
return self::isNoteError() ? null : json_last_error_msg() . (empty($msg) ? '' : ' ' . print_r($msg ,true)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Error to exception |
130
|
|
|
* |
131
|
|
|
* @param $msg |
132
|
|
|
* |
133
|
|
|
* @throws CSEHelpersJsonException |
134
|
|
|
*/ |
135
|
|
|
public static function errorToException($msg = null): void |
136
|
|
|
{ |
137
|
|
|
if (self::isNoteError()) return; |
138
|
|
|
|
139
|
|
|
throw new CSEHelpersJsonException( |
140
|
|
|
self::getErrorMsg($msg), |
141
|
|
|
json_last_error() |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
} |