|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (c) Nate Brunette. |
|
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace Tebru\Gson\Element; |
|
10
|
|
|
|
|
11
|
|
|
use ArrayIterator; |
|
12
|
|
|
use Countable; |
|
13
|
|
|
use IteratorAggregate; |
|
14
|
|
|
use LogicException; |
|
15
|
|
|
use stdClass; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class JsonObject |
|
19
|
|
|
* |
|
20
|
|
|
* Represents a json object |
|
21
|
|
|
* |
|
22
|
|
|
* @author Nate Brunette <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class JsonObject extends JsonElement implements IteratorAggregate, Countable |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Object properties |
|
28
|
|
|
* |
|
29
|
|
|
* @var JsonElement[] |
|
30
|
|
|
*/ |
|
31
|
|
|
private $properties = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Add a string to object at property |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $property |
|
37
|
|
|
* @param string $value |
|
38
|
|
|
*/ |
|
39
|
9 |
|
public function addString(string $property, ?string $value) |
|
40
|
|
|
{ |
|
41
|
9 |
|
$this->add($property, JsonPrimitive::create($value)); |
|
42
|
9 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Add an integer to object at property |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $property |
|
48
|
|
|
* @param int $value |
|
49
|
|
|
*/ |
|
50
|
4 |
|
public function addInteger(string $property, ?int $value) |
|
51
|
|
|
{ |
|
52
|
4 |
|
$this->add($property, JsonPrimitive::create($value)); |
|
53
|
4 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Add a float to object at property |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $property |
|
59
|
|
|
* @param float $value |
|
60
|
|
|
*/ |
|
61
|
2 |
|
public function addFloat(string $property, ?float $value) |
|
62
|
|
|
{ |
|
63
|
2 |
|
$this->add($property, JsonPrimitive::create($value)); |
|
64
|
2 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Add a boolean to object at property |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $property |
|
70
|
|
|
* @param bool $value |
|
71
|
|
|
*/ |
|
72
|
4 |
|
public function addBoolean(string $property, ?bool $value) |
|
73
|
|
|
{ |
|
74
|
4 |
|
$this->add($property, JsonPrimitive::create($value)); |
|
75
|
4 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Add an element to object at property |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $property |
|
81
|
|
|
* @param JsonElement $jsonElement |
|
82
|
|
|
*/ |
|
83
|
28 |
|
public function add(string $property, JsonElement $jsonElement) |
|
84
|
|
|
{ |
|
85
|
28 |
|
$this->properties[$property] = $jsonElement; |
|
86
|
28 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns true if the object has property |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $property |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function has(string $property): bool |
|
95
|
|
|
{ |
|
96
|
1 |
|
return isset($this->properties[$property]); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get the value at property |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $property |
|
103
|
|
|
* @return JsonElement |
|
104
|
|
|
*/ |
|
105
|
10 |
|
public function get(string $property): JsonElement |
|
106
|
|
|
{ |
|
107
|
10 |
|
return $this->properties[$property]; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Convenience method to get a value and ensure it's a primitive |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $property |
|
114
|
|
|
* @return JsonPrimitive |
|
115
|
|
|
* @throws \LogicException |
|
116
|
|
|
*/ |
|
117
|
9 |
|
public function getAsJsonPrimitive(string $property): JsonPrimitive |
|
118
|
|
|
{ |
|
119
|
|
|
/** @var JsonPrimitive $jsonElement */ |
|
120
|
9 |
|
$jsonElement = $this->properties[$property]; |
|
121
|
|
|
|
|
122
|
9 |
|
if (!$jsonElement->isJsonPrimitive()) { |
|
123
|
1 |
|
throw new LogicException('This value is not a primitive'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
8 |
|
return $jsonElement; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Convenience method to get a value and ensure it's an object |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $property |
|
133
|
|
|
* @return JsonObject |
|
134
|
|
|
* @throws \LogicException |
|
135
|
|
|
*/ |
|
136
|
2 |
|
public function getAsJsonObject(string $property): JsonObject |
|
137
|
|
|
{ |
|
138
|
|
|
/** @var JsonObject $jsonElement */ |
|
139
|
2 |
|
$jsonElement = $this->properties[$property]; |
|
140
|
|
|
|
|
141
|
2 |
|
if (!$jsonElement->isJsonObject()) { |
|
142
|
1 |
|
throw new LogicException('This value is not an object'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
1 |
|
return $jsonElement; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Convenience method to get a value and ensure it's an array |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $property |
|
152
|
|
|
* @return JsonArray |
|
153
|
|
|
* @throws \LogicException |
|
154
|
|
|
*/ |
|
155
|
2 |
|
public function getAsJsonArray(string $property): JsonArray |
|
156
|
|
|
{ |
|
157
|
|
|
/** @var JsonArray $jsonElement */ |
|
158
|
2 |
|
$jsonElement = $this->properties[$property]; |
|
159
|
|
|
|
|
160
|
2 |
|
if (!$jsonElement->isJsonArray()) { |
|
161
|
1 |
|
throw new LogicException('This value is not an array'); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
1 |
|
return $jsonElement; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Get property as string |
|
169
|
|
|
* |
|
170
|
|
|
* @param string $property |
|
171
|
|
|
* @return string |
|
172
|
|
|
*/ |
|
173
|
4 |
|
public function getAsString(string $property): string |
|
174
|
|
|
{ |
|
175
|
4 |
|
return $this->getAsJsonPrimitive($property)->asString(); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Get property as integer |
|
180
|
|
|
* |
|
181
|
|
|
* @param string $property |
|
182
|
|
|
* @return int |
|
183
|
|
|
*/ |
|
184
|
1 |
|
public function getAsInteger(string $property): int |
|
185
|
|
|
{ |
|
186
|
1 |
|
return $this->getAsJsonPrimitive($property)->asInteger(); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Get property as float |
|
191
|
|
|
* |
|
192
|
|
|
* @param string $property |
|
193
|
|
|
* @return float |
|
194
|
|
|
*/ |
|
195
|
1 |
|
public function getAsFloat(string $property): float |
|
196
|
|
|
{ |
|
197
|
1 |
|
return $this->getAsJsonPrimitive($property)->asFloat(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Get property as boolean |
|
202
|
|
|
* |
|
203
|
|
|
* @param string $property |
|
204
|
|
|
* @return boolean |
|
205
|
|
|
*/ |
|
206
|
1 |
|
public function getAsBoolean(string $property): bool |
|
207
|
|
|
{ |
|
208
|
1 |
|
return $this->getAsJsonPrimitive($property)->asBoolean(); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Get property as array |
|
213
|
|
|
* |
|
214
|
|
|
* @param string $property |
|
215
|
|
|
* @return array |
|
216
|
|
|
*/ |
|
217
|
1 |
|
public function getAsArray(string $property): array |
|
218
|
|
|
{ |
|
219
|
1 |
|
return \json_decode(\json_encode($this->get($property)), true); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Get the value as a JsonObject |
|
224
|
|
|
* |
|
225
|
|
|
* @return JsonObject |
|
226
|
|
|
*/ |
|
227
|
4 |
|
public function asJsonObject(): JsonObject |
|
228
|
|
|
{ |
|
229
|
4 |
|
return $this; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Remove property from object |
|
234
|
|
|
* |
|
235
|
|
|
* @param string $property |
|
236
|
|
|
* @return bool |
|
237
|
|
|
*/ |
|
238
|
1 |
|
public function remove(string $property): bool |
|
239
|
|
|
{ |
|
240
|
1 |
|
if (!isset($this->properties[$property])) { |
|
241
|
1 |
|
return false; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
1 |
|
unset($this->properties[$property]); |
|
245
|
|
|
|
|
246
|
1 |
|
return true; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Specify data which should be serialized to JSON |
|
251
|
|
|
* |
|
252
|
|
|
* @return stdClass |
|
253
|
|
|
*/ |
|
254
|
2 |
|
public function jsonSerialize(): stdClass |
|
255
|
|
|
{ |
|
256
|
2 |
|
$class = new stdClass(); |
|
257
|
2 |
|
foreach ($this->properties as $key => $property) { |
|
258
|
2 |
|
$class->$key = $property->jsonSerialize(); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
2 |
|
return $class; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Retrieve an external iterator |
|
266
|
|
|
* |
|
267
|
|
|
* @return ArrayIterator |
|
268
|
|
|
*/ |
|
269
|
1 |
|
public function getIterator(): ArrayIterator |
|
270
|
|
|
{ |
|
271
|
1 |
|
return new ArrayIterator($this->properties); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Get the object properties |
|
276
|
|
|
* |
|
277
|
|
|
* @return JsonElement[] |
|
278
|
|
|
*/ |
|
279
|
3 |
|
public function getProperties(): array |
|
280
|
|
|
{ |
|
281
|
3 |
|
return $this->properties; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Returns the number of properties in object |
|
286
|
|
|
* |
|
287
|
|
|
* @return int |
|
288
|
|
|
*/ |
|
289
|
1 |
|
public function count(): int |
|
290
|
|
|
{ |
|
291
|
1 |
|
return \count($this->properties); |
|
292
|
|
|
} |
|
293
|
|
|
} |
|
294
|
|
|
|