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