@@ -172,12 +172,13 @@ public function __toString() {
172
172
return $ this ->toJSONString ();
173
173
}
174
174
/**
175
- * Adds a new value to the JSON string .
175
+ * Adds a new value to JSON.
176
176
*
177
177
* This method can be used to add an integer, a double,
178
178
* a string, an array or an object. If null is given, the method will
179
179
* set the value at the given key to null. If the given value or key is
180
180
* invalid, the method will not add the value and will return false.
181
+ * This method also can be used to update the value of an existing property.
181
182
*
182
183
* @param string $key The value of the key.
183
184
*
@@ -193,31 +194,21 @@ public function __toString() {
193
194
* @since 1.1
194
195
*/
195
196
public function add (string $ key , $ value , $ arrayAsObj = false ) {
196
- if ($ value !== null ) {
197
- if (!$ this ->updateExisting ($ key , $ value )) {
198
- return $ this ->addString ($ key , $ value ) ||
199
- $ this ->addArray ($ key , $ value , $ arrayAsObj ) ||
200
- $ this ->addBoolean ($ key , $ value ) ||
201
- $ this ->addNumber ($ key , $ value ) ||
202
- $ this ->addObject ($ key , $ value );
203
- }
204
- $ this ->getProperty ($ key )->setAsObject ($ arrayAsObj );
205
- return true ;
206
- } else {
207
- $ prop = $ this ->createProb ($ key , $ value );
208
-
209
- if ($ prop !== null ) {
210
- $ this ->propsArr [] = $ prop ;
211
-
212
- return true ;
213
- }
197
+ if (!$ this ->updateExisting ($ key , $ value )) {
198
+ return $ this ->addString ($ key , $ value ) ||
199
+ $ this ->addArray ($ key , $ value , $ arrayAsObj ) ||
200
+ $ this ->addBoolean ($ key , $ value ) ||
201
+ $ this ->addNumber ($ key , $ value ) ||
202
+ $ this ->addObject ($ key , $ value ) ||
203
+ $ this ->addNull ($ key );
214
204
}
215
-
216
- return false ;
205
+ return true ;
217
206
}
218
207
/**
219
208
* Adds an array to the JSON.
220
209
*
210
+ * This method also can be used to update the value of an existing property.
211
+ *
221
212
* @param string $key The name of the key.
222
213
*
223
214
* @param array $value The array that will be added.
@@ -234,7 +225,7 @@ public function addArray(string $key, $value, $asObject = false) {
234
225
$ prop = $ this ->createProb ($ key , $ value );
235
226
$ propType = $ prop ->getType ();
236
227
237
- if ($ prop !== null && $ propType == JsonTypes::ARR ) {
228
+ if ($ propType == JsonTypes::ARR ) {
238
229
$ prop ->setAsObject ($ asObject );
239
230
$ this ->propsArr [] = $ prop ;
240
231
@@ -244,12 +235,15 @@ public function addArray(string $key, $value, $asObject = false) {
244
235
return false ;
245
236
} else {
246
237
$ this ->getProperty ($ key )->setAsObject ($ asObject );
238
+
247
239
return true ;
248
240
}
249
241
}
250
242
/**
251
243
* Adds a boolean value (true or false) to the JSON data.
252
244
*
245
+ * This method also can be used to update the value of an existing property.
246
+ *
253
247
* @param string $key The name of the key.
254
248
*
255
249
* @param boolean $val true or false. If not specified,
@@ -265,14 +259,15 @@ public function addBoolean($key, $val = true) {
265
259
if (!$ this ->updateExisting ($ key , $ val )) {
266
260
$ prop = $ this ->createProb ($ key , $ val );
267
261
268
- if ($ prop !== null && $ prop ->getType () == 'boolean ' ) {
262
+ if ($ prop ->getType () == 'boolean ' ) {
269
263
$ this ->propsArr [] = $ prop ;
270
264
271
265
return true ;
272
266
}
273
267
274
268
return false ;
275
269
}
270
+
276
271
return true ;
277
272
}
278
273
/**
@@ -290,12 +285,41 @@ public function addMultiple(array $arr) {
290
285
$ this ->add ($ key , $ value );
291
286
}
292
287
}
288
+ /**
289
+ * Adds a 'null' value to JSON.
290
+ *
291
+ * This method also can be used to update the value of an existing property.
292
+ *
293
+ * @param string $key The name of value key.
294
+ *
295
+ * @return boolean The method will return true if the value is set.
296
+ * If the given value or key is invalid, the method will return false.
297
+ */
298
+ public function addNull (string $ key ) {
299
+ $ nul = null ;
300
+
301
+ if (!$ this ->updateExisting ($ key , $ nul )) {
302
+ $ prop = $ this ->createProb ($ key , $ nul );
303
+ $ propType = $ prop ->getType ();
304
+
305
+ if ($ propType == JsonTypes::NUL ) {
306
+ $ this ->propsArr [] = $ prop ;
307
+
308
+ return true ;
309
+ }
310
+
311
+ return false ;
312
+ }
313
+
314
+ return true ;
315
+ }
293
316
/**
294
317
* Adds a number to the JSON data.
295
318
*
296
319
* Note that if the given number is the constant <b>INF</b> or the constant
297
320
* <b>NAN</b>, The method will add them as a string. The 'INF' will be added
298
321
* as the string "Infinity" and the 'NAN' will be added as the string "Nan".
322
+ * This method also can be used to update the value of an existing property.
299
323
*
300
324
* @param string $key The name of the key.
301
325
*
@@ -313,14 +337,15 @@ public function addNumber(string $key, $value) {
313
337
$ prop = $ this ->createProb ($ key , $ value );
314
338
$ propType = $ prop ->getType ();
315
339
316
- if ($ prop !== null && $ propType == JsonTypes::INT || $ propType == JsonTypes::DOUBLE ) {
340
+ if ($ propType == JsonTypes::INT || $ propType == JsonTypes::DOUBLE ) {
317
341
$ this ->propsArr [] = $ prop ;
318
342
319
343
return true ;
320
344
}
321
345
322
346
return false ;
323
347
}
348
+
324
349
return true ;
325
350
}
326
351
/**
@@ -335,6 +360,7 @@ public function addNumber(string $key, $value) {
335
360
* <code>getFirstProp()</code> and <code>getSecondProp()</code>.
336
361
* In that case, the generated JSON will be on the formate
337
362
* <b>{"FirstProp":"prop-1","SecondProp":""}</b>.
363
+ * This method also can be used to update the value of an existing property.
338
364
*
339
365
* @param string $key The key value.
340
366
*
@@ -345,24 +371,27 @@ public function addNumber(string $key, $value) {
345
371
*
346
372
* @since 1.0
347
373
*/
348
- public function addObject (string $ key , $ val ) {
374
+ public function addObject (string $ key , & $ val ) {
349
375
if (!$ this ->updateExisting ($ key , $ val )) {
350
376
$ prop = $ this ->createProb ($ key , $ val );
351
377
$ propType = $ prop ->getType ();
352
378
353
- if ($ prop !== null && $ propType == JsonTypes::OBJ ) {
379
+ if ($ propType == JsonTypes::OBJ ) {
354
380
$ this ->propsArr [] = $ prop ;
355
381
356
382
return true ;
357
383
}
358
384
359
385
return false ;
360
386
}
387
+
361
388
return true ;
362
389
}
363
390
/**
364
391
* Adds a new key to the JSON data with its value as string.
365
392
*
393
+ * This method also can be used to update the value of an existing property.
394
+ *
366
395
* @param string $key The name of the key. Must be non empty string.
367
396
*
368
397
* @param string $val The value of the string.
@@ -377,27 +406,17 @@ public function addString(string $key, $val) {
377
406
if (!$ this ->updateExisting ($ key , $ val )) {
378
407
$ prop = $ this ->createProb ($ key , $ val );
379
408
380
- if ($ prop !== null && $ prop ->getType () == JsonTypes::STRING ) {
409
+ if ($ prop ->getType () == JsonTypes::STRING ) {
381
410
$ this ->propsArr [] = $ prop ;
382
411
383
412
return true ;
384
413
}
385
414
386
415
return false ;
387
416
}
417
+
388
418
return true ;
389
419
}
390
- private function updateExisting ($ key , $ val ) {
391
- $ tempProp = $ this ->getProperty ($ key );
392
-
393
- if ($ tempProp !== null ) {
394
- $ tempProp ->setValue ($ val );
395
-
396
- return true ;
397
- }
398
-
399
- return false ;
400
- }
401
420
402
421
/**
403
422
* Converts a JSON-like string to JSON object.
@@ -764,8 +783,19 @@ private function _setIsFormattedArray(&$arr) {
764
783
private function createProb ($ name , $ value ) {
765
784
try {
766
785
return new Property ($ name , $ value , $ this ->getPropStyle ());
767
- } catch (\Exception $ ex ) {
768
- return null ;
786
+ } catch (InvalidArgumentException $ ex ) {
787
+ throw new InvalidArgumentException ($ ex ->getMessage (), $ ex ->getCode (), $ ex );
788
+ }
789
+ }
790
+ private function updateExisting ($ key , &$ val ) {
791
+ $ tempProp = $ this ->getProperty ($ key );
792
+
793
+ if ($ tempProp !== null ) {
794
+ $ tempProp ->setValue ($ val );
795
+
796
+ return true ;
769
797
}
798
+
799
+ return false ;
770
800
}
771
801
}
0 commit comments