1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AppUtils\URLInfo\Parser; |
6
|
|
|
|
7
|
|
|
use AppUtils\ConvertHelper; |
8
|
|
|
use AppUtils\URLInfo; |
9
|
|
|
use AppUtils\URLInfo\URISchemes; |
10
|
|
|
use AppUtils\URLInfo\URLHosts; |
11
|
|
|
use AppUtils\URLInfo\URLInfoTrait; |
12
|
|
|
|
13
|
|
|
class ParsedInfoFilter |
14
|
|
|
{ |
15
|
|
|
use URLInfoTrait; |
16
|
|
|
|
17
|
|
|
private string $url; |
18
|
|
|
|
19
|
|
|
public function __construct(string $url, array $info) |
20
|
|
|
{ |
21
|
|
|
$this->info = $info; |
22
|
|
|
$this->url = $url; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function filter() : array |
26
|
|
|
{ |
27
|
|
|
$this->info['type'] = URLInfo::TYPE_NONE; |
28
|
|
|
|
29
|
|
|
$this->filterScheme(); |
30
|
|
|
$this->filterAuth(); |
31
|
|
|
$this->filterHost(); |
32
|
|
|
$this->filterPath(); |
33
|
|
|
$this->filterKnownHosts(); |
34
|
|
|
$this->filterParams(); |
35
|
|
|
|
36
|
|
|
return $this->info; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function filterScheme() : void |
40
|
|
|
{ |
41
|
|
|
if($this->hasScheme()) |
42
|
|
|
{ |
43
|
|
|
$this->setScheme(strtolower($this->getScheme())); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
else |
46
|
|
|
{ |
47
|
|
|
$scheme = URISchemes::detectScheme($this->url); |
48
|
|
|
if(!empty($scheme)) { |
49
|
|
|
$this->setScheme(URISchemes::resolveSchemeName($scheme)); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function filterAuth() : void |
55
|
|
|
{ |
56
|
|
|
if(!$this->hasAuth()) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->setAuth( |
61
|
|
|
urldecode((string)$this->getUser()), |
62
|
|
|
urldecode((string)$this->getPassword()) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function filterHost() : void |
67
|
|
|
{ |
68
|
|
|
if(!$this->hasHost()) |
69
|
|
|
{ |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$host = $this->getHost(); |
74
|
|
|
$host = strtolower($host); |
|
|
|
|
75
|
|
|
$host = str_replace(' ', '', $host); |
76
|
|
|
$this->setHost($host); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function filterPath() : void |
80
|
|
|
{ |
81
|
|
|
if($this->hasPath()) { |
82
|
|
|
$this->setPath(str_replace(' ', '', $this->getPath())); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function filterKnownHosts() : void |
87
|
|
|
{ |
88
|
|
|
$host = $this->getPath(); |
89
|
|
|
|
90
|
|
|
if(empty($host) || !URLHosts::isHostKnown($host)) |
91
|
|
|
{ |
92
|
|
|
return; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->setHost($host); |
96
|
|
|
$this->removePath(); |
97
|
|
|
|
98
|
|
|
if(!$this->hasScheme()) { |
99
|
|
|
$this->setSchemeHTTPS(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function filterParams() : void |
104
|
|
|
{ |
105
|
|
|
$this->info['params'] = array(); |
106
|
|
|
|
107
|
|
|
$query = $this->getQuery(); |
108
|
|
|
if(empty($query)) { |
109
|
|
|
return; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->info['params'] = ConvertHelper::parseQueryString($query); |
113
|
|
|
|
114
|
|
|
ksort($this->info['params']); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|