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