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
|
|
|
} |