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