1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HumanDirect\Socially\Normalizer; |
4
|
|
|
|
5
|
|
|
use HumanDirect\Socially\Factory; |
6
|
|
|
use HumanDirect\Socially\Util; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class DefaultNormalizer. |
10
|
|
|
*/ |
11
|
|
|
class DefaultNormalizer implements NormalizerInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var bool |
15
|
|
|
*/ |
16
|
|
|
protected $stripSubdomain = true; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var bool |
20
|
|
|
*/ |
21
|
|
|
protected $stripQueryStrings = true; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $allowedQueryParams = [ |
27
|
|
|
'id' |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Normalize a URL. |
32
|
|
|
* |
33
|
|
|
* @param string $url |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public function normalize(string $url): string |
38
|
|
|
{ |
39
|
|
|
$url = rawurldecode($url); |
40
|
|
|
$url = $this->cleanUrl($url); |
41
|
|
|
$url = str_replace('http://', 'https://', $url); |
42
|
|
|
|
43
|
|
|
return $this->afterNormalization($url); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function afterNormalization(string $url): string |
50
|
|
|
{ |
51
|
|
|
if (true === $this->stripSubdomain) { |
52
|
|
|
$tldExtractor = Factory::createTldExtractor(); |
53
|
|
|
$result = $tldExtractor->parse($url); |
54
|
|
|
$subdomain = $result->getSubdomain(); |
55
|
|
|
|
56
|
|
|
if (null === $subdomain) { |
57
|
|
|
return $url; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$url = str_replace($subdomain . '.', '', $url); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (true === $this->stripQueryStrings) { |
64
|
|
|
if (empty($this->allowedQueryParams)) { |
65
|
|
|
$url = strtok($url, '?'); |
66
|
|
|
} else { |
67
|
|
|
// first remove fragments from URL |
68
|
|
|
$url = strtok($url, '#'); |
69
|
|
|
$query = parse_url($url, PHP_URL_QUERY); |
70
|
|
|
|
71
|
|
|
if (null !== $query) { |
72
|
|
|
parse_str($query, $params); |
73
|
|
|
foreach ($params as $paramKey => $paramValue) { |
74
|
|
|
if (!\in_array($paramKey, $this->allowedQueryParams, true)) { |
75
|
|
|
unset($params[$paramKey]); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$url = strtok($url, '?') . (\count($params) ? '?' . http_build_query($params) : ''); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $url; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
public function supports(string $platform): bool |
91
|
|
|
{ |
92
|
|
|
return $this->getName() === Util::toCamelCase($platform); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @throws \ReflectionException |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
protected function getName(): string |
101
|
|
|
{ |
102
|
|
|
$shortName = (new \ReflectionClass($this))->getShortName(); |
103
|
|
|
|
104
|
|
|
return substr($shortName, 0, -10); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $url |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
protected function cleanUrl(string $url): string |
113
|
|
|
{ |
114
|
|
|
return rtrim(Util::cleanUrl($url), '/'); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|