|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Helldar\Support\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Helldar\Support\Exceptions\NotValidUrlException; |
|
7
|
|
|
use Helldar\Support\Facades\Helpers\Filesystem\File as FileHelper; |
|
8
|
|
|
use Helldar\Support\Facades\Helpers\HttpBuilder as HttpBuilderHelper; |
|
9
|
|
|
|
|
10
|
|
|
final class Http |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Check if the string is a valid URL. |
|
14
|
|
|
* |
|
15
|
|
|
* @param string|null $url |
|
16
|
|
|
* |
|
17
|
|
|
* @return bool |
|
18
|
|
|
*/ |
|
19
|
48 |
|
public function isUrl(?string $url): bool |
|
20
|
|
|
{ |
|
21
|
48 |
|
return filter_var($url, FILTER_VALIDATE_URL) !== false; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Validate if the value is a valid URL or throw an error. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string|null $url |
|
28
|
|
|
* |
|
29
|
|
|
* @throws \Helldar\Support\Exceptions\NotValidUrlException |
|
30
|
|
|
*/ |
|
31
|
46 |
|
public function validateUrl(?string $url): void |
|
32
|
|
|
{ |
|
33
|
46 |
|
if (! $this->isUrl($url)) { |
|
34
|
30 |
|
throw new NotValidUrlException($url); |
|
35
|
|
|
} |
|
36
|
16 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Returns the URL after validation, or throws an error. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string|null $url |
|
42
|
|
|
* |
|
43
|
|
|
* @throws \Helldar\Support\Exceptions\NotValidUrlException |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
4 |
|
public function validatedUrl(?string $url): string |
|
47
|
|
|
{ |
|
48
|
4 |
|
$this->validateUrl($url); |
|
49
|
|
|
|
|
50
|
2 |
|
return $url; |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Check if the specified URL exists. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string|null $url |
|
57
|
|
|
* |
|
58
|
|
|
* @throws \Helldar\Support\Exceptions\NotValidUrlException |
|
59
|
|
|
* |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
6 |
|
public function exists(?string $url): bool |
|
63
|
|
|
{ |
|
64
|
6 |
|
$this->validateUrl($url); |
|
65
|
|
|
|
|
66
|
|
|
try { |
|
67
|
4 |
|
$headers = get_headers($url); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
4 |
|
$key = array_search('HTTP/', $headers); |
|
70
|
4 |
|
$value = $headers[$key] ?? null; |
|
71
|
|
|
|
|
72
|
4 |
|
preg_match('/HTTP\/\d{1}\.?\d?\s[2-3]\d{2}/i', $value, $matches); |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
4 |
|
return count($matches) > 0; |
|
75
|
|
|
} |
|
76
|
4 |
|
catch (Exception $e) { |
|
77
|
4 |
|
return false; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get the domain name from the URL. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string|null $url |
|
85
|
|
|
* |
|
86
|
|
|
* @throws \Helldar\Support\Exceptions\NotValidUrlException |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
8 |
|
public function domain(?string $url): string |
|
91
|
|
|
{ |
|
92
|
8 |
|
$this->validateUrl($url); |
|
93
|
|
|
|
|
94
|
2 |
|
return HttpBuilderHelper::parse($url)->getHost(); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get the subdomain name from the URL. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string|null $url |
|
101
|
|
|
* |
|
102
|
|
|
* @throws \Helldar\Support\Exceptions\NotValidUrlException |
|
103
|
|
|
* |
|
104
|
|
|
* @return string|null |
|
105
|
|
|
*/ |
|
106
|
8 |
|
public function subdomain(?string $url): ?string |
|
107
|
|
|
{ |
|
108
|
8 |
|
$this->validateUrl($url); |
|
109
|
|
|
|
|
110
|
2 |
|
$host = explode('.', HttpBuilderHelper::parse($url)->getHost()); |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
2 |
|
return count($host) > 2 ? reset($host) : null; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get the scheme and host from the URL. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string|null $url |
|
119
|
|
|
* |
|
120
|
|
|
* @throws \Helldar\Support\Exceptions\NotValidUrlException |
|
121
|
|
|
* |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
8 |
|
public function host(?string $url): string |
|
125
|
|
|
{ |
|
126
|
8 |
|
$this->validateUrl($url); |
|
127
|
|
|
|
|
128
|
2 |
|
return HttpBuilderHelper::same() |
|
129
|
2 |
|
->parse($url, PHP_URL_SCHEME) |
|
|
|
|
|
|
130
|
2 |
|
->parse($url, PHP_URL_HOST) |
|
131
|
2 |
|
->compile(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get the scheme name from the URL. |
|
136
|
|
|
* |
|
137
|
|
|
* @param string|null $url |
|
138
|
|
|
* |
|
139
|
|
|
* @throws \Helldar\Support\Exceptions\NotValidUrlException |
|
140
|
|
|
* |
|
141
|
|
|
* @return string |
|
142
|
|
|
*/ |
|
143
|
8 |
|
public function scheme(?string $url): string |
|
144
|
|
|
{ |
|
145
|
8 |
|
$this->validateUrl($url); |
|
146
|
|
|
|
|
147
|
2 |
|
return HttpBuilderHelper::parse($url)->getScheme(); |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Check the existence of the file and return the default value if it is missing. |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $url |
|
154
|
|
|
* @param string|null $default |
|
155
|
|
|
* |
|
156
|
|
|
* @throws \Helldar\Support\Exceptions\NotValidUrlException |
|
157
|
|
|
* |
|
158
|
|
|
* @return string|null |
|
159
|
|
|
*/ |
|
160
|
2 |
|
public function image(string $url, string $default = null): ?string |
|
161
|
|
|
{ |
|
162
|
2 |
|
return $this->isUrl($url) |
|
163
|
2 |
|
? ($this->exists($url) ? $url : $default) |
|
164
|
2 |
|
: (FileHelper::exists($url) ? $url : $default); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|