diff --git a/src/JsonPatch.php b/src/JsonPatch.php index 62a80f2..ccd4c99 100644 --- a/src/JsonPatch.php +++ b/src/JsonPatch.php @@ -60,11 +60,15 @@ public static function import(array $data) $operation = (object)$operation; } + if (!is_object($operation)) { + throw new Exception( 'Invalid patch operation - should be a JSON object' ); + } + if (!isset($operation->op)) { - throw new Exception('Missing "op" in operation data'); + throw new MissingFieldException('op', $operation); } if (!isset($operation->path)) { - throw new Exception('Missing "path" in operation data'); + throw new MissingFieldException('path', $operation); } $op = null; @@ -88,18 +92,18 @@ public static function import(array $data) $op = new Test(); break; default: - throw new Exception('Unknown "op": ' . $operation->op); + throw new UnknownOperationException($operation); } $op->path = $operation->path; if ($op instanceof OpPathValue) { if (property_exists($operation, 'value')) { $op->value = $operation->value; } else { - throw new Exception('Missing "value" in operation data'); + throw new MissingFieldException('value', $operation); } } elseif ($op instanceof OpPathFrom) { if (!isset($operation->from)) { - throw new Exception('Missing "from" in operation data'); + throw new MissingFieldException('from', $operation); } $op->from = $operation->from; } diff --git a/src/MissingFieldException.php b/src/MissingFieldException.php new file mode 100644 index 0000000..915f1dc --- /dev/null +++ b/src/MissingFieldException.php @@ -0,0 +1,47 @@ +missingField = $missingField; + $this->operation = $operation; + } + + /** + * @return string + */ + public function getMissingField() + { + return $this->missingField; + } + + /** + * @return object + */ + public function getOperation() + { + return $this->operation; + } +} diff --git a/src/UnknownOperationException.php b/src/UnknownOperationException.php new file mode 100644 index 0000000..0855bb2 --- /dev/null +++ b/src/UnknownOperationException.php @@ -0,0 +1,36 @@ +op, $code, $previous); + $this->operation = $operation; + } + + /** + * @return object + */ + public function getOperation() + { + return $this->operation; + } +} diff --git a/tests/src/JsonPatchTest.php b/tests/src/JsonPatchTest.php index 0870442..ce73ab6 100644 --- a/tests/src/JsonPatchTest.php +++ b/tests/src/JsonPatchTest.php @@ -5,7 +5,9 @@ use Swaggest\JsonDiff\Exception; use Swaggest\JsonDiff\JsonDiff; use Swaggest\JsonDiff\JsonPatch; +use Swaggest\JsonDiff\MissingFieldException; use Swaggest\JsonDiff\PatchTestOperationFailedException; +use Swaggest\JsonDiff\UnknownOperationException; class JsonPatchTest extends \PHPUnit_Framework_TestCase { @@ -75,7 +77,7 @@ public function testNull() public function testMissingOp() { - $this->setExpectedException(get_class(new Exception()), 'Missing "op" in operation data'); + $this->setExpectedException(MissingFieldException::class, 'Missing "op" in operation data'); JsonPatch::import(array((object)array('path' => '/123'))); } @@ -87,7 +89,7 @@ public function testMissingPath() public function testInvalidOp() { - $this->setExpectedException(get_class(new Exception()), 'Unknown "op": wat'); + $this->setExpectedException(UnknownOperationException::class, 'Unknown "op": wat'); JsonPatch::import(array((object)array('op' => 'wat', 'path' => '/123'))); }