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
|
2 |
|
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
|
|
|
private $skipOnEmptyCallback = null, |
55
|
|
|
private bool $skipOnError = false, |
56
|
|
|
/** |
57
|
|
|
* @var Closure(mixed, ValidationContext):bool|null |
58
|
|
|
*/ |
59
|
|
|
private ?Closure $when = null, |
60
|
|
|
) { |
61
|
2 |
|
$this->initSkipOnEmptyProperties($skipOnEmpty, $skipOnEmptyCallback); |
62
|
|
|
|
63
|
2 |
|
if ($enableIDN && !function_exists('idn_to_ascii')) { |
64
|
|
|
throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.'); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
41 |
|
public function getPattern(): string |
69
|
|
|
{ |
70
|
41 |
|
if (str_contains($this->pattern, '{schemes}')) { |
71
|
38 |
|
return str_replace('{schemes}', '((?i)' . implode('|', $this->validSchemes) . ')', $this->pattern); |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
return $this->pattern; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array|string[] |
79
|
|
|
*/ |
80
|
|
|
public function getValidSchemes(): array |
81
|
|
|
{ |
82
|
|
|
return $this->validSchemes; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
37 |
|
public function isEnableIDN(): bool |
89
|
|
|
{ |
90
|
37 |
|
return $this->enableIDN; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
16 |
|
public function getMessage(): string |
97
|
|
|
{ |
98
|
16 |
|
return $this->message; |
99
|
|
|
} |
100
|
|
|
|
101
|
4 |
|
public function getOptions(): array |
102
|
|
|
{ |
103
|
|
|
return [ |
104
|
4 |
|
'pattern' => $this->getPattern(), |
105
|
4 |
|
'validSchemes' => $this->validSchemes, |
106
|
4 |
|
'enableIDN' => $this->enableIDN, |
107
|
|
|
'message' => [ |
108
|
4 |
|
'message' => $this->message, |
109
|
|
|
], |
110
|
4 |
|
'skipOnEmpty' => $this->skipOnEmpty, |
111
|
4 |
|
'skipOnError' => $this->skipOnError, |
112
|
|
|
]; |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
public function getHandlerClassName(): string |
116
|
|
|
{ |
117
|
1 |
|
return UrlHandler::class; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|