| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Yiisoft\Validator\Rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Yiisoft\Validator\HasValidationErrorMessage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Yiisoft\Validator\Result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Yiisoft\Validator\Rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Yiisoft\Validator\ValidationContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * BooleanValidator checks if the attribute value is a boolean value or a value corresponding to it. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class Boolean extends Rule | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     use HasValidationErrorMessage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var mixed the value representing true status. Defaults to '1'. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $trueValue = '1'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var mixed the value representing false status. Defaults to '0'. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $falseValue = '0'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @var bool whether the comparison to {@see trueValue()} and {@see falseValue()} is strict. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * When this is true, the attribute value and type must both match those of {@see trueValue()} or {@see falseValue()}. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * Defaults to false, meaning only the value needs to be matched. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private bool $strict = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private string $message = 'The value must be either "{true}" or "{false}".'; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 2 |  |     public static function rule(): self | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 2 |  |         return new self(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function trueValue($value): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $new = clone $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $new->trueValue = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         return $new; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function falseValue($value): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $new = clone $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $new->falseValue = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return $new; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function strict(bool $value): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $new = clone $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $new->strict = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return $new; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 20 |  |     protected function validateValue($value, ValidationContext $context = null): Result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 20 |  |         if ($this->strict) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 8 |  |             $valid = $value === $this->trueValue || $value === $this->falseValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 12 |  |             $valid = $value == $this->trueValue || $value == $this->falseValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 20 |  |         $result = new Result(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 20 |  |         if (!$valid) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 7 |  |             $result->addError( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 7 |  |                 $this->formatMessage( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 7 |  |                     $this->message, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 7 |  |                         'true' => $this->trueValue === true ? 'true' : $this->trueValue, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 7 |  |                         'false' => $this->falseValue === false ? 'false' : $this->falseValue, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 20 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 7 |  |     public function getOptions(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 7 |  |         return array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 7 |  |             parent::getOptions(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 7 |  |                 'strict' => $this->strict, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 7 |  |                 'trueValue' => $this->trueValue, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 7 |  |                 'falseValue' => $this->falseValue, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 7 |  |                 'message' => $this->formatMessage( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 7 |  |                     $this->message, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 7 |  |                         'true' => $this->trueValue === true ? 'true' : $this->trueValue, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 7 |  |                         'false' => $this->falseValue === false ? 'false' : $this->falseValue, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 105 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |  |