diff --git a/src/JsonPatch.php b/src/JsonPatch.php index 1b2d239..e4fbb2a 100644 --- a/src/JsonPatch.php +++ b/src/JsonPatch.php @@ -151,7 +151,7 @@ public function jsonSerialize() public function apply(&$original, $stopOnError = true) { $errors = array(); - foreach ($this->operations as $operation) { + foreach ($this->operations as $opIndex => $operation) { try { // track the current pointer field so we can use it for a potential PathException $pointerField = 'path'; @@ -199,6 +199,7 @@ public function apply(&$original, $stopOnError = true) $pointerField, $jsonPointerException->getCode() ); + $pathException->setOpIndex($opIndex); if ($stopOnError) { throw $pathException; } else { diff --git a/src/PathException.php b/src/PathException.php index 6ccbe91..6d54fe4 100644 --- a/src/PathException.php +++ b/src/PathException.php @@ -14,6 +14,9 @@ class PathException extends Exception /** @var string */ private $field; + /** @var int */ + private $opIndex; + /** * @param string $message * @param OpPath $operation @@ -49,4 +52,22 @@ public function getField() { return $this->field; } + + /** + * @param int $opIndex + * @return $this + */ + public function setOpIndex($opIndex) + { + $this->opIndex = $opIndex; + return $this; + } + + /** + * @return int + */ + public function getOpIndex() + { + return $this->opIndex; + } } diff --git a/tests/src/JsonPatchTest.php b/tests/src/JsonPatchTest.php index 1732377..47cf755 100644 --- a/tests/src/JsonPatchTest.php +++ b/tests/src/JsonPatchTest.php @@ -229,20 +229,21 @@ public function testPathExceptionContinueOnError() $data = array('abc' => $actualValue); $patch = new JsonPatch(); - $operation1 = new JsonPatch\Test('/abc', 'def'); - $patch->op($operation1); + $operation0 = new JsonPatch\Test('/abc', 'def'); + $patch->op($operation0); - $operation2 = new JsonPatch\Move('/target', '/source'); - $patch->op($operation2); + $operation1 = new JsonPatch\Move('/target', '/source'); + $patch->op($operation1); $errors = $patch->apply($data, false); $this->assertInstanceOf(PatchTestOperationFailedException::class, $errors[0]); - $this->assertSame($operation1, $errors[0]->getOperation()); + $this->assertSame($operation0, $errors[0]->getOperation()); $this->assertInstanceOf(PathException::class, $errors[1]); - $this->assertSame($operation2, $errors[1]->getOperation()); + $this->assertSame($operation1, $errors[1]->getOperation()); $this->assertSame('from', $errors[1]->getField()); + $this->assertEquals(1, $errors[1]->getOpIndex()); } public function pathExceptionProvider() { @@ -305,6 +306,7 @@ public function testPathException(OpPath $operation, $expectedMessage, $expected $this->assertInstanceOf(PathException::class, $ex); $this->assertEquals($expectedMessage, $ex->getMessage()); $this->assertEquals($expectedField, $ex->getField()); + $this->assertEquals(0, $ex->getOpIndex()); // There is only one operation } } }