|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jabe\Impl\Util; |
|
4
|
|
|
|
|
5
|
|
|
use Jabe\Impl\Json\JsonObjectConverter; |
|
6
|
|
|
|
|
7
|
|
|
class JsonUtil |
|
8
|
|
|
{ |
|
9
|
|
|
public static function addFieldRawValue(\stdClass $jsonObject, string $memberName, $rawValue = null): void |
|
10
|
|
|
{ |
|
11
|
|
|
if ($rawValue !== null) { |
|
12
|
|
|
$jsonObject->{$memberName} = json_decode($rawValue); |
|
13
|
|
|
} |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
public static function addNullField(\stdClass $jsonObject, string $name): void |
|
17
|
|
|
{ |
|
18
|
|
|
$jsonObject->{$name} = null; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public static function addField(\stdClass $jsonObject, string $name, $converterOrValue = null, $value = null): void |
|
22
|
|
|
{ |
|
23
|
|
|
if ($converterOrValue !== null && $converterOrValue instanceof JsonObjectConverter) { |
|
24
|
|
|
$jsonObject->{$name} = $converterOrValue->toJsonObject($value); |
|
25
|
|
|
} elseif ($converterOrValue !== null) { |
|
26
|
|
|
$jsonObject->{$name} = $converterOrValue; |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public static function addListField(\stdClass $jsonObject, string $name, $converterOrlist, $list = null): void |
|
31
|
|
|
{ |
|
32
|
|
|
if ($converterOrlist !== null && is_array($converterOrlist)) { |
|
33
|
|
|
$jsonObject->{$name} = $converterOrlist; |
|
34
|
|
|
} elseif ($converterOrlist !== null && $converterOrlist instanceof JsonObjectConverter && $list !== null) { |
|
35
|
|
|
$arrayNode = []; |
|
36
|
|
|
foreach ($list as $item) { |
|
37
|
|
|
if ($item !== null) { |
|
38
|
|
|
$jsonElement = $converterOrlist->toJsonObject($item); |
|
39
|
|
|
$arrayNode[] = $jsonElement; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
$jsonObject->{$name} = $arrayNode; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public static function addArrayField(\stdClass $jsonObject, string $name, array $array = null): void |
|
47
|
|
|
{ |
|
48
|
|
|
if ($array !== null) { |
|
49
|
|
|
self::addListField($jsonObject, $name, $array); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public static function addDateField(\stdClass $jsonObject, string $name, $date = null): void |
|
54
|
|
|
{ |
|
55
|
|
|
if ($date !== null) { |
|
56
|
|
|
if (is_string($date)) { |
|
57
|
|
|
$jsonObject->{$name} = $date; |
|
58
|
|
|
} elseif ($date instanceof \DateTime) { |
|
59
|
|
|
$jsonObject->{$name} = $date->format('c'); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public static function addElement(array &$jsonObject, JsonObjectConverter $converter, $value = null): void |
|
65
|
|
|
{ |
|
66
|
|
|
if ($value !== null) { |
|
67
|
|
|
$jsonElement = $converter->toJsonObject($value); |
|
68
|
|
|
if ($jsonElement !== null) { |
|
69
|
|
|
$jsonObject[] = $jsonElement; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public static function asObject($data) |
|
75
|
|
|
{ |
|
76
|
|
|
try { |
|
77
|
|
|
if (is_object($data)) { |
|
78
|
|
|
return $data; |
|
79
|
|
|
} |
|
80
|
|
|
if (is_string($data)) { |
|
81
|
|
|
return json_decode($data); |
|
82
|
|
|
} |
|
83
|
|
|
if (is_array($data)) { |
|
84
|
|
|
return (object) $data; |
|
85
|
|
|
} |
|
86
|
|
|
} catch (\Exception $e) { |
|
87
|
|
|
throw $e; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public static function asBytes(\stdClass $jsonObject): string |
|
92
|
|
|
{ |
|
93
|
|
|
return json_encode($jsonObject); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public static function asString(?array $properties = null): string |
|
97
|
|
|
{ |
|
98
|
|
|
if (!empty($properties)) { |
|
99
|
|
|
return json_encode($properties); |
|
100
|
|
|
} |
|
101
|
|
|
return ""; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public static function asPhpObject(\stdClass $jsonObject = null, JsonObjectConverter $converter = null) |
|
105
|
|
|
{ |
|
106
|
|
|
if ($jsonObject !== null && $converter !== null) { |
|
107
|
|
|
return $converter->toObject($jsonObject); |
|
108
|
|
|
} else { |
|
109
|
|
|
return null; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public static function asStringList($jsonObject = []): array |
|
114
|
|
|
{ |
|
115
|
|
|
if (empty($jsonObject)) { |
|
116
|
|
|
return []; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$list = []; |
|
120
|
|
|
foreach ($jsonObject as $entry) { |
|
121
|
|
|
$stringValue = null; |
|
|
|
|
|
|
122
|
|
|
try { |
|
123
|
|
|
$stringValue = json_encode($entry); |
|
124
|
|
|
} catch (\Exception $e) { |
|
125
|
|
|
//LOG.logJsonException(e); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
if ($stringValue !== null) { |
|
129
|
|
|
$list[] = $stringValue; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $list; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public static function asList($jsonArray = [], JsonObjectConverter $converter = null, $listSupplier = null) |
|
137
|
|
|
{ |
|
138
|
|
|
if (empty($jsonArray) || $converter === null) { |
|
139
|
|
|
return []; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$list = null; |
|
143
|
|
|
if (is_string($listSupplier) && class_exists($listSupplier)) { |
|
144
|
|
|
$list = new $listSupplier(); |
|
145
|
|
|
} else { |
|
146
|
|
|
$list = []; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
foreach ($jsonArray as $element) { |
|
150
|
|
|
$jsonObject = null; |
|
|
|
|
|
|
151
|
|
|
try { |
|
152
|
|
|
$jsonObject = (object) $element; |
|
153
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
|
154
|
|
|
//LOG.logJsonException(e); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
if ($jsonObject !== null) { |
|
158
|
|
|
$rawObject = $converter->toObject($jsonObject); |
|
159
|
|
|
if ($rawObject !== null) { |
|
160
|
|
|
if (is_array($list)) { |
|
161
|
|
|
$list[] = $rawObject; |
|
162
|
|
|
} elseif ($list !== null && method_exists($list, 'add')) { |
|
163
|
|
|
$list->add($rawObject); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $list; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public static function getArray($json = null, string $memberName = null): array |
|
173
|
|
|
{ |
|
174
|
|
|
if ($json !== null && $json instanceof \stdClass && $memberName !== null && property_exists($json, $memberName)) { |
|
175
|
|
|
return self::getArray($json->{$memberName}); |
|
176
|
|
|
} elseif ($json !== null && is_array($json)) { |
|
177
|
|
|
return $json; |
|
178
|
|
|
} else { |
|
179
|
|
|
return self::createArray(); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
public static function getObject(\stdClass $json, ?string $memberName = null): \stdClass |
|
184
|
|
|
{ |
|
185
|
|
|
if ($json !== null && $memberName === null) { |
|
186
|
|
|
return $json; |
|
187
|
|
|
} |
|
188
|
|
|
if ($json !== null && $memberName !== null && property_exists($json, $memberName)) { |
|
189
|
|
|
return $json->{$memberName}; |
|
190
|
|
|
} else { |
|
191
|
|
|
return self::createObject(); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public static function createObject(): \stdClass |
|
196
|
|
|
{ |
|
197
|
|
|
return new \stdClass(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public static function createArray(): array |
|
201
|
|
|
{ |
|
202
|
|
|
return []; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
public static function getBoolean(\stdClass $json = null, string $memberName = null): bool |
|
206
|
|
|
{ |
|
207
|
|
|
if ($json !== null && $memberName !== null && property_exists($json, $memberName)) { |
|
208
|
|
|
try { |
|
209
|
|
|
return boolval(json_decode($json->{$memberName})); |
|
210
|
|
|
} catch (\Exception $e) { |
|
211
|
|
|
//LOG.logJsonException(e); |
|
212
|
|
|
return false; |
|
213
|
|
|
} |
|
214
|
|
|
} else { |
|
215
|
|
|
return false; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public static function getString($json = null, string $memberName = null, string $defaultString = null): string |
|
220
|
|
|
{ |
|
221
|
|
|
if (is_object($json)) { |
|
222
|
|
|
if ($json !== null && $memberName !== null && property_exists($json, $memberName)) { |
|
223
|
|
|
return self::getString($json->{$memberName}); |
|
224
|
|
|
} else { |
|
225
|
|
|
return $defaultString; |
|
|
|
|
|
|
226
|
|
|
} |
|
227
|
|
|
} else { |
|
228
|
|
|
try { |
|
229
|
|
|
return json_encode($json); |
|
230
|
|
|
} catch (\Exception $e) { |
|
231
|
|
|
return ""; |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
public static function getInt(\stdClass $json = null, string $memberName = null): int |
|
237
|
|
|
{ |
|
238
|
|
|
if ($json !== null && $memberName !== null && property_exists($json, $memberName)) { |
|
239
|
|
|
try { |
|
240
|
|
|
return intval($json->{$memberName}); |
|
241
|
|
|
} catch (\Exception $e) { |
|
242
|
|
|
//LOG.logJsonException(e); |
|
243
|
|
|
return 0; |
|
244
|
|
|
} |
|
245
|
|
|
} else { |
|
246
|
|
|
return 0; |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
public static function isNull(\stdClass $jsonObject = null, string $memberName = null): bool |
|
251
|
|
|
{ |
|
252
|
|
|
if ($jsonObject !== null && $memberName !== null && property_exists($jsonObject, $memberName)) { |
|
253
|
|
|
return $jsonObject->{$memberName} === null; |
|
254
|
|
|
} else { |
|
255
|
|
|
return false; |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
public static function getLong(\stdClass $json = null, string $memberName = null): int |
|
260
|
|
|
{ |
|
261
|
|
|
return self::getInt($json, $memberName); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|