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

Helper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 1
dl 0
loc 55
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B toPregPattern() 0 22 5
C resolveURI() 0 27 8
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
}