|
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 RuntimeException; |
|
11
|
|
|
use Yiisoft\Validator\ParametrizedRuleInterface; |
|
12
|
|
|
use Yiisoft\Validator\BeforeValidationInterface; |
|
13
|
|
|
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait; |
|
14
|
|
|
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait; |
|
15
|
|
|
use Yiisoft\Validator\Rule\Trait\RuleNameTrait; |
|
16
|
|
|
use Yiisoft\Validator\ValidationContext; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Validates if the specified value is not equal to another value or attribute. |
|
20
|
|
|
* |
|
21
|
|
|
* The value being validated with {@see NotEqual::$targetValue} or {@see NotEqual::$targetAttribute}, which |
|
22
|
|
|
* is set in the constructor. |
|
23
|
|
|
* |
|
24
|
|
|
* The default validation function is based on string values, which means the values |
|
25
|
|
|
* are equals byte by byte. When validating numbers, make sure to change {@see NotEqual::$type} to |
|
26
|
|
|
* {@see NotEqual::TYPE_NUMBER} to enable numeric validation. |
|
27
|
|
|
*/ |
|
28
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
|
29
|
|
|
final class NotEqual implements ParametrizedRuleInterface, BeforeValidationInterface |
|
30
|
|
|
{ |
|
31
|
|
|
use BeforeValidationTrait; |
|
32
|
|
|
use HandlerClassNameTrait; |
|
33
|
|
|
use RuleNameTrait; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Constant for specifying validation as string values. |
|
37
|
|
|
* No conversion will be done before validation. |
|
38
|
|
|
* |
|
39
|
|
|
* @see $type |
|
40
|
|
|
*/ |
|
41
|
|
|
public const TYPE_STRING = 'string'; |
|
42
|
|
|
/** |
|
43
|
|
|
* Constant for specifying validation as numeric values. |
|
44
|
|
|
* String values will be converted into numbers before validation. |
|
45
|
|
|
* |
|
46
|
|
|
* @see $type |
|
47
|
|
|
*/ |
|
48
|
|
|
public const TYPE_NUMBER = 'number'; |
|
49
|
|
|
|
|
50
|
1 |
|
public function __construct( |
|
51
|
|
|
/** |
|
52
|
|
|
* @var mixed the constant value to not be equal to. When both this property |
|
53
|
|
|
* and {@see $targetAttribute} are set, this property takes precedence. |
|
54
|
|
|
*/ |
|
55
|
|
|
private $targetValue = null, |
|
56
|
|
|
/** |
|
57
|
|
|
* @var mixed the constant value to not be equal to. When both this property |
|
58
|
|
|
* and {@see $targetValue} are set, the {@see $targetValue} takes precedence. |
|
59
|
|
|
*/ |
|
60
|
|
|
private ?string $targetAttribute = null, |
|
61
|
|
|
/** |
|
62
|
|
|
* @var string|null user-defined error message |
|
63
|
|
|
*/ |
|
64
|
|
|
private ?string $message = null, |
|
65
|
|
|
/** |
|
66
|
|
|
* @var string the type of the values being validated. |
|
67
|
|
|
*/ |
|
68
|
|
|
private string $type = self::TYPE_STRING, |
|
69
|
|
|
/** |
|
70
|
|
|
* @var bool Whether this validator strictly check. |
|
71
|
|
|
*/ |
|
72
|
|
|
private bool $strict = false, |
|
73
|
|
|
private bool $skipOnEmpty = false, |
|
74
|
|
|
private bool $skipOnError = false, |
|
75
|
|
|
/** |
|
76
|
|
|
* @var Closure(mixed, ValidationContext):bool|null |
|
77
|
|
|
*/ |
|
78
|
|
|
private ?Closure $when = null, |
|
79
|
|
|
) { |
|
80
|
1 |
|
if ($this->targetValue === null && $this->targetAttribute === null) { |
|
81
|
|
|
throw new RuntimeException('Either "targetValue" or "targetAttribute" must be specified.'); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return mixed |
|
87
|
|
|
*/ |
|
88
|
5 |
|
public function getTargetValue(): mixed |
|
89
|
|
|
{ |
|
90
|
5 |
|
return $this->targetValue; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return string|null |
|
95
|
|
|
*/ |
|
96
|
3 |
|
public function getTargetAttribute(): ?string |
|
97
|
|
|
{ |
|
98
|
3 |
|
return $this->targetAttribute; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
5 |
|
public function getType(): string |
|
105
|
|
|
{ |
|
106
|
5 |
|
return $this->type; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
5 |
|
public function shouldCheckStrictly(): bool |
|
110
|
|
|
{ |
|
111
|
5 |
|
return $this->strict; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
11 |
|
public function getMessage(): string |
|
115
|
|
|
{ |
|
116
|
11 |
|
return $this->message ?? 'Value must not be equal to "{targetValueOrAttribute}".'; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
8 |
|
#[ArrayShape([ |
|
120
|
|
|
'targetValue' => 'mixed', |
|
121
|
|
|
'targetAttribute' => 'string|null', |
|
122
|
|
|
'message' => 'array', |
|
123
|
|
|
'type' => 'string', |
|
124
|
|
|
'strict' => 'bool', |
|
125
|
|
|
'skipOnEmpty' => 'bool', |
|
126
|
|
|
'skipOnError' => 'bool', |
|
127
|
|
|
])] |
|
128
|
|
|
public function getOptions(): array |
|
129
|
|
|
{ |
|
130
|
|
|
return [ |
|
131
|
8 |
|
'targetValue' => $this->targetValue, |
|
132
|
8 |
|
'targetAttribute' => $this->targetAttribute, |
|
133
|
|
|
'message' => [ |
|
134
|
8 |
|
'message' => $this->getMessage(), |
|
135
|
|
|
'parameters' => [ |
|
136
|
8 |
|
'targetValue' => $this->targetValue, |
|
137
|
8 |
|
'targetAttribute' => $this->targetAttribute, |
|
138
|
8 |
|
'targetValueOrAttribute' => $this->targetValue ?? $this->targetAttribute, |
|
139
|
|
|
], |
|
140
|
|
|
], |
|
141
|
8 |
|
'type' => $this->type, |
|
142
|
8 |
|
'strict' => $this->strict, |
|
143
|
8 |
|
'skipOnEmpty' => $this->skipOnEmpty, |
|
144
|
8 |
|
'skipOnError' => $this->skipOnError, |
|
145
|
|
|
]; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|