diff --git a/README.md b/README.md index 966e5e9..6fbe2f7 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ A PHP implementation for finding unordered diff between two `JSON` documents. [![Build Status](https://travis-ci.org/swaggest/json-diff.svg?branch=master)](https://travis-ci.org/swaggest/json-diff) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/swaggest/json-diff/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/swaggest/json-diff/?branch=master) [![Code Climate](https://codeclimate.com/github/swaggest/json-diff/badges/gpa.svg)](https://codeclimate.com/github/swaggest/json-diff) -[![Test Coverage](https://codeclimate.com/github/swaggest/json-diff/badges/coverage.svg)](https://codeclimate.com/github/swaggest/json-diff/coverage) +[![Code Coverage](https://scrutinizer-ci.com/g/swaggest/json-diff/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/swaggest/json-diff/code-structure/master/code-coverage) ## Purpose @@ -54,7 +54,7 @@ $r = new JsonDiff( On created object you have several handy methods. ### `getPatch` -Returns JsonPatch of difference +Returns `JsonPatch` of difference ### `getRearranged` Returns new value, rearranged with original order. diff --git a/src/Cli/Diff.php b/src/Cli/Diff.php index 6b29ea4..c5b43fb 100644 --- a/src/Cli/Diff.php +++ b/src/Cli/Diff.php @@ -7,7 +7,7 @@ class Diff extends Base { - static function setUpDefinition(Command\Definition $definition, $options) + public static function setUpDefinition(Command\Definition $definition, $options) { parent::setUpDefinition($definition, $options); $definition->description = 'Make patch from two json documents, output to STDOUT'; diff --git a/src/Cli/Rearrange.php b/src/Cli/Rearrange.php index 76a5da3..32ac6af 100644 --- a/src/Cli/Rearrange.php +++ b/src/Cli/Rearrange.php @@ -7,7 +7,7 @@ class Rearrange extends Base { - static function setUpDefinition(Command\Definition $definition, $options) + public static function setUpDefinition(Command\Definition $definition, $options) { parent::setUpDefinition($definition, $options); $definition->description = 'Rearrange json document in the order of another (original) json document'; diff --git a/src/JsonPatch.php b/src/JsonPatch.php index f32d3f2..f7799be 100644 --- a/src/JsonPatch.php +++ b/src/JsonPatch.php @@ -27,11 +27,8 @@ class JsonPatch implements \JsonSerializable * @return JsonPatch * @throws Exception */ - public static function import($data) + public static function import(array $data) { - if (!is_array($data)) { - throw new Exception('Array expected in JsonPatch::import'); - } $result = new JsonPatch(); foreach ($data as $operation) { /** @var OpPath|OpPathValue|OpPathFrom $operation */ diff --git a/src/JsonPointer.php b/src/JsonPointer.php index 9f179f5..6f7960f 100644 --- a/src/JsonPointer.php +++ b/src/JsonPointer.php @@ -28,20 +28,30 @@ public static function splitPath($path) { $pathItems = explode('/', $path); $first = array_shift($pathItems); - $result = array(); if ($first === '#') { - foreach ($pathItems as $key) { - $key = str_replace(array('~1', '~0'), array('/', '~'), urldecode($key)); - $result[] = $key; - } + return self::splitPathURIFragment($pathItems); } else { if ($first !== '') { throw new Exception('Path must start with "/": ' . $path); } - foreach ($pathItems as $key) { - $key = str_replace(array('~1', '~0'), array('/', '~'), $key); - $result[] = $key; - } + return self::splitPathJsonString($pathItems); + } + } + + private static function splitPathURIFragment(array $pathItems) { + $result = array(); + foreach ($pathItems as $key) { + $key = str_replace(array('~1', '~0'), array('/', '~'), urldecode($key)); + $result[] = $key; + } + return $result; + } + + private static function splitPathJsonString(array $pathItems) { + $result = array(); + foreach ($pathItems as $key) { + $key = str_replace(array('~1', '~0'), array('/', '~'), $key); + $result[] = $key; } return $result; } diff --git a/tests/src/JsonPatchTest.php b/tests/src/JsonPatchTest.php index 75f40f7..b06d212 100644 --- a/tests/src/JsonPatchTest.php +++ b/tests/src/JsonPatchTest.php @@ -72,13 +72,6 @@ public function testNull() } - - public function testInvalidPatch() - { - $this->setExpectedException(get_class(new Exception()), 'Array expected in JsonPatch::import'); - JsonPatch::import(123); - } - public function testMissingOp() { $this->setExpectedException(get_class(new Exception()), 'Missing "op" in operation data');