|
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
|
|
|
public function parse($url): Builder |
|
19
|
|
|
{ |
|
20
|
|
|
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
|
|
|
public function is($url): bool |
|
31
|
|
|
{ |
|
32
|
|
|
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
|
|
|
public function validate($url): void |
|
43
|
|
|
{ |
|
44
|
|
|
if (! $this->is($url)) { |
|
45
|
|
|
throw new NotValidUrlException((string) $url); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
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
|
|
|
public function validated($url): string |
|
59
|
|
|
{ |
|
60
|
|
|
$this->validate($url); |
|
61
|
|
|
|
|
62
|
|
|
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
|
|
|
public function exists($url): bool |
|
75
|
|
|
{ |
|
76
|
|
|
$this->validate($url); |
|
77
|
|
|
|
|
78
|
|
|
try { |
|
79
|
|
|
$headers = get_headers($url); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
$key = array_search('HTTP/', $headers); |
|
82
|
|
|
|
|
83
|
|
|
$value = $headers[$key] ?? null; |
|
84
|
|
|
|
|
85
|
|
|
preg_match('/HTTP\/\d{1}\.?\d?\s[2-3]\d{2}/i', $value, $matches); |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
return count($matches) > 0; |
|
88
|
|
|
} catch (Throwable $e) { |
|
89
|
|
|
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
|
|
|
public function default($url, $default): string |
|
104
|
|
|
{ |
|
105
|
|
|
$value = $this->exists($url) ? $url : $default; |
|
106
|
|
|
|
|
107
|
|
|
return $this->validated($value); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|