Completed
Pull Request — master (#11)
by Viacheslav
02:53
created

Helper   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 19
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 78
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B toPregPattern() 0 22 5
D resolveURI() 0 30 9
B padLines() 0 13 5
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
    /**
32
     * @param $parent
33
     * @param $current
34
     * @return string
35
     * @todo getaway from zeroes
36
     */
37
    public static function resolveURI($parent, $current)
38
    {
39
        if ($current === '') {
40
            return $parent;
41
        }
42
43
        $parentParts = explode('#', $parent, 2);
44
        $currentParts = explode('#', $current, 2);
45
46
        $resultParts = array($parentParts[0], '');
47
        if (isset($currentParts[1])) {
48
            $resultParts[1] = $currentParts[1];
49
        }
50
51
        if (isset($currentParts[0]) && $currentParts[0]) {
52
            if (strpos($currentParts[0], '://')) {
53
                $resultParts[0] = $currentParts[0];
54
            } elseif ('/' === substr($currentParts[0], 0, 1)) {
55
                $resultParts[0] = $currentParts[0];
56
                if ($pos = strpos($parentParts[0], '://')) {
57
                    $resultParts[0] = substr($parentParts[0], 0, strpos($parentParts[0], '/', $pos)) . $resultParts[0];
58
                }
59
            } elseif (false !== $pos = strrpos($parentParts[0], '/')) {
60
                $resultParts[0] = substr($parentParts[0], 0, $pos + 1) . $currentParts[0];
61
            }
62
        }
63
64
        $result = $resultParts[0] . '#' . $resultParts[1];
65
        return $result;
66
    }
67
68
69
    public static function padLines($with, $text, $skipFirst = true)
70
    {
71
        $lines = explode("\n", $text);
72
        foreach ($lines as $index => $line) {
73
            if ($skipFirst && !$index) {
74
                continue;
75
            }
76
            if ($line) {
77
                $lines[$index] = $with . $line;
78
            }
79
        }
80
        return implode("\n", $lines);
81
    }
82
83
}