| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Yiisoft\Validator\Rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Closure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Yiisoft\Validator\Rule\Trait\WhenTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Yiisoft\Validator\SerializableRuleInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Yiisoft\Validator\SkipOnEmptyInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Yiisoft\Validator\SkipOnErrorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Yiisoft\Validator\ValidationContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Yiisoft\Validator\WhenInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | abstract class Compare implements SerializableRuleInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     use SkipOnEmptyTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     use SkipOnErrorTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     use WhenTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * Constant for specifying the comparison as string values. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * Values will be converted to strings before comparison. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @see $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public const TYPE_STRING = 'string'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Constant for specifying the comparison as numeric values. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Values will be converted to float numbers before comparison. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @see $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public const TYPE_NUMBER = 'number'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private array $validOperatorsMap = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         '==' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         '===' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         '!=' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         '!==' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         '>' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         '>=' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         '<' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         '<=' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 33 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |          * @var scalar|null The constant value to be compared with. When both this property and {@see $targetAttribute} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |          * are set, this property takes precedence. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         private int|float|string|bool|null $targetValue = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |          * @var string|null The name of the attribute to be compared with. When both this property and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |          * {@see $targetValue} are set, the {@see $targetValue} takes precedence. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         private string|null $targetAttribute = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         private string $incorrectInputMessage = 'The allowed types are integer, float, string, boolean and null.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         private string $incorrectDataSetTypeMessage = 'The attribute value returned from a custom data set must have ' . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         'a scalar type.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |          * @var string|null User-defined error message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         private string|null $message = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |          * @var string The type of the values being compared. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         private string $type = self::TYPE_STRING, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |          * @var string The operator for comparison. The following operators are supported: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |          * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |          * - `==`: check if two values are equal. The comparison is done in non-strict mode. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |          * - `===`: check if two values are equal. The comparison is done in strict mode. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |          * - `!=`: check if two values are NOT equal. The comparison is done in non-strict mode. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |          * - `!==`: check if two values are NOT equal. The comparison is done in strict mode. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |          * - `>`: check if value being validated is greater than the value being compared with. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |          * - `>=`: check if value being validated is greater than or equal to the value being compared with. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |          * - `<`: check if value being validated is less than the value being compared with. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |          * - `<=`: check if value being validated is less than or equal to the value being compared with. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |          * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |          * When you want to compare numbers, make sure to also change {@see $type} to {@see TYPE_NUMBER}. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         private string $operator = '==', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |          * @var bool|callable|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         private mixed $skipOnEmpty = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         private bool $skipOnError = false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |          * @var Closure(mixed, ValidationContext):bool|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         private ?Closure $when = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 33 |  |         if (!isset($this->validOperatorsMap[$this->operator])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             throw new InvalidArgumentException("Operator \"$operator\" is not supported."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @return scalar|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 72 |  |     public function getTargetValue(): int|float|string|bool|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 72 |  |         return $this->targetValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 72 |  |     public function getTargetAttribute(): string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 72 |  |         return $this->targetAttribute; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 71 |  |     public function getType(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 71 |  |         return $this->type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 71 |  |     public function getOperator(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 71 |  |         return $this->operator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 1 |  |     public function getIncorrectInputMessage(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 1 |  |         return $this->incorrectInputMessage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 1 |  |     public function getIncorrectDataSetTypeMessage(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 1 |  |         return $this->incorrectDataSetTypeMessage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 135 | 79 |  |     public function getMessage(): string | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 137 | 79 |  |         return $this->message ?? match ($this->operator) { | 
            
                                                                        
                            
            
                                    
            
            
                | 138 | 21 |  |             '==', '===' => 'Value must be equal to "{targetValueOrAttribute}".', | 
            
                                                                        
                            
            
                                    
            
            
                | 139 | 15 |  |             '!=', '!==' => 'Value must not be equal to "{targetValueOrAttribute}".', | 
            
                                                                        
                            
            
                                    
            
            
                | 140 | 7 |  |             '>' => 'Value must be greater than "{targetValueOrAttribute}".', | 
            
                                                                        
                            
            
                                    
            
            
                | 141 | 7 |  |             '>=' => 'Value must be greater than or equal to "{targetValueOrAttribute}".', | 
            
                                                                        
                            
            
                                    
            
            
                | 142 | 7 |  |             '<' => 'Value must be less than "{targetValueOrAttribute}".', | 
            
                                                                        
                            
            
                                    
            
            
                | 143 | 79 |  |             '<=' => 'Value must be less than or equal to "{targetValueOrAttribute}".', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 40 |  |     public function getOptions(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 40 |  |         $messageParameters = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 40 |  |             'targetValue' => $this->targetValue, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 40 |  |             'targetAttribute' => $this->targetAttribute, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 40 |  |             'targetValueOrAttribute' => $this->targetValue ?? $this->targetAttribute, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 40 |  |             'targetValue' => $this->targetValue, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 40 |  |             'targetAttribute' => $this->targetAttribute, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             'incorrectInputMessage' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 40 |  |                 'message' => $this->incorrectInputMessage, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |                 'parameters' => $messageParameters, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             'incorrectDataSetTypeMessage' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 40 |  |                 'message' => $this->incorrectDataSetTypeMessage, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |                 'parameters' => $messageParameters, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             'message' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 40 |  |                 'message' => $this->getMessage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |                 'parameters' => $messageParameters, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 40 |  |             'type' => $this->type, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 40 |  |             'operator' => $this->operator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 40 |  |             'skipOnEmpty' => $this->getSkipOnEmptyOption(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 40 |  |             'skipOnError' => $this->skipOnError, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 73 |  |     public function getHandlerClassName(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 73 |  |         return CompareHandler::class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 181 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 182 |  |  |  |