Skip to content

Commit

Permalink
Fix, use JSON Pointer release handling array/object casting correctly -
Browse files Browse the repository at this point in the history
closes #33
  • Loading branch information
narcoticfresh authored and raphaelstolt committed Aug 22, 2016
1 parent 0700023 commit af14521
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=5.4",
"symfony/polyfill-mbstring": "^1.1",
"php-jsonpointer/php-jsonpointer": "^2.0"
"php-jsonpointer/php-jsonpointer": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "4.6.*",
Expand Down
7 changes: 7 additions & 0 deletions src/Rs/Json/Patch/Operations/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ public function perform($targetDocument)
$targetArray[$additionIndex] = $value;
}
}
}

if (is_object($targetArray)) {
$targetArray->{$additionIndex} = $value;
}

if (is_array($targetArray) || is_object($targetArray)) {
$augmentedDocument = &$targetDocument;
foreach ($pointerParts as $pointerPart) {
if (is_array($augmentedDocument)) {
Expand Down
22 changes: 19 additions & 3 deletions src/Rs/Json/Patch/Operations/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,35 @@ public function perform($targetDocument)
$get = $pointer->get($this->getPath());
// Pointer::get() method can return mixed result, we should force type to array for json string
if ($this->isValidJsonString($get)) {
$get = json_decode($get, true);
$get = json_decode($get);
}
} catch (NonexistentValueReferencedException $e) {
$get = null;
}

$value = is_object($this->getValue()) ? (array) $this->getValue() : $this->getValue();
$value = $this->getValue();

/**
* to remain backwards compatible, we support testing a $value of array with non-numeric indexes
* to a $get of object.. in that case, we cast $value to object
*/
if (is_array($value) && !empty($value)) {
// in if to remain php 5.4 compatible
$keys = array_keys($value);
if (!ctype_digit((string) $keys[0])) {
$value = (object) $value;
}
}

if (is_array($value) && is_array($get)) {
return $this->arraysAreIdentical($value, $get);
}

return $get === $this->getValue();
if (is_object($value) && is_object($get)) {
return ($get == $value);
}

return ($get === $value);
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/integration/Rs/Json/PatchAddTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ public function shouldAddEmptyObject()
$patchedDocument
);
}
/**
* @test
* @ticket 33 (https://github.com/raphaelstolt/php-jsonpatch/issues/33)
*/
public function shouldPreserveEmptyObjectSameLevel()
{
$targetDocument = '{"foo": {"bar": {"baz": {"boo": {}, "qux": "value"}}}}';
$patchDocument = '[{"op":"add", "path":"/foo/bar/baz/qux", "value":"otherValue"}]';
$expectedDocument = '{"foo": {"bar": {"baz": {"boo": {}, "qux": "otherValue"}}}}';

$patch = new Patch($targetDocument, $patchDocument);
$patchedDocument = $patch->apply();

$this->assertJsonStringEqualsJsonString(
$expectedDocument,
$patchedDocument
);
}
/**
* @test
*/
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/Rs/Json/PatchReplaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,23 @@ public function shouldPreserveObjects()
$patchedDocument
);
}

/**
* @test
* @ticket 33 (https://github.com/raphaelstolt/php-jsonpatch/issues/33)
*/
public function shouldPreserveObjectsSameLevel()
{
$targetDocument = '{"foo": {"bar": {"baz": {"boo": {}, "qux": "value"}}}}';
$patchDocument = '[{"op":"replace", "path":"/foo/bar/baz/qux", "value":"otherValue"}]';
$expectedDocument = '{"foo": {"bar": {"baz": {"boo": {}, "qux": "otherValue"}}}}';

$patch = new Patch($targetDocument, $patchDocument);
$patchedDocument = $patch->apply();

$this->assertJsonStringEqualsJsonString(
$expectedDocument,
$patchedDocument
);
}
}
21 changes: 21 additions & 0 deletions tests/unit/Rs/Json/Patch/Operations/AddTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,27 @@ public function shouldKeepObjectsAsObjects()
);
}

/**
* @test
* @ticket 33 (https://github.com/raphaelstolt/php-jsonpatch/issues/33)
*/
public function shouldPreserveEmptyObjectSameLevel()
{
$targetJson = '{"foo": {"bar": {"baz": {"boo": {}, "qux": "value"}}}}';
$expectedJson = '{"foo": {"bar": {"baz": {"boo": {}, "qux": "otherValue"}}}}';

$operation = new \stdClass;
$operation->path = '/foo/bar/baz/qux';
$operation->value = 'otherValue';

$addOperation = new Add($operation);

$this->assertJsonStringEqualsJsonString(
$expectedJson,
$addOperation->perform($targetJson)
);
}

/**
* @test
*/
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/Rs/Json/Patch/Operations/ReplaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ public function shouldKeepObjectsAsObjects()
);
}

/**
* @test
* @ticket 33 (https://github.com/raphaelstolt/php-jsonpatch/issues/33)
*/
public function shouldPreserveEmptyObjectSameLevel()
{
$targetJson = '{"foo": {"bar": {"baz": {"boo": {}, "qux": "value"}}}}';
$expectedJson = '{"foo": {"bar": {"baz": {"boo": {}, "qux": "otherValue"}}}}';

$operation = new \stdClass;
$operation->path = '/foo/bar/baz/qux';
$operation->value = 'otherValue';

$replaceOperation = new Replace($operation);

$this->assertJsonStringEqualsJsonString(
$expectedJson,
$replaceOperation->perform($targetJson)
);
}

/**
* @test
* @ticket 5 (https://github.com/raphaelstolt/php-jsonpatch/issues/5)
Expand Down

0 comments on commit af14521

Please sign in to comment.