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