1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helldar\Support\Helpers\Http; |
4
|
|
|
|
5
|
|
|
use Helldar\Support\Exceptions\NotValidUrlException; |
6
|
|
|
use Helldar\Support\Facades\Http\Builder as UrlBuilder; |
7
|
|
|
use Throwable; |
8
|
|
|
|
9
|
|
|
class Url |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Parsing URL into components. |
13
|
|
|
* |
14
|
|
|
* @param \Psr\Http\Message\UriInterface|string|null $url |
15
|
|
|
* |
16
|
|
|
* @return \Helldar\Support\Helpers\Http\Builder |
17
|
|
|
*/ |
18
|
6 |
|
public function parse($url): Builder |
19
|
|
|
{ |
20
|
6 |
|
return UrlBuilder::parse($url); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Check if the string is a valid URL. |
25
|
|
|
* |
26
|
|
|
* @param \Psr\Http\Message\UriInterface|string|null $url |
27
|
|
|
* |
28
|
|
|
* @return bool |
29
|
|
|
*/ |
30
|
160 |
|
public function is($url): bool |
31
|
|
|
{ |
32
|
160 |
|
return filter_var((string) $url, FILTER_VALIDATE_URL) !== false; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Validate if the value is a valid URL or throw an error. |
37
|
|
|
* |
38
|
|
|
* @param \Psr\Http\Message\UriInterface|string|null $url |
39
|
|
|
* |
40
|
|
|
* @throws \Helldar\Support\Exceptions\NotValidUrlException |
41
|
|
|
*/ |
42
|
160 |
|
public function validate($url): void |
43
|
|
|
{ |
44
|
160 |
|
if (! $this->is($url)) { |
45
|
12 |
|
throw new NotValidUrlException((string) $url); |
46
|
|
|
} |
47
|
148 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns the URL after validation, or throws an error. |
51
|
|
|
* |
52
|
|
|
* @param \Psr\Http\Message\UriInterface|string|null $url |
53
|
|
|
* |
54
|
|
|
* @throws \Helldar\Support\Exceptions\NotValidUrlException |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
9 |
|
public function validated($url): string |
59
|
|
|
{ |
60
|
9 |
|
$this->validate($url); |
61
|
|
|
|
62
|
9 |
|
return (string) $url; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Check if the specified URL exists. |
67
|
|
|
* |
68
|
|
|
* @param \Psr\Http\Message\UriInterface|string|null $url |
69
|
|
|
* |
70
|
|
|
* @throws \Helldar\Support\Exceptions\NotValidUrlException |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
12 |
|
public function exists($url): bool |
75
|
|
|
{ |
76
|
12 |
|
$this->validate($url); |
77
|
|
|
|
78
|
|
|
try { |
79
|
9 |
|
$headers = get_headers($url); |
|
|
|
|
80
|
|
|
|
81
|
9 |
|
$key = array_search('HTTP/', $headers); |
82
|
|
|
|
83
|
9 |
|
$value = $headers[$key] ?? null; |
84
|
|
|
|
85
|
9 |
|
preg_match('/HTTP\/\d{1}\.?\d?\s[2-3]\d{2}/i', $value, $matches); |
|
|
|
|
86
|
|
|
|
87
|
9 |
|
return count($matches) > 0; |
88
|
3 |
|
} catch (Throwable $e) { |
89
|
3 |
|
return false; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Check the existence of the URL and return the default value if it is missing. |
95
|
|
|
* |
96
|
|
|
* @param \Psr\Http\Message\UriInterface|string $url |
97
|
|
|
* @param \Psr\Http\Message\UriInterface|string $default |
98
|
|
|
* |
99
|
|
|
* @throws \Helldar\Support\Exceptions\NotValidUrlException |
100
|
|
|
* |
101
|
|
|
* @return string|null |
102
|
|
|
*/ |
103
|
3 |
|
public function default($url, $default): string |
104
|
|
|
{ |
105
|
3 |
|
$value = $this->exists($url) ? $url : $default; |
106
|
|
|
|
107
|
3 |
|
return $this->validated($value); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|