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