33namespace Swaggest \JsonSchema \Path ;
44
55use Swaggest \JsonDiff \JsonPointer ;
6+ use Swaggest \JsonSchema \Schema ;
67
78class PointerUtil
89{
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+ public static function getSchemaPointer ($ path , $ isURIFragmentId = false )
18+ {
19+ $ result = self ::getSchemaPointers ($ path , $ isURIFragmentId );
20+ 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+ public static function getSchemaPointers ($ path , $ isURIFragmentId = false )
31+ {
32+ $ items = explode ('-> ' , $ path );
33+ unset($ items [0 ]);
34+ $ result = array ();
35+ $ pointer = $ isURIFragmentId ? '# ' : '' ;
36+ foreach ($ items as $ item ) {
37+ $ parts = explode (': ' , $ item );
38+ if (isset ($ parts [0 ])) {
39+ $ schemaPaths = explode ('[ ' , $ parts [0 ], 2 );
40+ if ($ schemaPaths [0 ] === Schema::PROP_REF ) {
41+ $ result [] = $ pointer . '/ ' . JsonPointer::escapeSegment (Schema::PROP_REF , $ isURIFragmentId );
42+ $ pointer = self ::rebuildPointer (substr ($ schemaPaths [1 ], 0 , -1 ), $ isURIFragmentId );
43+ continue ;
44+ }
45+ $ pointer .= '/ ' . JsonPointer::escapeSegment ($ schemaPaths [0 ], $ isURIFragmentId );
46+ if (isset ($ schemaPaths [1 ])) {
47+ $ pointer .= '/ ' . JsonPointer::escapeSegment (substr ($ schemaPaths [1 ], 0 , -1 ), $ isURIFragmentId );
48+ } elseif ($ parts [1 ]) {
49+ $ pointer .= '/ ' . JsonPointer::escapeSegment ($ parts [1 ], $ isURIFragmentId );
50+ }
51+ }
52+ }
53+ $ result [] = $ pointer ;
54+ return $ result ;
55+ }
56+
57+
958 /**
1059 * Builds JSON pointer to data from processing path
1160 * Path example: #->properties:responses->additionalProperties:envvar->properties:schema
1261 * @param string $path
62+ * @param bool $isURIFragmentId
1363 * @return string
14- * @todo proper path items escaping/moving to native JSON pointers
1564 */
16- public static function getDataPointer ($ path ) {
65+ public static function getDataPointer ($ path , $ isURIFragmentId = false )
66+ {
1767 $ items = explode ('-> ' , $ path );
1868 unset($ items [0 ]);
19- $ result = ' # ' ;
69+ $ result = $ isURIFragmentId ? ' # ' : ' ' ;
2070 foreach ($ items as $ item ) {
2171 $ parts = explode (': ' , $ item );
2272 if (isset ($ parts [1 ])) {
23- $ result .= '/ ' . JsonPointer::escapeSegment ($ parts [1 ], true );
73+ $ result .= '/ ' . JsonPointer::escapeSegment ($ parts [1 ], $ isURIFragmentId );
2474 }
2575 }
2676 return $ result ;
2777 }
2878
79+ private static function rebuildPointer ($ pointer , $ isURIFragmentId = false )
80+ {
81+ $ parts = JsonPointer::splitPath ($ pointer );
82+ $ result = $ isURIFragmentId ? '# ' : '' ;
83+ foreach ($ parts as $ item ) {
84+ $ result .= '/ ' . JsonPointer::escapeSegment ($ item , $ isURIFragmentId );
85+ }
86+ return $ result ;
87+ }
88+
2989}
0 commit comments