@@ -172,12 +172,13 @@ public function __toString() {
172172 return $ this ->toJSONString ();
173173 }
174174 /**
175- * Adds a new value to the JSON string .
175+ * Adds a new value to JSON.
176176 *
177177 * This method can be used to add an integer, a double,
178178 * a string, an array or an object. If null is given, the method will
179179 * set the value at the given key to null. If the given value or key is
180180 * 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.
181182 *
182183 * @param string $key The value of the key.
183184 *
@@ -193,31 +194,21 @@ public function __toString() {
193194 * @since 1.1
194195 */
195196 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 );
214204 }
215-
216- return false ;
205+ return true ;
217206 }
218207 /**
219208 * Adds an array to the JSON.
220209 *
210+ * This method also can be used to update the value of an existing property.
211+ *
221212 * @param string $key The name of the key.
222213 *
223214 * @param array $value The array that will be added.
@@ -234,7 +225,7 @@ public function addArray(string $key, $value, $asObject = false) {
234225 $ prop = $ this ->createProb ($ key , $ value );
235226 $ propType = $ prop ->getType ();
236227
237- if ($ prop !== null && $ propType == JsonTypes::ARR ) {
228+ if ($ propType == JsonTypes::ARR ) {
238229 $ prop ->setAsObject ($ asObject );
239230 $ this ->propsArr [] = $ prop ;
240231
@@ -244,12 +235,15 @@ public function addArray(string $key, $value, $asObject = false) {
244235 return false ;
245236 } else {
246237 $ this ->getProperty ($ key )->setAsObject ($ asObject );
238+
247239 return true ;
248240 }
249241 }
250242 /**
251243 * Adds a boolean value (true or false) to the JSON data.
252244 *
245+ * This method also can be used to update the value of an existing property.
246+ *
253247 * @param string $key The name of the key.
254248 *
255249 * @param boolean $val true or false. If not specified,
@@ -265,14 +259,15 @@ public function addBoolean($key, $val = true) {
265259 if (!$ this ->updateExisting ($ key , $ val )) {
266260 $ prop = $ this ->createProb ($ key , $ val );
267261
268- if ($ prop !== null && $ prop ->getType () == 'boolean ' ) {
262+ if ($ prop ->getType () == 'boolean ' ) {
269263 $ this ->propsArr [] = $ prop ;
270264
271265 return true ;
272266 }
273267
274268 return false ;
275269 }
270+
276271 return true ;
277272 }
278273 /**
@@ -290,12 +285,41 @@ public function addMultiple(array $arr) {
290285 $ this ->add ($ key , $ value );
291286 }
292287 }
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+ }
293316 /**
294317 * Adds a number to the JSON data.
295318 *
296319 * Note that if the given number is the constant <b>INF</b> or the constant
297320 * <b>NAN</b>, The method will add them as a string. The 'INF' will be added
298321 * 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.
299323 *
300324 * @param string $key The name of the key.
301325 *
@@ -313,14 +337,15 @@ public function addNumber(string $key, $value) {
313337 $ prop = $ this ->createProb ($ key , $ value );
314338 $ propType = $ prop ->getType ();
315339
316- if ($ prop !== null && $ propType == JsonTypes::INT || $ propType == JsonTypes::DOUBLE ) {
340+ if ($ propType == JsonTypes::INT || $ propType == JsonTypes::DOUBLE ) {
317341 $ this ->propsArr [] = $ prop ;
318342
319343 return true ;
320344 }
321345
322346 return false ;
323347 }
348+
324349 return true ;
325350 }
326351 /**
@@ -335,6 +360,7 @@ public function addNumber(string $key, $value) {
335360 * <code>getFirstProp()</code> and <code>getSecondProp()</code>.
336361 * In that case, the generated JSON will be on the formate
337362 * <b>{"FirstProp":"prop-1","SecondProp":""}</b>.
363+ * This method also can be used to update the value of an existing property.
338364 *
339365 * @param string $key The key value.
340366 *
@@ -345,24 +371,27 @@ public function addNumber(string $key, $value) {
345371 *
346372 * @since 1.0
347373 */
348- public function addObject (string $ key , $ val ) {
374+ public function addObject (string $ key , & $ val ) {
349375 if (!$ this ->updateExisting ($ key , $ val )) {
350376 $ prop = $ this ->createProb ($ key , $ val );
351377 $ propType = $ prop ->getType ();
352378
353- if ($ prop !== null && $ propType == JsonTypes::OBJ ) {
379+ if ($ propType == JsonTypes::OBJ ) {
354380 $ this ->propsArr [] = $ prop ;
355381
356382 return true ;
357383 }
358384
359385 return false ;
360386 }
387+
361388 return true ;
362389 }
363390 /**
364391 * Adds a new key to the JSON data with its value as string.
365392 *
393+ * This method also can be used to update the value of an existing property.
394+ *
366395 * @param string $key The name of the key. Must be non empty string.
367396 *
368397 * @param string $val The value of the string.
@@ -377,27 +406,17 @@ public function addString(string $key, $val) {
377406 if (!$ this ->updateExisting ($ key , $ val )) {
378407 $ prop = $ this ->createProb ($ key , $ val );
379408
380- if ($ prop !== null && $ prop ->getType () == JsonTypes::STRING ) {
409+ if ($ prop ->getType () == JsonTypes::STRING ) {
381410 $ this ->propsArr [] = $ prop ;
382411
383412 return true ;
384413 }
385414
386415 return false ;
387416 }
417+
388418 return true ;
389419 }
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- }
401420
402421 /**
403422 * Converts a JSON-like string to JSON object.
@@ -764,8 +783,19 @@ private function _setIsFormattedArray(&$arr) {
764783 private function createProb ($ name , $ value ) {
765784 try {
766785 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 ;
769797 }
798+
799+ return false ;
770800 }
771801}
0 commit comments