Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/JsonPatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -199,6 +199,7 @@ public function apply(&$original, $stopOnError = true)
$pointerField,
$jsonPointerException->getCode()
);
$pathException->setOpIndex($opIndex);
if ($stopOnError) {
throw $pathException;
} else {
Expand Down
21 changes: 21 additions & 0 deletions src/PathException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class PathException extends Exception
/** @var string */
private $field;

/** @var int */
private $opIndex;

/**
* @param string $message
* @param OpPath $operation
Expand Down Expand Up @@ -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;
}
}
14 changes: 8 additions & 6 deletions tests/src/JsonPatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}
}
}