|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Weew\Url; |
|
4
|
|
|
|
|
5
|
|
|
class UrlParser implements IUrlParser { |
|
6
|
|
|
/** |
|
7
|
|
|
* @param $string |
|
8
|
|
|
* |
|
9
|
|
|
* @return array |
|
10
|
|
|
*/ |
|
11
|
|
|
public function parse($string) { |
|
12
|
|
|
$segments = $this->parseSegments($string); |
|
13
|
|
|
$segments = array_extend($segments, $this->parseHost(array_get($segments, 'host'))); |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
return $this->replaceEmptyStringsWithNull($segments); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param $string |
|
20
|
|
|
* |
|
21
|
|
|
* @return array |
|
22
|
|
|
*/ |
|
23
|
|
|
public function parseHost($string) { |
|
24
|
|
|
$segments = [ |
|
25
|
|
|
'tld' => null, |
|
26
|
|
|
'domain' => null, |
|
27
|
|
|
'subdomain' => null, |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
$parts = explode('.', $string); |
|
31
|
|
|
|
|
32
|
|
|
if (count($parts) == 1) { |
|
33
|
|
|
$segments['domain'] = $parts[0]; |
|
34
|
|
|
|
|
35
|
|
|
return $segments; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if (count($parts)) { |
|
39
|
|
|
$segments['tld'] = array_pop($parts); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if (count($parts)) { |
|
43
|
|
|
$segments['domain'] = array_pop($parts); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if (count($parts)) { |
|
47
|
|
|
$segments['subdomain'] = implode('.', $parts); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $this->replaceEmptyStringsWithNull($segments); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param $string |
|
55
|
|
|
* |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function parseSegments($string) { |
|
59
|
|
|
preg_match_all( |
|
60
|
|
|
'/^' . |
|
61
|
|
|
'((?P<protocol>.*):\/\/)?' . |
|
62
|
|
|
'(' . |
|
63
|
|
|
'(?P<username>.*?)' . |
|
64
|
|
|
'(:(?P<password>.*?)' . |
|
65
|
|
|
')@)?' . |
|
66
|
|
|
'(?P<host>[^:\/\s]+)?' . |
|
67
|
|
|
'(:(?P<port>\d*))?' . |
|
68
|
|
|
'(?P<path>[^#\?]*)?' . |
|
69
|
|
|
'(\?(?P<query>[^#]*))?' . |
|
70
|
|
|
'(#(?P<fragment>.*))?' . |
|
71
|
|
|
'$/', |
|
72
|
|
|
$string, |
|
73
|
|
|
$segments |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
foreach ($segments as $key => $value) { |
|
|
|
|
|
|
77
|
|
|
if (is_numeric($key)) { |
|
78
|
|
|
unset($segments[$key]); |
|
79
|
|
|
} else { |
|
80
|
|
|
$segments[$key] = array_pop($value); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$host = array_get($segments, 'host'); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
if ($host && $host !== 'localhost' && stripos($host, '.') === false) { |
|
87
|
|
|
$segments['path'] = '/' . $host . array_get($segments, 'path'); |
|
88
|
|
|
$segments['host'] = null; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $segments; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param $array |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function replaceEmptyStringsWithNull(array $array) { |
|
100
|
|
|
return array_map(function($item) { |
|
101
|
|
|
return ($item === '') ? null : $item; |
|
102
|
|
|
}, $array); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.