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