Completed
Push — master ( 89ec86...246773 )
by Viacheslav
01:53
created

Helper::resolveURI()   C

Complexity

Conditions 8
Paths 19

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 16
nc 19
nop 2
1
<?php
2
3
namespace Swaggest\JsonSchema;
4
5
6
class Helper
7
{
8
    public static function toPregPattern($jsonPattern)
9
    {
10
        static $delimiters = array('/', '#', '+', '~', '%');
11
12
        $pattern = false;
13
        foreach ($delimiters as $delimiter) {
14
            if (strpos($jsonPattern, $delimiter) === false) {
15
                $pattern = $delimiter . $jsonPattern . $delimiter . 'u';
16
                break;
17
            }
18
        }
19
20
        if (false === $pattern) {
21
            throw new InvalidValue('Failed to prepare preg pattern');
22
        }
23
24
        if (@preg_match($pattern, '') === false) {
25
            throw new InvalidValue('Regex pattern is invalid: ' . $jsonPattern);
26
        }
27
28
        return $pattern;
29
    }
30
31
    public static function resolveURI($parent, $current)
32
    {
33
        if (false !== $pos = strpos($current, '://')) {
34
            if (strpos($current, '/') > $pos) {
35
                return $current;
36
            }
37
        }
38
39
        if ($current === '') {
40
            return $parent;
41
        }
42
43
        $result = $parent;
44
        if ($current[0] === '#') {
45
            if (false !== $pos = strpos($parent, '#')) {
46
                $result = substr($parent, 0, $pos) . $current;
47
            }
48
        } else {
49
            if (false !== $pos = strrpos($parent, '/')) {
50
                $result = substr($parent, 0, $pos + 1) . $current;
51
            }
52
        }
53
        if (false === strpos($result, '#')) {
54
            $result .= '#';
55
        }
56
        return $result;
57
    }
58
59
60
}