Skip to content

Commit 94925ea

Browse files
committed
JsonPointer::buildPath
1 parent 44b7078 commit 94925ea

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ Escapes path segment.
115115
#### `splitPath`
116116
Creates array of unescaped segments from `JSON Pointer` string.
117117

118+
#### `buildPath`
119+
Creates `JSON Pointer` string from array of unescaped segments.
120+
118121
#### `add`
119122
Adds value to data at path specified by segments.
120123

src/JsonPointer.php

+14
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ public static function escapeSegment($key, $isURIFragmentId = false)
1919
}
2020
}
2121

22+
/**
23+
* @param string[] $pathItems
24+
* @param bool $isURIFragmentId
25+
* @return string
26+
*/
27+
public static function buildPath(array $pathItems, $isURIFragmentId = false)
28+
{
29+
$result = $isURIFragmentId ? '#' : '';
30+
foreach ($pathItems as $pathItem) {
31+
$result .= '/' . self::escapeSegment($pathItem, $isURIFragmentId);
32+
}
33+
return $result;
34+
}
35+
2236
/**
2337
* @param string $path
2438
* @return string[]

tests/src/JsonPointerTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,14 @@ public function testEscapeSegment()
5252
$segment = '/project/{username}/{project}';
5353
$this->assertSame('~1project~1%7Busername%7D~1%7Bproject%7D', JsonPointer::escapeSegment($segment, true));
5454
}
55+
56+
public function testBuildPath()
57+
{
58+
$pathItems = ['key1', '/project/{username}/{project}', 'key2'];
59+
60+
$this->assertSame('/key1/~1project~1{username}~1{project}/key2',
61+
JsonPointer::buildPath($pathItems));
62+
$this->assertSame('#/key1/~1project~1%7Busername%7D~1%7Bproject%7D/key2',
63+
JsonPointer::buildPath($pathItems, true));
64+
}
5565
}

0 commit comments

Comments
 (0)