1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package Application Utils |
4
|
|
|
* @subpackage URLInfo |
5
|
|
|
* @see \AppUtils\URLInfo\URINormalizer |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace AppUtils\URLInfo; |
11
|
|
|
|
12
|
|
|
use AppUtils\URLInfo; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Handles normalizing a URL. |
16
|
|
|
* |
17
|
|
|
* @package Application Utils |
18
|
|
|
* @subpackage URLInfo |
19
|
|
|
* @author Sebastian Mordziol <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class URINormalizer |
22
|
|
|
{ |
23
|
|
|
protected URLInfo $info; |
24
|
|
|
protected bool $auth = true; |
25
|
|
|
|
26
|
|
|
public function __construct(URLInfo $info) |
27
|
|
|
{ |
28
|
|
|
$this->info = $info; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Enables the authentication information in the URL, |
33
|
|
|
* if a username and password are present. |
34
|
|
|
* |
35
|
|
|
* @param bool $enable Whether to turn it on or off. |
36
|
|
|
* @return URINormalizer |
37
|
|
|
*/ |
38
|
|
|
public function enableAuth(bool $enable=true) : URINormalizer |
39
|
|
|
{ |
40
|
|
|
$this->auth = $enable; |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Retrieves the normalized URL. |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function normalize() : string |
49
|
|
|
{ |
50
|
|
|
$method = 'normalize_'.$this->info->getType(); |
51
|
|
|
|
52
|
|
|
return (string)$this->$method(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function normalize_fragment() : string |
56
|
|
|
{ |
57
|
|
|
return '#'.$this->info->getFragment(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function normalize_phone() : string |
61
|
|
|
{ |
62
|
|
|
return 'tel:'.$this->info->getHost(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function normalize_email() : string |
66
|
|
|
{ |
67
|
|
|
return 'mailto:'.$this->info->getPath(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function normalize_url() : string |
71
|
|
|
{ |
72
|
|
|
$scheme = $this->info->getScheme(); |
73
|
|
|
$normalized = ''; |
74
|
|
|
|
75
|
|
|
if(!empty($scheme)) |
76
|
|
|
{ |
77
|
|
|
$normalized = $this->info->getScheme() . '://'; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$normalized = $this->renderAuth($normalized); |
81
|
|
|
$normalized .= $this->info->getHost(); |
82
|
|
|
$normalized = $this->renderPort($normalized); |
83
|
|
|
$normalized = $this->renderPath($normalized); |
84
|
|
|
$normalized = $this->renderParams($normalized); |
85
|
|
|
$normalized = $this->renderFragment($normalized); |
86
|
|
|
|
87
|
|
|
return $normalized; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function renderAuth(string $normalized) : string |
91
|
|
|
{ |
92
|
|
|
if(!$this->auth || !$this->info->hasUsername()) { |
93
|
|
|
return $normalized; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $normalized . urlencode($this->info->getUsername()).':'.urlencode($this->info->getPassword()).'@'; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function renderPort(string $normalized) : string |
100
|
|
|
{ |
101
|
|
|
if(!$this->info->hasPort()) { |
102
|
|
|
return $normalized; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $normalized . ':'.$this->info->getPort(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function renderPath(string $normalized) : string |
109
|
|
|
{ |
110
|
|
|
if(!$this->info->hasPath()) { |
111
|
|
|
return $normalized; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $normalized . $this->info->getPath(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected function renderParams(string $normalized) : string |
118
|
|
|
{ |
119
|
|
|
$params = $this->info->getParams(); |
120
|
|
|
|
121
|
|
|
if(empty($params)) { |
122
|
|
|
return $normalized; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $normalized . '?'.http_build_query($params, '', '&', PHP_QUERY_RFC3986); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
protected function renderFragment(string $normalized) : string |
129
|
|
|
{ |
130
|
|
|
if(!$this->info->hasFragment()) { |
131
|
|
|
return $normalized; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $normalized . '#'.$this->info->getFragment(); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|