-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJson.php
More file actions
217 lines (166 loc) · 6.09 KB
/
Json.php
File metadata and controls
217 lines (166 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/base/blob/master/LICENSE
*/
namespace Quid\Base;
// json
// class with static methods to encode and decode JSON
final class Json extends Assoc
{
// config
protected static array $config = [
'option'=>[ // tableau d'options
'encode'=>JSON_INVALID_UTF8_IGNORE | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES, // flag encode
'decode'=>JSON_INVALID_UTF8_IGNORE | JSON_BIGINT_AS_STRING, // flag decode
'depth'=>512, // depth pour encode et decode
'assoc'=>true, // option assoc pour decode
'case'=>null, // les clés sont ramenés dans cette case dans arr
'sort'=>null] // les clés sont sort
];
// is
// retourne vrai si la chaîne est du json
final public static function is($value):bool
{
return is_string($value) && self::decode($value) !== null;
}
// isEmpty
// retourne vrai si la chaîne est du json mais vide
final public static function isEmpty($value):bool
{
return is_string($value) && ($json = self::decode($value)) !== null && empty($json);
}
// isNotEmpty
// retourne vrai si la chaîne est du json non vide
final public static function isNotEmpty($value):bool
{
return is_string($value) && ($json = self::decode($value)) !== null && !empty($json);
}
// encode
// encode une variable en json
// option à null enlève les options, si option est set remplace les options par défaut
// note: json_encode retourne false si une erreur survient, à ce moment une erreur est déclenché
final public static function encode($value,?int $flag=null,?int $depth=null):?string
{
$return = null;
$option = self::option();
$flag ??= $option['encode'];
$depth ??= $option['depth'];
$return = json_encode($value,$flag,$depth);
return $return;
}
// encodeOption
// encode une variable en json
// option append les options par défaut
final public static function encodeOption($value,int $flag,?int $depth=null):?string
{
$return = null;
$option = self::option();
$flag = $option['encode'] | $flag;
$return = self::encode($value,$flag,$depth);
return $return;
}
// encodePretty
// encode une variable en json
// append json_pretty_print aux options par défaut
final public static function encodePretty($value,int $depth=null):?string
{
$return = null;
$option = self::option();
$flag = $option['encode'] | JSON_PRETTY_PRINT;
$return = self::encode($value,$flag,$depth);
return $return;
}
// encodeSpecialchars
// encode en json et envoie la string dans specialchars
final public static function encodeSpecialchars($value,?int $flag=null,?int $depth=null):?string
{
$return = '';
$json = self::encode($value,$flag,$depth);
if(is_string($json))
$return = Html::specialchars($json);
return $return;
}
// encodeVar
// encode une valeur et retourne la dans une variable javascript
final public static function encodeVar(string $var,$value,?int $flag=null,?int $depth=null):?string
{
$return = null;
$value = self::encode($value,$flag,$depth);
if(is_string($value))
$return = self::var($var,$value);
return $return;
}
// var
// écrit une valeur js dans une variable javascript
final public static function var(string $var,string $value):string
{
$return = $var;
$return .= ' = ';
$return .= $value;
$return .= ';';
return $return;
}
// decode
// decode une chaine json
// option à null enlève les options
// note: json_decode retourne false si une erreur survient
final public static function decode(string $value,?bool $assoc=null,?int $flag=null,?int $depth=null)
{
$option = self::option();
$assoc ??= $option['assoc'];
$depth ??= $option['depth'];
$flag ??= $option['decode'];
return json_decode($value,$assoc,$depth,$flag);
}
// decodeKeys
// decode une chaîne json et retourne les clés demandés
final public static function decodeKeys(array $keys,string $value,?bool $assoc=null,?int $flag=null,?int $depth=null)
{
$return = null;
$decode = self::decode($value,$assoc,$flag,$depth);
if(is_array($decode))
$return = Arr::gets($keys,$decode);
return $return;
}
// decodeKeysExists
// decode une chaîne json et retourne le tableau seulement si les clés existent
final public static function decodeKeysExists(array $keys,string $value,?bool $assoc=null,?int $flag=null,?int $depth=null)
{
$return = null;
$decode = self::decode($value,$assoc,$flag,$depth);
if(is_array($decode) && Arr::keysExists($keys,$decode))
$return = $decode;
return $return;
}
// error
// retourne les informations sur la dernière erreur json
final public static function error():array
{
return ['code'=>json_last_error(),'msg'=>json_last_error_msg()];
}
// arr
// explose une string json
// retourne tableau vide si après decode ce n'est pas un tableau
final public static function arr($value,?array $option=null):array
{
$return = [];
$option = self::option($option);
if(is_scalar($value))
$value = self::decode($value,$option['assoc'],$option['decode'],$option['depth']);
if(is_array($value))
{
$return = Arr::trimClean($value,$option['trim'],$option['trim'],$option['clean']);
if($option['case'] !== null)
$return = Arr::keysChangeCase($option['case'],$return);
if($option['sort'] !== null)
$return = Arr::sort($return,$option['sort']);
}
return $return;
}
}
// init
Json::__init();
?>