Skip to content

Commit 83edbd6

Browse files
committed
Improvement, allows to inject path as array
1 parent 77b6d0a commit 83edbd6

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
### v4.1.0
2+
* Additional ability to inject path as an array. Closes issue [50](https://github.com/raphaelstolt/php-jsonpatch/issues/50).
13
### v4.0.0
24
* Dropped support for unsupported PHP versions
35

src/Rs/Json/Patch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function __construct($targetDocument, $patchDocument, $allowedPatchOperat
4848

4949
/**
5050
* @throws \Rs\Json\Patch\FailedTestException
51-
* @return string
51+
* @return mixed
5252
*/
5353
public function apply()
5454
{

src/Rs/Json/Patch/Operation.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ abstract class Operation
99
protected $name;
1010

1111
/**
12-
* @var string
12+
* @var string|array
1313
*/
1414
protected $path;
1515

@@ -62,10 +62,13 @@ public function getName()
6262
}
6363

6464
/**
65-
* @return string
65+
* @return string|array
6666
*/
6767
public function getPath()
6868
{
69+
if (is_array($this->path)) {
70+
return '/'.implode('/', $this->path);
71+
}
6972
return $this->path;
7073
}
7174

@@ -81,7 +84,7 @@ protected function getPointerParts()
8184
}
8285

8386
/**
84-
* @param string $targetDocument
87+
* @param mixed $targetDocument
8588
*
8689
* @return mixed
8790
*/

tests/integration/Rs/Json/PatchAddTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,26 @@ public function shouldAddEmptyObject()
4242
$patchedDocument
4343
);
4444
}
45+
46+
/**
47+
* @test
48+
*/
49+
public function shouldAllowPathAsAnArray()
50+
{
51+
$targetDocument = '{"foo":{"obj": {"property": "value", "emptyObj": {}}}}';
52+
$patchDocument = '[{"op":"add", "path":["foo", "obj", "anotherProp"], "value":"qux"}]';
53+
$expectedDocument = '{"foo":{"obj": {"property": "value", "anotherProp": "qux", "emptyObj": {}}}}';
54+
55+
$patch = new Patch($targetDocument, $patchDocument);
56+
$patchedDocument = $patch->apply();
57+
58+
$this->assertJsonStringEqualsJsonString(
59+
$expectedDocument,
60+
$patchedDocument
61+
);
62+
}
63+
64+
4565
/**
4666
* @test
4767
* @ticket 33 (https://github.com/raphaelstolt/php-jsonpatch/issues/33)

0 commit comments

Comments
 (0)