Completed
Push — master ( ee9962...b72321 )
by Viacheslav
15s queued 10s
created

Helper::resolveURI()   D

Complexity

Conditions 9
Paths 13

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 20
cts 20
cp 1
rs 4.909
c 0
b 0
f 0
cc 9
eloc 20
nc 13
nop 2
crap 9
1
<?php
2
3
namespace Swaggest\JsonSchema;
4
5
6
class Helper
7
{
8
    /**
9
     * @param string $jsonPattern
10
     * @return bool|string
11
     * @throws InvalidValue
12
     */
13 181
    public static function toPregPattern($jsonPattern)
14
    {
15 181
        static $delimiters = array('/', '#', '+', '~', '%');
16
17 181
        $pattern = false;
18 181
        foreach ($delimiters as $delimiter) {
19 181
            if (strpos($jsonPattern, $delimiter) === false) {
20 180
                $pattern = $delimiter . $jsonPattern . $delimiter . 'u';
21 181
                break;
22
            }
23
        }
24
25 181
        if (false === $pattern) {
26 1
            throw new InvalidValue('Failed to prepare preg pattern');
27
        }
28
29 180
        return $pattern;
30
    }
31
32
    /**
33
     * @param string $parent
34
     * @param string $current
35
     * @return string
36
     * @todo getaway from zeroes
37
     */
38 422
    public static function resolveURI($parent, $current)
39
    {
40 422
        if ($current === '') {
41 1
            return $parent;
42
        }
43
44 422
        $parentParts = explode('#', $parent, 2);
45 422
        $currentParts = explode('#', $current, 2);
46
47 422
        $resultParts = array($parentParts[0], '');
48 422
        if (isset($currentParts[1])) {
49 139
            $resultParts[1] = $currentParts[1];
50
        }
51
52 422
        if (isset($currentParts[0]) && $currentParts[0]) {
53 399
            if (strpos($currentParts[0], '://')) {
54 12
                $resultParts[0] = $currentParts[0];
55 393
            } elseif ('/' === substr($currentParts[0], 0, 1)) {
56 1
                $resultParts[0] = $currentParts[0];
57 1
                if ($pos = strpos($parentParts[0], '://')) {
58 1
                    $resultParts[0] = substr($parentParts[0], 0, strpos($parentParts[0], '/', $pos + 3)) . $resultParts[0];
59
                }
60 392
            } elseif (false !== $pos = strrpos($parentParts[0], '/')) {
61 392
                $resultParts[0] = substr($parentParts[0], 0, $pos + 1) . $currentParts[0];
62
            } else {
63 6
                $resultParts[0] = $currentParts[0];
64
            }
65
        }
66
67 422
        $result = $resultParts[0] . '#' . $resultParts[1];
68 422
        return $result;
69
    }
70
71
72 97
    public static function padLines($with, $text, $skipFirst = true)
73
    {
74 97
        $lines = explode("\n", $text);
75 97
        foreach ($lines as $index => $line) {
76 97
            if ($skipFirst && !$index) {
77 69
                continue;
78
            }
79 30
            if ($line) {
80 30
                $lines[$index] = $with . $line;
81
            }
82
        }
83 97
        return implode("\n", $lines);
84
    }
85
86
}