|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Albert221\Validation\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use Albert221\Validation\Rule; |
|
8
|
|
|
use Albert221\Validation\RuleValidator; |
|
9
|
|
|
use Albert221\Validation\Verdict; |
|
10
|
|
|
use Albert221\Validation\VerdictInterface; |
|
11
|
|
|
|
|
12
|
|
|
class UrlValidator extends RuleValidator |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Regex borrowed from Symfony Validator under MIT license: |
|
16
|
|
|
* https://github.com/symfony/validator/blob/master/LICENSE |
|
17
|
|
|
* |
|
18
|
|
|
* @see https://github.com/symfony/validator/blob/master/Constraints/UrlValidator.php#L24 |
|
19
|
|
|
*/ |
|
20
|
|
|
private const PATTERN = '~^ |
|
21
|
|
|
(http|https):// # protocol |
|
22
|
|
|
(([\.\pL\pN-]+:)?([\.\pL\pN-]+)@)? # basic auth |
|
23
|
|
|
( |
|
24
|
|
|
([\pL\pN\pS\-\.])+(\.?([\pL\pN]|xn\-\-[\pL\pN-]+)+\.?) # a domain name |
|
25
|
|
|
| # or |
|
26
|
|
|
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # an IP address |
|
27
|
|
|
| # or |
|
28
|
|
|
\[ |
|
29
|
|
|
(?:(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))| |
|
30
|
|
|
(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])? |
|
31
|
|
|
[0-9])))))))|(?:(?:::(?:(?:(?:[0-9a-f]{1,4})):){5})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?: |
|
32
|
|
|
(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]| |
|
33
|
|
|
(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})): |
|
34
|
|
|
){4})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]| |
|
35
|
|
|
2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?: |
|
36
|
|
|
[0-9a-f]{1,4})):){0,1}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){3})(?:(?:(?:(?:(?: |
|
37
|
|
|
[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3} |
|
38
|
|
|
(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,2}(?: |
|
39
|
|
|
(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){2})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?: |
|
40
|
|
|
[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]| |
|
41
|
|
|
1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,3}(?:(?:[0-9a-f]{1,4})))?::(?: |
|
42
|
|
|
(?:[0-9a-f]{1,4})):)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]| |
|
43
|
|
|
(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?: |
|
44
|
|
|
(?:(?:[0-9a-f]{1,4})):){0,4}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?: |
|
45
|
|
|
(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]| |
|
46
|
|
|
(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,5}(?:(?:[0-9a-f]{1,4})))? |
|
47
|
|
|
::)(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,6}(?:(?:[0-9a-f]{1,4})))?::)))) |
|
48
|
|
|
\] # an IPv6 address |
|
49
|
|
|
) |
|
50
|
|
|
(:[0-9]+)? # a port (optional) |
|
51
|
|
|
(?:/ (?:[\pL\pN\-._\~!$&\'()*+,;=:@]|%%[0-9A-Fa-f]{2})* )* # a path |
|
52
|
|
|
(?:\? (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )? # a query (optional) |
|
53
|
|
|
(?:\# (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )? # a fragment (optional) |
|
54
|
|
|
$~ixu'; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function verdict($value, Rule $rule): VerdictInterface |
|
60
|
|
|
{ |
|
61
|
1 |
|
if (is_null($value)) { |
|
62
|
1 |
|
return Verdict::create(true, $rule); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
return Verdict::create((bool) preg_match(self::PATTERN, $value), $rule); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|