Skip to content

Commit 6074e4f

Browse files
authored
Merge pull request #32 from WebFiori/dev
Added Support for Adding a Null Value
2 parents f49f79e + 08eb8cf commit 6074e4f

File tree

6 files changed

+147
-52
lines changed

6 files changed

+147
-52
lines changed

phpunit.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
</php>
55
<filter>
66
<whitelist addUncoveredFilesFromWhitelist="true">
7-
<directory>./webfiori</directory>
7+
<directory>./webfiori/json</directory>
88
</whitelist>
99
</filter>
1010
<logging>
1111
<log type="coverage-clover" target="clover.xml"/>
1212
</logging>
1313
<testsuites>
1414
<testsuite name="Json Tests">
15-
<directory>./tests</directory>
15+
<directory>./tests/webfiori/tests/json</directory>
1616
</testsuite>
1717
</testsuites>
1818
</phpunit>

tests/JsonConverterTest.php tests/webfiori/tests/json/JsonConverterTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace jsonx\tests;
3+
namespace webfiori\tests\json;
44

55
use webfiori\json\Json;
66
use jsonx\tests\Obj0;

tests/JsonTest.php tests/webfiori/tests/json/JsonTest.php

+68-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
namespace webfiori\tests\json;
23

34
use webfiori\json\Json;
45
use jsonx\tests\Obj0;
@@ -735,9 +736,11 @@ public function testAdd04() {
735736
*/
736737
public function testAdd05() {
737738
$j = new Json();
738-
$this->assertFalse($j->add(' ',null));
739739
$this->assertTrue($j->add('null-value',null));
740740
$this->assertEquals('{"null-value":null}',$j->toJSONString());
741+
$this->expectException(\InvalidArgumentException::class);
742+
$this->assertFalse($j->add(' ',null));
743+
741744
}
742745
/**
743746
* @test
@@ -814,6 +817,43 @@ public function testAdd09() {
814817
$j->addNumber('one', 1);
815818
$this->assertEquals(1, $j->get('one'));
816819
}
820+
/**
821+
* @test
822+
*/
823+
public function testAdd10() {
824+
$j = new Json();
825+
$j->add('one', 'one');
826+
$this->assertEquals(1, count($j->getPropsNames()));
827+
$j->add('one', null);
828+
$this->assertEquals(1, count($j->getPropsNames()));
829+
$this->assertNull($j->get('one'));
830+
}
831+
/**
832+
* @test
833+
*/
834+
public function testAddNull00() {
835+
$j = new Json();
836+
$this->assertTrue($j->addNull('null'));
837+
$this->assertNull($j->get('null'));
838+
}
839+
/**
840+
* @test
841+
*/
842+
public function testAddNull01() {
843+
$j = new Json();
844+
$this->expectException(\InvalidArgumentException::class);
845+
$this->assertTrue($j->addNull(' '));
846+
}
847+
/**
848+
* @test
849+
*/
850+
public function testAddNull02() {
851+
$j = new Json();
852+
$j->addString('not-null', 'Hello');
853+
$this->assertEquals('Hello', $j->get('not-null'));
854+
$this->assertTrue($j->addNull('not-null'));
855+
$this->assertNull($j->get('not-null'));
856+
}
817857
/**
818858
* @test
819859
*/
@@ -976,7 +1016,19 @@ public function testAddObj02() {
9761016
/**
9771017
* @test
9781018
*/
979-
public function testAddStringTest00() {
1019+
public function testAddObj03() {
1020+
$j = new Json();
1021+
$obj = new Obj1('Hello',0,true,null,'he');
1022+
$j->addString('object','An Obj');
1023+
$this->assertEquals('An Obj', $j->get('object'));
1024+
$j->addObject('object', $obj);
1025+
$this->assertEquals('{"object":{"property-00":"Hello","property-01":0,"property-02":true}}',$j.'');
1026+
}
1027+
/**
1028+
* @test
1029+
*/
1030+
public function testAddString00() {
1031+
$this->expectException(\InvalidArgumentException::class);
9801032
$j = new Json();
9811033
$this->assertFalse($j->addString('','Hello World!'));
9821034
$this->assertFalse($j->addString(' ','Hello World!'));
@@ -986,18 +1038,30 @@ public function testAddStringTest00() {
9861038
/**
9871039
* @test
9881040
*/
989-
public function testAddStringTest01() {
1041+
public function testAddString01() {
9901042
$j = new Json();
9911043
$this->assertTrue($j->addString('hello','Hello World!'));
9921044
$this->assertEquals('{"hello":"Hello World!"}',$j.'');
1045+
$this->assertFalse($j->addString('a-number', 33));
9931046
}
9941047
/**
9951048
* @test
9961049
*/
997-
public function testAddStringTest02() {
1050+
public function testAddString02() {
9981051
$j = new Json();
9991052
$this->assertFalse($j->addBoolean('invalid-boolean','falseX'));
10001053
}
1054+
/**
1055+
* @test
1056+
*/
1057+
public function testAddString03() {
1058+
$j = new Json();
1059+
$j->addNumber('a-number', 33);
1060+
$this->assertSame(33, $j->get('a-number'));
1061+
$j->addString('a-number', '33');
1062+
$this->assertNotSame(33, $j->get('a-number'));
1063+
$this->assertEquals('33', $j->get('a-number'));
1064+
}
10011065
/**
10021066
* @test
10031067
*/

tests/PropertyTest.php tests/webfiori/tests/json/PropertyTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
namespace jsonx\tests;
2+
namespace webfiori\tests\json;
33

44
use webfiori\json\Json;
55
use jsonx\tests\Obj0;

webfiori/json/Json.php

+70-40
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)