Skip to content

Commit 95a931e

Browse files
committed
adding getByPointer to JsonPointer
1 parent fb25971 commit 95a931e

File tree

3 files changed

+66
-15
lines changed

3 files changed

+66
-15
lines changed

README.md

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ A PHP implementation for finding unordered diff between two `JSON` documents.
1313
* To detect breaking changes by analyzing removals and changes from original `JSON`.
1414
* To keep original order of object sets (for example `swagger.json` [parameters](https://swagger.io/docs/specification/describing-parameters/) list).
1515
* To make and apply JSON Patches, specified in [RFC 6902](http://tools.ietf.org/html/rfc6902) from the IETF.
16+
* To retrieve and modify data by [JSON Pointer](http://tools.ietf.org/html/rfc6901).
1617

1718
## Installation
1819

@@ -32,6 +33,8 @@ composer require swaggest/json-diff
3233

3334
## Library usage
3435

36+
### `JsonDiff`
37+
3538
Create `JsonDiff` object from two values (`original` and `new`).
3639

3740
```php
@@ -53,42 +56,76 @@ $r = new JsonDiff(
5356

5457
On created object you have several handy methods.
5558

56-
### `getPatch`
57-
Returns `JsonPatch` of difference
59+
#### `getPatch`
60+
Returns [`JsonPatch`](#jsonpatch) of difference
5861

59-
### `getRearranged`
62+
#### `getRearranged`
6063
Returns new value, rearranged with original order.
6164

62-
### `getRemoved`
65+
#### `getRemoved`
6366
Returns removals as partial value of original.
6467

65-
### `getRemovedPaths`
68+
#### `getRemovedPaths`
6669
Returns list of `JSON` paths that were removed from original.
6770

68-
### `getRemovedCnt`
71+
#### `getRemovedCnt`
6972
Returns number of removals.
7073

71-
### `getAdded`
74+
#### `getAdded`
7275
Returns additions as partial value of new.
7376

74-
### `getAddedPaths`
77+
#### `getAddedPaths`
7578
Returns list of `JSON` paths that were added to new.
7679

77-
### `getAddedCnt`
80+
#### `getAddedCnt`
7881
Returns number of additions.
7982

80-
### `getModifiedOriginal`
83+
#### `getModifiedOriginal`
8184
Returns modifications as partial value of original.
8285

83-
### `getModifiedNew`
86+
#### `getModifiedNew`
8487
Returns modifications as partial value of new.
8588

86-
### `getModifiedPaths`
89+
#### `getModifiedPaths`
8790
Returns list of `JSON` paths that were modified from original to new.
8891

89-
### `getModifiedCnt`
92+
#### `getModifiedCnt`
9093
Returns number of modifications.
9194

95+
### `JsonPatch`
96+
97+
#### `import`
98+
Creates `JsonPatch` instance from `JSON`-decoded data.
99+
100+
#### `export`
101+
Creates patch data from `JsonPatch` object.
102+
103+
#### `op`
104+
Adds operation to `JsonPatch`.
105+
106+
#### `apply`
107+
Applies patch to `JSON`-decoded data.
108+
109+
### `JsonPointer`
110+
111+
#### `escapeSegment`
112+
Escapes path segment.
113+
114+
#### `splitPath`
115+
Creates array of unescaped segments from `JSON Pointer` string.
116+
117+
#### `add`
118+
Adds value to data at path specified by segments.
119+
120+
#### `get`
121+
Gets value from data at path specified by segments.
122+
123+
#### `getByPointer`
124+
Gets value from data at path specified `JSON Pointer` string.
125+
126+
#### `remove`
127+
Removes value from data at path specified by segments.
128+
92129
## Example
93130

94131
```php

src/JsonPointer.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public static function splitPath($path)
3838
}
3939
}
4040

41-
private static function splitPathURIFragment(array $pathItems) {
41+
private static function splitPathURIFragment(array $pathItems)
42+
{
4243
$result = array();
4344
foreach ($pathItems as $key) {
4445
$key = str_replace(array('~1', '~0'), array('/', '~'), urldecode($key));
@@ -47,7 +48,8 @@ private static function splitPathURIFragment(array $pathItems) {
4748
return $result;
4849
}
4950

50-
private static function splitPathJsonString(array $pathItems) {
51+
private static function splitPathJsonString(array $pathItems)
52+
{
5153
$result = array();
5254
foreach ($pathItems as $key) {
5355
$key = str_replace(array('~1', '~0'), array('/', '~'), $key);
@@ -170,6 +172,17 @@ public static function get($holder, $pathItems)
170172
return $ref;
171173
}
172174

175+
/**
176+
* @param $holder
177+
* @param $pointer
178+
* @return bool|mixed
179+
* @throws Exception
180+
*/
181+
public static function getByPointer($holder, $pointer)
182+
{
183+
return self::get($holder, self::splitPath($pointer));
184+
}
185+
173186
/**
174187
* @param mixed $holder
175188
* @param string[] $pathItems

tests/src/JsonPointerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function testProcess()
3434
$this->assertSame('{"l1":{"l2":[[0],{"1":1}]}}', json_encode($json));
3535

3636
$this->assertSame(1, JsonPointer::get($json, JsonPointer::splitPath('/l1/l2/1/1')));
37+
$this->assertSame(1, JsonPointer::getByPointer($json, '/l1/l2/1/1'));
3738
}
3839

3940
/**

0 commit comments

Comments
 (0)