Skip to content

Commit

Permalink
Merge pull request #30 from WebFiori/dev
Browse files Browse the repository at this point in the history
Fix to Doublications Bug
  • Loading branch information
usernane authored Apr 27, 2022
2 parents 3208b25 + 69e3e66 commit 0832307
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 36 deletions.
13 changes: 13 additions & 0 deletions tests/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,19 @@ public function testAdd08() {
.'{"property-00":"p0","property-01":"p1","property-02":"p2"}]]]'
.'}',$j->toJSONString());
}
/**
* @test
*/
public function testAdd09() {
$j = new Json();
$j->add('one', 'one');
$this->assertEquals(1, count($j->getPropsNames()));
$j->add('two', 'two');
$this->assertEquals(2, count($j->getPropsNames()));
$j->add('one', 'not one');
$this->assertEquals(2, count($j->getPropsNames()));
$this->assertEquals('not one', $j->get('one'));
}
/**
* @test
*/
Expand Down
126 changes: 90 additions & 36 deletions webfiori/json/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,27 @@ public function &get($key) {

return $retVal;
}
/**
* Returns a property object given its key.
*
* @param string $key The key of the property.
*
* @return Property|null if a property which has the given key exist, it
* will be returned. Other than that, null is returned.
*/
public function &getProperty(string $key) {
$keyTrimmed = CaseConverter::convert($key, $this->getPropStyle());
$retVal = null;

foreach ($this->getProperties() as $val) {
if ($val->getName() == $keyTrimmed) {
$retVal = $val;
break;
}
}

return $retVal;
}
/**
* Returns the data on the object as a JSON string.
*
Expand Down Expand Up @@ -173,11 +194,15 @@ public function __toString() {
*/
public function add(string $key, $value, $arrayAsObj = false) {
if ($value !== null) {
return $this->addString($key, $value) ||
$this->addArray($key, $value, $arrayAsObj) ||
$this->addBoolean($key, $value) ||
$this->addNumber($key, $value) ||
$this->addObject($key, $value);
if (!$this->updateExisting($key, $value)) {
return $this->addString($key, $value) ||
$this->addArray($key, $value, $arrayAsObj) ||
$this->addBoolean($key, $value) ||
$this->addNumber($key, $value) ||
$this->addObject($key, $value);
}
$this->getProperty($key)->setAsObject($arrayAsObj);
return true;
} else {
$prop = $this->createProb($key, $value);

Expand Down Expand Up @@ -205,17 +230,22 @@ public function add(string $key, $value, $arrayAsObj = false) {
* or the given value is not an array.
*/
public function addArray(string $key, $value, $asObject = false) {
$prop = $this->createProb($key, $value);
$propType = $prop->getType();
if (!$this->updateExisting($key, $value)) {
$prop = $this->createProb($key, $value);
$propType = $prop->getType();

if ($prop !== null && $propType == JsonTypes::ARR) {
$prop->setAsObject($asObject);
$this->propsArr[] = $prop;
if ($prop !== null && $propType == JsonTypes::ARR) {
$prop->setAsObject($asObject);
$this->propsArr[] = $prop;

return true;
}

return false;
} else {
$this->getProperty($key)->setAsObject($asObject);
return true;
}

return false;
}
/**
* Adds a boolean value (true or false) to the JSON data.
Expand All @@ -232,15 +262,18 @@ public function addArray(string $key, $value, $asObject = false) {
* @since 1.0
*/
public function addBoolean($key, $val = true) {
$prop = $this->createProb($key, $val);
if (!$this->updateExisting($key, $val)) {
$prop = $this->createProb($key, $val);

if ($prop !== null && $prop->getType() == 'boolean') {
$this->propsArr[] = $prop;
if ($prop !== null && $prop->getType() == 'boolean') {
$this->propsArr[] = $prop;

return true;
}
return true;
}

return false;
return false;
}
return true;
}
/**
* Adds multiple values to the object.
Expand Down Expand Up @@ -276,16 +309,19 @@ public function addMultiple(array $arr) {
* @since 1.0
*/
public function addNumber(string $key, $value) {
$prop = $this->createProb($key, $value);
$propType = $prop->getType();
if (!$this->updateExisting($key, $value)) {
$prop = $this->createProb($key, $value);
$propType = $prop->getType();

if ($prop !== null && $propType == JsonTypes::INT || $propType == JsonTypes::DOUBLE) {
$this->propsArr[] = $prop;
if ($prop !== null && $propType == JsonTypes::INT || $propType == JsonTypes::DOUBLE) {
$this->propsArr[] = $prop;

return true;
}
return true;
}

return false;
return false;
}
return true;
}
/**
* Adds an object to the JSON string.
Expand All @@ -310,16 +346,19 @@ public function addNumber(string $key, $value) {
* @since 1.0
*/
public function addObject(string $key, $val) {
$prop = $this->createProb($key, $val);
$propType = $prop->getType();
if (!$this->updateExisting($key, $val)) {
$prop = $this->createProb($key, $val);
$propType = $prop->getType();

if ($prop !== null && $propType == JsonTypes::OBJ) {
$this->propsArr[] = $prop;
if ($prop !== null && $propType == JsonTypes::OBJ) {
$this->propsArr[] = $prop;

return true;
}
return true;
}

return false;
return false;
}
return true;
}
/**
* Adds a new key to the JSON data with its value as string.
Expand All @@ -335,16 +374,31 @@ public function addObject(string $key, $val) {
* @since 1.0
*/
public function addString(string $key, $val) {
$prop = $this->createProb($key, $val);
if (!$this->updateExisting($key, $val)) {
$prop = $this->createProb($key, $val);

if ($prop !== null && $prop->getType() == JsonTypes::STRING) {
$this->propsArr[] = $prop;
if ($prop !== null && $prop->getType() == JsonTypes::STRING) {
$this->propsArr[] = $prop;

return true;
}

return false;
}
return true;
}
private function updateExisting($key, $val) {
$tempProp = $this->getProperty($key);

if ($tempProp !== null) {
$tempProp->setValue($val);

return true;
}

return false;
}

/**
* Converts a JSON-like string to JSON object.
*
Expand Down

0 comments on commit 0832307

Please sign in to comment.