PointerUtil   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 97.83%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
eloc 44
c 1
b 0
f 0
dl 0
loc 93
ccs 45
cts 46
cp 0.9783
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSchemaPointer() 0 4 1
B getSchemaPointers() 0 36 10
A rebuildPointer() 0 8 3
A getDataPointer() 0 15 5
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|null
16
     */
17 3
    public static function getSchemaPointer($path, $isURIFragmentId = false)
18
    {
19 3
        $result = self::getSchemaPointers($path, $isURIFragmentId);
20 3
        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 5
    public static function getSchemaPointers($path, $isURIFragmentId = false)
31
    {
32 5
        $items = explode('->', $path);
33 5
        unset($items[0]);
34 5
        $result = array();
35 5
        $pointer = $isURIFragmentId ? '#' : '';
36 5
        foreach ($items as $item) {
37 5
            $parts = explode(':', $item);
38 5
            if (isset($parts[0])) {
39 5
                $schemaPaths = explode('[', $parts[0], 2);
40 5
                if (isset($schemaPaths[1])) {
41 4
                    $schemaPaths[1] = substr(strtr($schemaPaths[1], array('~1' => '~', '~2' => ':')), 0, -1);
42
                }
43
44 5
                if ($schemaPaths[0] === Schema::PROP_REF) {
45 4
                    $result[] = $pointer . '/' . JsonPointer::escapeSegment(Schema::PROP_REF, $isURIFragmentId);
46 4
                    if ($schemaPaths[1][0] !== '#') { // Absolute URI.
47 2
                        $pointer = $schemaPaths[1];
48
                    } else {
49 2
                        $pointer = self::rebuildPointer($schemaPaths[1], $isURIFragmentId);
50
                    }
51 4
                    continue;
52
                }
53 4
                $pointer .= '/' . JsonPointer::escapeSegment($schemaPaths[0], $isURIFragmentId);
54 4
                if (isset($schemaPaths[1])) {
55 2
                    $pointer .= '/' . JsonPointer::escapeSegment($schemaPaths[1], $isURIFragmentId);
56 4
                } elseif (isset($parts[1])) {
57 3
                    $pointer .= '/' . JsonPointer::escapeSegment($parts[1], $isURIFragmentId);
58
                }
59
            }
60
        }
61 5
        if ($pointer === '') {
62
            $pointer = '/';
63
        }
64 5
        $result[] = $pointer;
65 5
        return $result;
66
    }
67
68
69
    /**
70
     * Builds JSON pointer to data from processing path
71
     * Path example: #->properties:responses->additionalProperties:envvar->properties:schema
72
     * @param string $path
73
     * @param bool $isURIFragmentId
74
     * @return string
75
     */
76 5
    public static function getDataPointer($path, $isURIFragmentId = false)
77
    {
78 5
        $items = explode('->', $path);
79 5
        unset($items[0]);
80 5
        $result = $isURIFragmentId ? '#' : '';
81 5
        foreach ($items as $item) {
82 4
            $parts = explode(':', $item);
83 4
            if (isset($parts[1])) {
84 3
                $result .= '/' . JsonPointer::escapeSegment($parts[1], $isURIFragmentId);
85
            }
86
        }
87 5
        if ($result === '') {
88 1
            return '/';
89
        }
90 4
        return $result;
91
    }
92
93 2
    private static function rebuildPointer($pointer, $isURIFragmentId = false)
94
    {
95 2
        $parts = JsonPointer::splitPath($pointer);
96 2
        $result = $isURIFragmentId ? '#' : '';
97 2
        foreach ($parts as $item) {
98 2
            $result .= '/' . JsonPointer::escapeSegment($item, $isURIFragmentId);
99
        }
100 2
        return $result;
101
    }
102
103
}