1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\JsonSchema\Path; |
4
|
|
|
|
5
|
|
|
use Swaggest\JsonDiff\JsonPointer; |
6
|
|
|
use Swaggest\JsonSchema\Schema; |
7
|
|
|
|
8
|
|
|
class PointerUtil |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Builds JSON pointer to schema from processing path |
12
|
|
|
* Path example: #->properties:responses->additionalProperties:envvar->properties:schema |
13
|
|
|
* @param string $path |
14
|
|
|
* @param bool $isURIFragmentId |
15
|
|
|
* @return string |
16
|
|
|
*/ |
17
|
1 |
|
public static function getSchemaPointer($path, $isURIFragmentId = false) |
18
|
|
|
{ |
19
|
1 |
|
$result = self::getSchemaPointers($path, $isURIFragmentId); |
20
|
1 |
|
return array_pop($result); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Builds JSON pointer to schema from processing path |
25
|
|
|
* Path example: #->properties:responses->additionalProperties:envvar->properties:schema |
26
|
|
|
* @param string $path |
27
|
|
|
* @param bool $isURIFragmentId |
28
|
|
|
* @return string[] |
29
|
|
|
*/ |
30
|
2 |
|
public static function getSchemaPointers($path, $isURIFragmentId = false) |
31
|
|
|
{ |
32
|
2 |
|
$items = explode('->', $path); |
33
|
2 |
|
unset($items[0]); |
34
|
2 |
|
$result = array(); |
35
|
2 |
|
$pointer = $isURIFragmentId ? '#' : ''; |
36
|
2 |
|
foreach ($items as $item) { |
37
|
2 |
|
$parts = explode(':', $item); |
38
|
2 |
|
if (isset($parts[0])) { |
39
|
2 |
|
$schemaPaths = explode('[', $parts[0], 2); |
40
|
2 |
|
if ($schemaPaths[0] === Schema::PROP_REF) { |
41
|
2 |
|
$result[] = $pointer . '/' . JsonPointer::escapeSegment(Schema::PROP_REF, $isURIFragmentId); |
42
|
2 |
|
$pointer = self::rebuildPointer(substr($schemaPaths[1], 0, -1), $isURIFragmentId); |
43
|
2 |
|
continue; |
44
|
|
|
} |
45
|
2 |
|
$pointer .= '/' . JsonPointer::escapeSegment($schemaPaths[0], $isURIFragmentId); |
46
|
2 |
|
if (isset($schemaPaths[1])) { |
47
|
2 |
|
$pointer .= '/' . JsonPointer::escapeSegment(substr($schemaPaths[1], 0, -1), $isURIFragmentId); |
48
|
2 |
|
} elseif ($parts[1]) { |
49
|
2 |
|
$pointer .= '/' . JsonPointer::escapeSegment($parts[1], $isURIFragmentId); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
2 |
|
$result[] = $pointer; |
54
|
2 |
|
return $result; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Builds JSON pointer to data from processing path |
60
|
|
|
* Path example: #->properties:responses->additionalProperties:envvar->properties:schema |
61
|
|
|
* @param string $path |
62
|
|
|
* @param bool $isURIFragmentId |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
3 |
|
public static function getDataPointer($path, $isURIFragmentId = false) |
66
|
|
|
{ |
67
|
3 |
|
$items = explode('->', $path); |
68
|
3 |
|
unset($items[0]); |
69
|
3 |
|
$result = $isURIFragmentId ? '#' : ''; |
70
|
3 |
|
foreach ($items as $item) { |
71
|
2 |
|
$parts = explode(':', $item); |
72
|
2 |
|
if (isset($parts[1])) { |
73
|
2 |
|
$result .= '/' . JsonPointer::escapeSegment($parts[1], $isURIFragmentId); |
74
|
|
|
} |
75
|
|
|
} |
76
|
3 |
|
return $result; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
private static function rebuildPointer($pointer, $isURIFragmentId = false) |
80
|
|
|
{ |
81
|
2 |
|
$parts = JsonPointer::splitPath($pointer); |
82
|
2 |
|
$result = $isURIFragmentId ? '#' : ''; |
83
|
2 |
|
foreach ($parts as $item) { |
84
|
2 |
|
$result .= '/' . JsonPointer::escapeSegment($item, $isURIFragmentId); |
85
|
|
|
} |
86
|
2 |
|
return $result; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} |