Skip to content

Commit 0832307

Browse files
authored
Merge pull request #30 from WebFiori/dev
Fix to Doublications Bug
2 parents 3208b25 + 69e3e66 commit 0832307

2 files changed

Lines changed: 103 additions & 36 deletions

File tree

tests/JsonTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,19 @@ public function testAdd08() {
816816
.'{"property-00":"p0","property-01":"p1","property-02":"p2"}]]]'
817817
.'}',$j->toJSONString());
818818
}
819+
/**
820+
* @test
821+
*/
822+
public function testAdd09() {
823+
$j = new Json();
824+
$j->add('one', 'one');
825+
$this->assertEquals(1, count($j->getPropsNames()));
826+
$j->add('two', 'two');
827+
$this->assertEquals(2, count($j->getPropsNames()));
828+
$j->add('one', 'not one');
829+
$this->assertEquals(2, count($j->getPropsNames()));
830+
$this->assertEquals('not one', $j->get('one'));
831+
}
819832
/**
820833
* @test
821834
*/

webfiori/json/Json.php

Lines changed: 90 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,27 @@ public function &get($key) {
142142

143143
return $retVal;
144144
}
145+
/**
146+
* Returns a property object given its key.
147+
*
148+
* @param string $key The key of the property.
149+
*
150+
* @return Property|null if a property which has the given key exist, it
151+
* will be returned. Other than that, null is returned.
152+
*/
153+
public function &getProperty(string $key) {
154+
$keyTrimmed = CaseConverter::convert($key, $this->getPropStyle());
155+
$retVal = null;
156+
157+
foreach ($this->getProperties() as $val) {
158+
if ($val->getName() == $keyTrimmed) {
159+
$retVal = $val;
160+
break;
161+
}
162+
}
163+
164+
return $retVal;
165+
}
145166
/**
146167
* Returns the data on the object as a JSON string.
147168
*
@@ -173,11 +194,15 @@ public function __toString() {
173194
*/
174195
public function add(string $key, $value, $arrayAsObj = false) {
175196
if ($value !== null) {
176-
return $this->addString($key, $value) ||
177-
$this->addArray($key, $value, $arrayAsObj) ||
178-
$this->addBoolean($key, $value) ||
179-
$this->addNumber($key, $value) ||
180-
$this->addObject($key, $value);
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;
181206
} else {
182207
$prop = $this->createProb($key, $value);
183208

@@ -205,17 +230,22 @@ public function add(string $key, $value, $arrayAsObj = false) {
205230
* or the given value is not an array.
206231
*/
207232
public function addArray(string $key, $value, $asObject = false) {
208-
$prop = $this->createProb($key, $value);
209-
$propType = $prop->getType();
233+
if (!$this->updateExisting($key, $value)) {
234+
$prop = $this->createProb($key, $value);
235+
$propType = $prop->getType();
210236

211-
if ($prop !== null && $propType == JsonTypes::ARR) {
212-
$prop->setAsObject($asObject);
213-
$this->propsArr[] = $prop;
237+
if ($prop !== null && $propType == JsonTypes::ARR) {
238+
$prop->setAsObject($asObject);
239+
$this->propsArr[] = $prop;
240+
241+
return true;
242+
}
214243

244+
return false;
245+
} else {
246+
$this->getProperty($key)->setAsObject($asObject);
215247
return true;
216248
}
217-
218-
return false;
219249
}
220250
/**
221251
* Adds a boolean value (true or false) to the JSON data.
@@ -232,15 +262,18 @@ public function addArray(string $key, $value, $asObject = false) {
232262
* @since 1.0
233263
*/
234264
public function addBoolean($key, $val = true) {
235-
$prop = $this->createProb($key, $val);
265+
if (!$this->updateExisting($key, $val)) {
266+
$prop = $this->createProb($key, $val);
236267

237-
if ($prop !== null && $prop->getType() == 'boolean') {
238-
$this->propsArr[] = $prop;
268+
if ($prop !== null && $prop->getType() == 'boolean') {
269+
$this->propsArr[] = $prop;
239270

240-
return true;
241-
}
271+
return true;
272+
}
242273

243-
return false;
274+
return false;
275+
}
276+
return true;
244277
}
245278
/**
246279
* Adds multiple values to the object.
@@ -276,16 +309,19 @@ public function addMultiple(array $arr) {
276309
* @since 1.0
277310
*/
278311
public function addNumber(string $key, $value) {
279-
$prop = $this->createProb($key, $value);
280-
$propType = $prop->getType();
312+
if (!$this->updateExisting($key, $value)) {
313+
$prop = $this->createProb($key, $value);
314+
$propType = $prop->getType();
281315

282-
if ($prop !== null && $propType == JsonTypes::INT || $propType == JsonTypes::DOUBLE) {
283-
$this->propsArr[] = $prop;
316+
if ($prop !== null && $propType == JsonTypes::INT || $propType == JsonTypes::DOUBLE) {
317+
$this->propsArr[] = $prop;
284318

285-
return true;
286-
}
319+
return true;
320+
}
287321

288-
return false;
322+
return false;
323+
}
324+
return true;
289325
}
290326
/**
291327
* Adds an object to the JSON string.
@@ -310,16 +346,19 @@ public function addNumber(string $key, $value) {
310346
* @since 1.0
311347
*/
312348
public function addObject(string $key, $val) {
313-
$prop = $this->createProb($key, $val);
314-
$propType = $prop->getType();
349+
if (!$this->updateExisting($key, $val)) {
350+
$prop = $this->createProb($key, $val);
351+
$propType = $prop->getType();
315352

316-
if ($prop !== null && $propType == JsonTypes::OBJ) {
317-
$this->propsArr[] = $prop;
353+
if ($prop !== null && $propType == JsonTypes::OBJ) {
354+
$this->propsArr[] = $prop;
318355

319-
return true;
320-
}
356+
return true;
357+
}
321358

322-
return false;
359+
return false;
360+
}
361+
return true;
323362
}
324363
/**
325364
* Adds a new key to the JSON data with its value as string.
@@ -335,16 +374,31 @@ public function addObject(string $key, $val) {
335374
* @since 1.0
336375
*/
337376
public function addString(string $key, $val) {
338-
$prop = $this->createProb($key, $val);
377+
if (!$this->updateExisting($key, $val)) {
378+
$prop = $this->createProb($key, $val);
339379

340-
if ($prop !== null && $prop->getType() == JsonTypes::STRING) {
341-
$this->propsArr[] = $prop;
380+
if ($prop !== null && $prop->getType() == JsonTypes::STRING) {
381+
$this->propsArr[] = $prop;
342382

383+
return true;
384+
}
385+
386+
return false;
387+
}
388+
return true;
389+
}
390+
private function updateExisting($key, $val) {
391+
$tempProp = $this->getProperty($key);
392+
393+
if ($tempProp !== null) {
394+
$tempProp->setValue($val);
395+
343396
return true;
344397
}
345-
398+
346399
return false;
347400
}
401+
348402
/**
349403
* Converts a JSON-like string to JSON object.
350404
*

0 commit comments

Comments
 (0)