| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Yiisoft\Validator\Rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Attribute; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Closure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use JetBrains\PhpStorm\ArrayShape; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Yiisoft\Validator\SerializableRuleInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Yiisoft\Validator\BeforeValidationInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Yiisoft\Validator\Rule\Trait\RuleNameTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Yiisoft\Validator\ValidationContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Validates that the value matches the pattern specified in constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * If the {@see Regex::$not} is used, the rule will ensure the value do NOT match the pattern. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  | final class Regex implements SerializableRuleInterface, BeforeValidationInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     use BeforeValidationTrait; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     use RuleNameTrait; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 3 |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         /** | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |          * @var string the regular expression to be matched with | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |          */ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         private string $pattern, | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         /** | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |          * @var bool whether to invert the validation logic. Defaults to `false`. If set to `true`, the regular | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |          * expression defined via {@see $pattern} should NOT match the value. | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |          */ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         private bool $not = false, | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         private string $incorrectInputMessage = 'Value should be string.', | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         private string $message = 'Value is invalid.', | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         private bool $skipOnEmpty = false, | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         private bool $skipOnError = false, | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         /** | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |          * @var Closure(mixed, ValidationContext):bool|null | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |          */ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         private ?Closure $when = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 7 |  |     public function getPattern(): string | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 7 |  |         return $this->pattern; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 7 |  |     public function isNot(): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 7 |  |         return $this->not; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 7 |  |     public function getIncorrectInputMessage(): string | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 7 |  |         return $this->incorrectInputMessage; | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 4 |  |     public function getMessage(): string | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 4 |  |         return $this->message; | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 2 |  |     #[ArrayShape([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         'pattern' => 'string', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         'not' => 'bool', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         'incorrectInputMessage' => 'string[]', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         'message' => 'string[]', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         'skipOnEmpty' => 'bool', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         'skipOnError' => 'bool', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     ])] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     public function getOptions(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 2 |  |             'pattern' => $this->pattern, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 2 |  |             'not' => $this->not, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             'incorrectInputMessage' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 2 |  |                 'message' => $this->incorrectInputMessage, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             'message' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 2 |  |                 'message' => $this->message, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 2 |  |             'skipOnEmpty' => $this->skipOnEmpty, | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 | 2 |  |             'skipOnError' => $this->skipOnError, | 
            
                                                                        
                                                                
            
                                    
            
            
                | 101 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 3 |  |     public function getHandlerClassName(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 106 | 3 |  |         return RegexHandler::class; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 108 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 109 |  |  |  |