1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Attribute; |
8
|
|
|
use Closure; |
9
|
|
|
use RuntimeException; |
10
|
|
|
use Yiisoft\Validator\Rule\Trait\RuleNameTrait; |
|
|
|
|
11
|
|
|
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait; |
12
|
|
|
use Yiisoft\Validator\RuleInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Validates that the value is a valid HTTP or HTTPS URL. |
16
|
|
|
* |
17
|
|
|
* Note that this rule only checks if the URL scheme and host part are correct. |
18
|
|
|
* It does not check the remaining parts of a URL. |
19
|
|
|
*/ |
20
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
21
|
|
|
final class Url implements RuleInterface |
22
|
|
|
{ |
23
|
|
|
use HandlerClassNameTrait; |
24
|
|
|
use RuleNameTrait; |
25
|
|
|
|
26
|
7 |
|
public function __construct( |
27
|
|
|
/** |
28
|
|
|
* @var string the regular expression used to validate the value. |
29
|
|
|
* The pattern may contain a `{schemes}` token that will be replaced |
30
|
|
|
* by a regular expression which represents the {@see $schemes}. |
31
|
|
|
* |
32
|
|
|
* Note that if you want to reuse the pattern in HTML5 input it should have ^ and $, should not have any |
33
|
|
|
* modifiers and should not be case-insensitive. |
34
|
|
|
*/ |
35
|
|
|
private string $pattern = '/^{schemes}:\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\d{1,5})?([?\/#].*$|$)/', |
36
|
|
|
/** |
37
|
|
|
* @var array list of URI schemes which should be considered valid. By default, http and https |
38
|
|
|
* are considered to be valid schemes. |
39
|
|
|
*/ |
40
|
|
|
public array $validSchemes = ['http', 'https'], |
41
|
|
|
/** |
42
|
|
|
* @var bool whether validation process should take into account IDN (internationalized |
43
|
|
|
* domain names). Defaults to false meaning that validation of URLs containing IDN will always |
44
|
|
|
* fail. Note that in order to use IDN validation you have to install and enable `intl` PHP |
45
|
|
|
* extension, otherwise an exception would be thrown. |
46
|
|
|
*/ |
47
|
|
|
public bool $enableIDN = false, |
48
|
|
|
public string $message = 'This value is not a valid URL.', |
49
|
|
|
public bool $skipOnEmpty = false, |
50
|
|
|
public bool $skipOnError = false, |
51
|
|
|
public ?Closure $when = null, |
52
|
|
|
) { |
53
|
7 |
|
if ($enableIDN && !function_exists('idn_to_ascii')) { |
54
|
1 |
|
throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.'); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
46 |
|
public function getPattern(): string |
59
|
|
|
{ |
60
|
46 |
|
if (str_contains($this->pattern, '{schemes}')) { |
61
|
43 |
|
return str_replace('{schemes}', '((?i)' . implode('|', $this->validSchemes) . ')', $this->pattern); |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
return $this->pattern; |
65
|
|
|
} |
66
|
|
|
|
67
|
5 |
|
public function getOptions(): array |
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
5 |
|
'pattern' => $this->getPattern(), |
71
|
5 |
|
'validSchemes' => $this->validSchemes, |
72
|
5 |
|
'enableIDN' => $this->enableIDN, |
73
|
|
|
'message' => [ |
74
|
5 |
|
'message' => $this->message, |
75
|
|
|
], |
76
|
5 |
|
'skipOnEmpty' => $this->skipOnEmpty, |
77
|
5 |
|
'skipOnError' => $this->skipOnError, |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|