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
|
179 |
|
public static function toPregPattern($jsonPattern) |
14
|
|
|
{ |
15
|
179 |
|
static $delimiters = array('/', '#', '+', '~', '%'); |
16
|
|
|
|
17
|
179 |
|
$pattern = false; |
18
|
179 |
|
foreach ($delimiters as $delimiter) { |
19
|
179 |
|
if (strpos($jsonPattern, $delimiter) === false) { |
20
|
178 |
|
$pattern = $delimiter . $jsonPattern . $delimiter . 'u'; |
21
|
179 |
|
break; |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
179 |
|
if (false === $pattern) { |
26
|
1 |
|
throw new InvalidValue('Failed to prepare preg pattern'); |
27
|
|
|
} |
28
|
|
|
|
29
|
178 |
|
return $pattern; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $parent |
34
|
|
|
* @param string $current |
35
|
|
|
* @return string |
36
|
|
|
* @todo getaway from zeroes |
37
|
|
|
*/ |
38
|
420 |
|
public static function resolveURI($parent, $current) |
39
|
|
|
{ |
40
|
420 |
|
if ($current === '') { |
41
|
1 |
|
return $parent; |
42
|
|
|
} |
43
|
|
|
|
44
|
420 |
|
$parentParts = explode('#', $parent, 2); |
45
|
420 |
|
$currentParts = explode('#', $current, 2); |
46
|
|
|
|
47
|
420 |
|
$resultParts = array($parentParts[0], ''); |
48
|
420 |
|
if (isset($currentParts[1])) { |
49
|
137 |
|
$resultParts[1] = $currentParts[1]; |
50
|
|
|
} |
51
|
|
|
|
52
|
420 |
|
if (isset($currentParts[0]) && $currentParts[0]) { |
53
|
397 |
|
if (strpos($currentParts[0], '://')) { |
54
|
11 |
|
$resultParts[0] = $currentParts[0]; |
55
|
392 |
|
} elseif ('/' === substr($currentParts[0], 0, 1)) { |
56
|
|
|
$resultParts[0] = $currentParts[0]; |
57
|
|
|
if ($pos = strpos($parentParts[0], '://')) { |
58
|
|
|
$resultParts[0] = substr($parentParts[0], 0, strpos($parentParts[0], '/', $pos)) . $resultParts[0]; |
59
|
|
|
} |
60
|
392 |
|
} elseif (false !== $pos = strrpos($parentParts[0], '/')) { |
|
|
|
|
61
|
392 |
|
$resultParts[0] = substr($parentParts[0], 0, $pos + 1) . $currentParts[0]; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
420 |
|
$result = $resultParts[0] . '#' . $resultParts[1]; |
66
|
420 |
|
return $result; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
95 |
|
public static function padLines($with, $text, $skipFirst = true) |
71
|
|
|
{ |
72
|
95 |
|
$lines = explode("\n", $text); |
73
|
95 |
|
foreach ($lines as $index => $line) { |
74
|
95 |
|
if ($skipFirst && !$index) { |
75
|
95 |
|
continue; |
76
|
|
|
} |
77
|
28 |
|
if ($line) { |
78
|
28 |
|
$lines[$index] = $with . $line; |
79
|
|
|
} |
80
|
|
|
} |
81
|
95 |
|
return implode("\n", $lines); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
} |