Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e583f89

Browse files
authoredJan 30, 2018
Merge pull request #5 from swaggest/v2-patch
code cleanup
2 parents 2058926 + 1f15d54 commit e583f89

File tree

6 files changed

+24
-24
lines changed

6 files changed

+24
-24
lines changed
 

‎README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ A PHP implementation for finding unordered diff between two `JSON` documents.
55
[![Build Status](https://travis-ci.org/swaggest/json-diff.svg?branch=master)](https://travis-ci.org/swaggest/json-diff)
66
[![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)
77
[![Code Climate](https://codeclimate.com/github/swaggest/json-diff/badges/gpa.svg)](https://codeclimate.com/github/swaggest/json-diff)
8-
[![Test Coverage](https://codeclimate.com/github/swaggest/json-diff/badges/coverage.svg)](https://codeclimate.com/github/swaggest/json-diff/coverage)
8+
[![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)
99

1010
## Purpose
1111

@@ -54,7 +54,7 @@ $r = new JsonDiff(
5454
On created object you have several handy methods.
5555

5656
### `getPatch`
57-
Returns JsonPatch of difference
57+
Returns `JsonPatch` of difference
5858

5959
### `getRearranged`
6060
Returns new value, rearranged with original order.

‎src/Cli/Diff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class Diff extends Base
99
{
10-
static function setUpDefinition(Command\Definition $definition, $options)
10+
public static function setUpDefinition(Command\Definition $definition, $options)
1111
{
1212
parent::setUpDefinition($definition, $options);
1313
$definition->description = 'Make patch from two json documents, output to STDOUT';

‎src/Cli/Rearrange.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class Rearrange extends Base
99
{
10-
static function setUpDefinition(Command\Definition $definition, $options)
10+
public static function setUpDefinition(Command\Definition $definition, $options)
1111
{
1212
parent::setUpDefinition($definition, $options);
1313
$definition->description = 'Rearrange json document in the order of another (original) json document';

‎src/JsonPatch.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@ class JsonPatch implements \JsonSerializable
2727
* @return JsonPatch
2828
* @throws Exception
2929
*/
30-
public static function import($data)
30+
public static function import(array $data)
3131
{
32-
if (!is_array($data)) {
33-
throw new Exception('Array expected in JsonPatch::import');
34-
}
3532
$result = new JsonPatch();
3633
foreach ($data as $operation) {
3734
/** @var OpPath|OpPathValue|OpPathFrom $operation */

‎src/JsonPointer.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,30 @@ public static function splitPath($path)
2828
{
2929
$pathItems = explode('/', $path);
3030
$first = array_shift($pathItems);
31-
$result = array();
3231
if ($first === '#') {
33-
foreach ($pathItems as $key) {
34-
$key = str_replace(array('~1', '~0'), array('/', '~'), urldecode($key));
35-
$result[] = $key;
36-
}
32+
return self::splitPathURIFragment($pathItems);
3733
} else {
3834
if ($first !== '') {
3935
throw new Exception('Path must start with "/": ' . $path);
4036
}
41-
foreach ($pathItems as $key) {
42-
$key = str_replace(array('~1', '~0'), array('/', '~'), $key);
43-
$result[] = $key;
44-
}
37+
return self::splitPathJsonString($pathItems);
38+
}
39+
}
40+
41+
private static function splitPathURIFragment(array $pathItems) {
42+
$result = array();
43+
foreach ($pathItems as $key) {
44+
$key = str_replace(array('~1', '~0'), array('/', '~'), urldecode($key));
45+
$result[] = $key;
46+
}
47+
return $result;
48+
}
49+
50+
private static function splitPathJsonString(array $pathItems) {
51+
$result = array();
52+
foreach ($pathItems as $key) {
53+
$key = str_replace(array('~1', '~0'), array('/', '~'), $key);
54+
$result[] = $key;
4555
}
4656
return $result;
4757
}

‎tests/src/JsonPatchTest.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,6 @@ public function testNull()
7272

7373
}
7474

75-
76-
public function testInvalidPatch()
77-
{
78-
$this->setExpectedException(get_class(new Exception()), 'Array expected in JsonPatch::import');
79-
JsonPatch::import(123);
80-
}
81-
8275
public function testMissingOp()
8376
{
8477
$this->setExpectedException(get_class(new Exception()), 'Missing "op" in operation data');

0 commit comments

Comments
 (0)
Please sign in to comment.