1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This file is part of Wszetko Sitemap. |
7
|
|
|
* |
8
|
|
|
* (c) Paweł Kłopotek-Główczewski <[email protected]> |
9
|
|
|
* |
10
|
|
|
* This source file is subject to the MIT license that is bundled |
11
|
|
|
* with this source code in the file LICENSE. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Wszetko\Sitemap\Helpers; |
15
|
|
|
|
16
|
|
|
use League\Uri\Components\Domain; |
17
|
|
|
use League\Uri\Components\Fragment; |
18
|
|
|
use League\Uri\Components\Host; |
19
|
|
|
use League\Uri\Components\Path; |
20
|
|
|
use League\Uri\Components\Port; |
21
|
|
|
use League\Uri\Components\Query; |
22
|
|
|
use League\Uri\Components\Scheme; |
23
|
|
|
use League\Uri\Components\UserInfo; |
24
|
|
|
use League\Uri\Exceptions\SyntaxError; |
25
|
|
|
use League\Uri\Uri; |
26
|
|
|
use League\Uri\UriModifier; |
27
|
|
|
use League\Uri\UriString; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class Url. |
31
|
|
|
* |
32
|
|
|
* @package Wszetko\Sitemap\Helpers |
33
|
|
|
*/ |
34
|
|
|
class Url |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @param string $url |
38
|
|
|
* |
39
|
|
|
* @return bool|string |
40
|
|
|
* |
41
|
|
|
* @throws \League\Uri\Exceptions\SyntaxError |
42
|
|
|
*/ |
43
|
246 |
|
public static function normalizeUrl(string $url) |
44
|
|
|
{ |
45
|
|
|
try { |
46
|
246 |
|
$uri = UriModifier::removeDotSegments(Uri::createFromString($url)); |
47
|
226 |
|
$parts = []; |
48
|
226 |
|
$parts['scheme'] = Scheme::createFromUri($uri)->getContent(); |
49
|
226 |
|
$parts['user'] = UserInfo::createFromUri($uri)->getUser(); |
50
|
226 |
|
$parts['pass'] = UserInfo::createFromUri($uri)->getPass(); |
51
|
226 |
|
$parts['host'] = Host::createFromUri($uri); |
52
|
226 |
|
$parts['port'] = Port::createFromUri($uri)->getContent(); |
53
|
226 |
|
$parts['path'] = Path::createFromUri($uri)->getContent(); |
54
|
226 |
|
$parts['query'] = Query::createFromUri($uri)->getContent(); |
55
|
226 |
|
$parts['fragment'] = Fragment::createFromUri($uri)->getContent(); |
56
|
|
|
|
57
|
|
|
if ( |
58
|
226 |
|
'' === $parts['scheme'] || |
59
|
226 |
|
null === $parts['scheme'] || |
60
|
214 |
|
'' === $parts['host']->getContent() || |
61
|
214 |
|
null === $parts['host']->getContent() || |
62
|
226 |
|
(false === $parts['host']->isDomain() && false === $parts['host']->isIp()) |
63
|
|
|
) { |
64
|
24 |
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
204 |
|
if (true === $parts['host']->isDomain()) { |
68
|
196 |
|
$domain = new Domain($parts['host']); |
69
|
196 |
|
if (true === $domain->isAbsolute()) { |
70
|
2 |
|
return false; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
202 |
|
$parts['host'] = (string) $parts['host']; |
75
|
|
|
|
76
|
202 |
|
return UriString::build($parts); |
77
|
20 |
|
} catch (\TypeError | SyntaxError $e) { |
78
|
20 |
|
return false; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|