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
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var bool |
20
|
|
|
*/ |
21
|
|
|
protected static $checkException = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Json encode |
25
|
|
|
* |
26
|
|
|
* @param array $data |
27
|
|
|
* @param int $options |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
* |
31
|
|
|
* @throws CSEHelpersJsonException |
32
|
|
|
*/ |
33
|
|
|
public static function encode(array $data, int $options = self::JSON_DEFAULT_UNESCAPED): string |
34
|
|
|
{ |
35
|
|
|
$result = json_encode($data, $options); |
36
|
|
|
|
37
|
|
|
if (self::$checkException) self::errorToException(); |
38
|
|
|
|
39
|
|
|
return $result; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Print JSON data |
44
|
|
|
* |
45
|
|
|
* @param $data |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
* |
49
|
|
|
* @throws CSEHelpersJsonException |
50
|
|
|
*/ |
51
|
|
|
public static function prettyPrint($data): string |
52
|
|
|
{ |
53
|
|
|
$result = json_encode($data, JSON_PRETTY_PRINT); |
54
|
|
|
|
55
|
|
|
if (self::$checkException) self::errorToException(); |
56
|
|
|
|
57
|
|
|
return $result; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Json decode |
62
|
|
|
* |
63
|
|
|
* @param string $data |
64
|
|
|
* @param bool $assoc |
65
|
|
|
* |
66
|
|
|
* @return mixed |
67
|
|
|
* |
68
|
|
|
* @throws CSEHelpersJsonException |
69
|
|
|
*/ |
70
|
|
|
public static function decode(string $data, bool $assoc = true) |
71
|
|
|
{ |
72
|
|
|
$result = json_decode($data, $assoc); |
73
|
|
|
|
74
|
|
|
if (self::$checkException) self::errorToException(); |
75
|
|
|
|
76
|
|
|
return $result; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set check Exception |
81
|
|
|
* |
82
|
|
|
* @param bool $status |
83
|
|
|
*/ |
84
|
|
|
public static function setCheckException(bool $status = true): void |
85
|
|
|
{ |
86
|
|
|
self::$checkException = $status; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Check error last json transform |
91
|
|
|
* |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public static function isNoteError(): bool |
95
|
|
|
{ |
96
|
|
|
return empty(json_last_error()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get error msg |
101
|
|
|
* |
102
|
|
|
* @param $msg |
103
|
|
|
* |
104
|
|
|
* @return string|null |
105
|
|
|
*/ |
106
|
|
|
public static function getErrorMsg($msg = null): ?string |
107
|
|
|
{ |
108
|
|
|
return self::isNoteError() ? null : json_last_error_msg() . (empty($msg) ? '' : ' ' . print_r($msg ,true)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Error to exception |
113
|
|
|
* |
114
|
|
|
* @param $msg |
115
|
|
|
* |
116
|
|
|
* @throws CSEHelpersJsonException |
117
|
|
|
*/ |
118
|
|
|
public static function errorToException($msg = null): void |
119
|
|
|
{ |
120
|
|
|
if (self::isNoteError()) return; |
121
|
|
|
|
122
|
|
|
throw new CSEHelpersJsonException( |
123
|
|
|
self::getErrorMsg($msg), |
124
|
|
|
json_last_error() |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
} |