-
Notifications
You must be signed in to change notification settings - Fork 5
/
JsonParseNode.php
296 lines (267 loc) · 8.53 KB
/
JsonParseNode.php
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
namespace Microsoft\Kiota\Serialization\Json;
use DateInterval;
use DateTime;
use Exception;
use GuzzleHttp\Psr7\Utils;
use InvalidArgumentException;
use Microsoft\Kiota\Abstractions\Enum;
use Microsoft\Kiota\Abstractions\Serialization\AdditionalDataHolder;
use Microsoft\Kiota\Abstractions\Serialization\Parsable;
use Microsoft\Kiota\Abstractions\Serialization\ParseNode;
use Microsoft\Kiota\Abstractions\Types\Date;
use Microsoft\Kiota\Abstractions\Types\Time;
use Psr\Http\Message\StreamInterface;
use RuntimeException;
/**
* @method onBeforeAssignFieldValues(Parsable $result)
* @method onAfterAssignFieldValues(Parsable $result)
*/
class JsonParseNode implements ParseNode
{
/** @var mixed|null $jsonNode*/
private $jsonNode;
/** @var callable|null */
public $onBeforeAssignFieldValues;
/** @var callable|null */
public $onAfterAssignFieldValues;
/**
* @param mixed|null $content
*/
public function __construct($content) {
$this->jsonNode = $content;
}
/**
* @inheritDoc
*/
public function getChildNode(string $identifier): ?ParseNode {
if ((!is_array($this->jsonNode)) || (($this->jsonNode[$identifier] ?? null) === null)) {
return null;
}
return new self($this->jsonNode[$identifier]);
}
/**
* @inheritDoc
*/
public function getStringValue(): ?string {
return $this->jsonNode !== null ? addcslashes(strval($this->jsonNode), "\\\r\n") : null;
}
/**
* @inheritDoc
*/
public function getBooleanValue(): ?bool {
return $this->jsonNode !== null ? (bool)$this->jsonNode : null;
}
/**
* @inheritDoc
*/
public function getIntegerValue(): ?int {
return $this->jsonNode !== null ? intval($this->jsonNode) : null;
}
/**
* @inheritDoc
*/
public function getFloatValue(): ?float {
return $this->jsonNode !== null ? floatval($this->jsonNode) : null;
}
/**
* @inheritDoc
* @throws Exception
*/
public function getCollectionOfObjectValues(array $type): ?array {
if (!is_array($this->jsonNode)) {
return null;
}
$result = array_map(static function ($val) use($type) {
return $val->getObjectValue($type);
}, array_map(static function ($value) {
return new JsonParseNode($value);
}, $this->jsonNode));
return array_filter($result, fn ($item) => !is_null($item));
}
/**
* @inheritDoc
* @throws Exception
*/
public function getObjectValue(array $type): ?Parsable {
if ($this->jsonNode === null) {
return null;
}
if (!is_subclass_of($type[0], Parsable::class)){
throw new InvalidArgumentException("Invalid type $type[0] provided.");
}
if (!is_callable($type, true, $callableString)) {
throw new InvalidArgumentException('Undefined method '. $type[1]);
}
$result = $callableString($this);
if($this->getOnBeforeAssignFieldValues() !== null) {
$this->getOnBeforeAssignFieldValues()($result);
}
$this->assignFieldValues($result);
if ($this->getOnAfterAssignFieldValues() !== null){
$this->getOnAfterAssignFieldValues()($result);
}
return $result;
}
/**
* @param Parsable|AdditionalDataHolder $result
* @return void
*/
private function assignFieldValues($result): void {
$fieldDeserializers = [];
if (is_a($result, Parsable::class)){
$fieldDeserializers = $result->getFieldDeserializers();
}
$isAdditionalDataHolder = false;
$additionalData = [];
if (is_a($result, AdditionalDataHolder::class)) {
$isAdditionalDataHolder = true;
$additionalData = $result->getAdditionalData() ?? [];
}
if (is_array($this->jsonNode)) {
foreach ($this->jsonNode as $key => $value) {
$deserializer = $fieldDeserializers[$key] ?? null;
if ($deserializer !== null) {
$deserializer(new JsonParseNode($value));
} else {
$key = (string)$key;
$additionalData[$key] = $value;
}
}
}
if ( $isAdditionalDataHolder ) {
$result->setAdditionalData($additionalData);
}
}
/**
* @inheritDoc
*/
public function getEnumValue(string $targetEnum): ?Enum{
if ($this->jsonNode === null){
return null;
}
if (!is_subclass_of($targetEnum, Enum::class)) {
throw new InvalidArgumentException('Invalid enum provided.');
}
return new $targetEnum($this->jsonNode);
}
/**
* @inheritDoc
*/
public function getCollectionOfEnumValues(string $targetClass): ?array {
if (!is_array($this->jsonNode)) {
return null;
}
$result = array_map(static function ($val) use($targetClass) {
return $val->getEnumValue($targetClass);
}, array_map(static function ($value) {
return new JsonParseNode($value);
}, $this->jsonNode));
return array_filter($result, fn ($item) => !is_null($item));
}
/**
* @inheritDoc
*/
public function getOnBeforeAssignFieldValues(): ?callable {
return $this->onBeforeAssignFieldValues;
}
/**
* @inheritDoc
*/
public function getOnAfterAssignFieldValues(): ?callable {
return $this->onAfterAssignFieldValues;
}
/**
* @inheritDoc
*/
public function setOnAfterAssignFieldValues(callable $value): void {
$this->onAfterAssignFieldValues = $value;
}
/**
* @inheritDoc
*/
public function setOnBeforeAssignFieldValues(callable $value): void {
$this->onBeforeAssignFieldValues = $value;
}
/**
* @inheritDoc
* @throws Exception
*/
public function getCollectionOfPrimitiveValues(?string $typeName = null): ?array {
if (!is_array($this->jsonNode)) {
return null;
}
return array_map(static function ($x) use ($typeName) {
$type = empty($typeName) ? get_debug_type($x) : $typeName;
return (new JsonParseNode($x))->getAnyValue($type);
}, $this->jsonNode);
}
/**
* @return mixed
* @throws Exception
*/
public function getAnyValue(string $type) {
switch ($type){
case 'bool':
return $this->getBooleanValue();
case 'string':
return $this->getStringValue();
case 'int':
return $this->getIntegerValue();
case 'float':
return $this->getFloatValue();
case 'null':
return null;
case 'array':
return $this->getCollectionOfPrimitiveValues();
case Date::class:
return $this->getDateValue();
case Time::class:
return $this->getTimeValue();
default:
if (is_subclass_of($type, Enum::class)){
return $this->getEnumValue($type);
}
if (is_subclass_of($type, StreamInterface::class)) {
return $this->getBinaryContent();
}
throw new InvalidArgumentException("Unable to decode type $type");
}
}
/**
* @inheritDoc
* @throws Exception
*/
public function getDateValue(): ?Date {
return ($this->jsonNode !== null) ? new Date(strval($this->jsonNode)) : null;
}
/**
* @inheritDoc
* @throws Exception
*/
public function getTimeValue(): ?Time {
return ($this->jsonNode !== null) ? new Time(strval($this->jsonNode)) : null;
}
/**
* @inheritDoc
* @throws Exception
*/
public function getDateTimeValue(): ?DateTime {
return ($this->jsonNode !== null) ? new DateTime(strval($this->jsonNode)) : null;
}
/**
* @inheritDoc
* @throws Exception
*/
public function getDateIntervalValue(): ?DateInterval{
return ($this->jsonNode !== null) ? new DateInterval(strval($this->jsonNode)) : null;
}
public function getBinaryContent(): ?StreamInterface {
if (is_null($this->jsonNode)) {
return null;
} elseif (is_array($this->jsonNode)) {
return Utils::streamFor(json_encode($this->jsonNode));
}
return Utils::streamFor(strval($this->jsonNode));
}
}