| 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\SkipOnEmptyTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Yiisoft\Validator\Rule\Trait\WhenTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Yiisoft\Validator\RuleWithOptionsInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Yiisoft\Validator\SkipOnEmptyInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Yiisoft\Validator\SkipOnErrorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Yiisoft\Validator\WhenInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use function function_exists; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * Validates that the value is a valid HTTP or HTTPS URL. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * Note that this rule only checks if the URL scheme and host parts are correct. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * It does not check the remaining parts of a URL. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @psalm-import-type WhenType from WhenInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | final class Url implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     use SkipOnEmptyTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     use SkipOnErrorTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     use WhenTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 4 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |          * @var string The regular expression used to validate the value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |          * The pattern may contain a `{schemes}` token that will be replaced | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |          * by a regular expression which represents the {@see $schemes}. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |          * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |          * Note that if you want to reuse the pattern in HTML5 input, it should have `^` and `$`, should not have any | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |          * modifiers, and should not be case-insensitive. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         private string $pattern = '/^{schemes}:\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\d{1,5})?([?\/#].*$|$)/', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |          * @var string[] List of URI schemes which should be considered valid. By default, http and https | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |          * are considered to be valid schemes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         private array $validSchemes = ['http', 'https'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |          * @var bool Whether validation process should take into account IDN (internationalized | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |          * domain names). Defaults to `false` meaning that validation of URLs containing IDN will always | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |          * fail. Note that in order to use IDN validation you have to install and enable `intl` PHP | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |          * extension, otherwise an exception would be thrown. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         private bool $enableIDN = false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |          * @var string A message used when the input it incorrect. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         private string $incorrectInputMessage = 'The value must have a string type.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |          * @var string A message used when the value is not valid. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         private string $message = 'This value is not a valid URL.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |          * @var bool|callable|null Whether to skip this rule if the value validated is empty. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |          * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |          * @see SkipOnEmptyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 4 |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         private $skipOnEmpty = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |          * @var bool Whether to skip this rule if any of the previous rules gave an error. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         private bool $skipOnError = false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |          * @var Closure|null A callable to define a condition for applying the rule. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |          * @psalm-var WhenType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |          * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |          * @see WhenInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         private Closure|null $when = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 44 |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         if ($enableIDN && !function_exists('idn_to_ascii')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 44 |  |             // Tested via separate CI configuration (see ".github/workflows/build.yml"). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             // @codeCoverageIgnoreStart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             // @codeCoverageIgnoreEnd | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public function getName(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 2 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         return 'url'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 40 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 40 |  |      * @return string Ready to use regular expression pattern used for URL validation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     public function getPattern(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 6 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         return str_replace('{schemes}', '((?i)' . implode('|', $this->validSchemes) . ')', $this->pattern); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 6 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 19 |  |      * @return string[] List of URI schemes which should be considered valid. By default, http and https | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * are considered to be valid schemes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 19 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     public function getValidSchemes(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 4 |  |         return $this->validSchemes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 4 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 4 |  |      * @return bool Whether validation process should take into account IDN (internationalized | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 4 |  |      * domain names). `false` meaning that validation of URLs containing IDN will always | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * fail. Note that in order to use IDN validation you have to install and enable `intl` PHP | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 4 |  |      * extension, otherwise an exception would be thrown. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |     public function isIDNEnabled(): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 121 | 4 |  |         return $this->enableIDN; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 4 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 4 |  |      * @return string A message used when the input it incorrect. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     public function getIncorrectInputMessage(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 48 |  |         return $this->incorrectInputMessage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 48 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @return string A message used when the value is not valid. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     public function getMessage(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         return $this->message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     public function getOptions(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             'pattern' => $this->getPattern(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             'validSchemes' => $this->validSchemes, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             'enableIDN' => $this->enableIDN, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             'incorrectInputMessage' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                 'template' => $this->incorrectInputMessage, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                 'parameters' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             'message' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                 'template' => $this->message, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |                 'parameters' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             'skipOnEmpty' => $this->getSkipOnEmptyOption(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             'skipOnError' => $this->skipOnError, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     public function getHandler(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         return UrlHandler::class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 163 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 164 |  |  |  |