1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of sitemap-common. |
4
|
|
|
* |
5
|
|
|
* (c) 2016 Daniele Moraschi |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace SiteMap\Http; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class UrlUtil |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param Url $baseUrl |
19
|
|
|
* @param Url $url |
20
|
|
|
* @return bool |
21
|
|
|
* @throws \RuntimeException |
22
|
|
|
*/ |
23
|
|
|
public static function checkSameHost(Url $baseUrl, Url $url) |
24
|
|
|
{ |
25
|
|
|
if ($baseUrl->getHost() == null) { |
|
|
|
|
26
|
|
|
throw new \RuntimeException("Base URL is null"); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return $baseUrl->getHost() == $url->getHost(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param Url $baseUrl |
34
|
|
|
* @param $link |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public static function getAbsoluteLink(Url $baseUrl, $link) |
38
|
|
|
{ |
39
|
|
|
if (! $link) { |
40
|
|
|
return $baseUrl->getWebUrl(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (self::isSingleSlashed($link)) { |
44
|
|
|
return self::addSchemaAndHost($baseUrl, $link); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (self::isDoubleSlashed($link)) { |
48
|
|
|
return self::addSchema($baseUrl, $link); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (self::isRelative($link)) { |
52
|
|
|
return self::buildUrl($baseUrl, $link); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $link; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @TODO Improve this, definitely not the most elegant way. |
60
|
|
|
* @param $link |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public static function isRelative($link) |
64
|
|
|
{ |
65
|
|
|
return $link && ($link[0] === '#' |
66
|
|
|
|| $link[0] === '?' |
67
|
|
|
|| (strlen($link) > 3 && $link[0].$link[1].$link[2] === '../') |
68
|
|
|
|| (strlen($link) > 2 && $link[0].$link[1] === './') |
69
|
|
|
|| self::isPathOnly($link) |
70
|
|
|
|| self::isSingleSlashed($link) |
71
|
|
|
|| self::isDoubleSlashed($link)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $link |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public static function isPathOnly($link) |
79
|
|
|
{ |
80
|
|
|
$parts = parse_url($link); |
81
|
|
|
return count($parts) === 1 && isset($parts['path']); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param $link |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
public static function isSingleSlashed($link) { |
89
|
|
|
return (! self::isDoubleSlashed($link)) && $link[0] === '/'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param $link |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public static function isDoubleSlashed($link) { |
97
|
|
|
return strlen($link) > 1 && $link[0].$link[1] === '//'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param Url $baseUrl |
102
|
|
|
* @param $partial |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public static function buildUrl(Url $baseUrl, $partial) |
106
|
|
|
{ |
107
|
|
|
return rtrim($baseUrl->getWebUrl(), '/') .'/'. ltrim($partial, '/'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param Url $baseUrl |
112
|
|
|
* @param $partial |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public static function addSchema(Url $baseUrl, $partial) |
116
|
|
|
{ |
117
|
|
|
return $baseUrl->getSchema() .':'. $partial; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param Url $baseUrl |
122
|
|
|
* @param $partial |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public static function addSchemaAndHost(Url $baseUrl, $partial) |
126
|
|
|
{ |
127
|
|
|
return $baseUrl->getSchema() .'://'. $baseUrl->getHost() .'/'. ltrim($partial, '/'); |
128
|
|
|
} |
129
|
|
|
} |