1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Attribute; |
8
|
|
|
use Closure; |
9
|
|
|
use Yiisoft\Validator\WhenInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Defines validation options to check that the specified value is equal to "target" value provided directly |
13
|
|
|
* ({@see GreaterThanOrEqual::$targetValue}) or within an attribute ({@see GreaterThanOrEqual::$targetAttribute}). |
14
|
|
|
* |
15
|
|
|
* The default comparison is based on number values (including float values). It's also possible to compare values as |
16
|
|
|
* strings byte by byte and compare original values as is. See {@see GreaterThanOrEqual::$type} for all possible |
17
|
|
|
* options. |
18
|
|
|
* |
19
|
|
|
* `new GreaterThanOrEqual()` is a shortcut for `new Compare(operator: '>=')`. |
20
|
|
|
* |
21
|
|
|
* @see CompareHandler |
22
|
|
|
* @see AbstractCompare |
23
|
|
|
* |
24
|
|
|
* @psalm-import-type WhenType from WhenInterface |
25
|
4 |
|
*/ |
26
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
27
|
|
|
final class GreaterThanOrEqual extends AbstractCompare |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @param mixed $targetValue The value to be greater than or equal to. When both this property and |
31
|
|
|
* {@see $targetAttribute} are set, this property takes precedence. |
32
|
|
|
* @param string|null $targetAttribute The attribute to be greater than or equal to. When both this property and |
33
|
|
|
* {@see $targetValue} are set, the {@see $targetValue} takes precedence. |
34
|
|
|
* @param string $incorrectInputMessage A message used when the input is incorrect. |
35
|
|
|
* |
36
|
|
|
* You may use the following placeholders in the message: |
37
|
|
|
* |
38
|
|
|
* - `{attribute}`: the translated label of the attribute being validated. |
39
|
|
|
* - `{type}`: the type of the value being validated. |
40
|
|
|
* @param string $incorrectDataSetTypeMessage A message used when the value returned from a custom |
41
|
|
|
* data set s not scalar. |
42
|
|
|
* |
43
|
|
|
* You may use the following placeholders in the message: |
44
|
|
|
* |
45
|
|
|
* - `{type}`: type of the value. |
46
|
|
|
* @param string|null $message A message used when the value is not valid. |
47
|
|
|
* |
48
|
|
|
* You may use the following placeholders in the message: |
49
|
|
|
* |
50
|
|
|
* - `{attribute}`: the translated label of the attribute being validated. |
51
|
|
|
* - `{targetValue}`: the value to be compared with. |
52
|
|
|
* - `{targetAttribute}`: the name of the attribute to be compared with. |
53
|
|
|
* - `{targetAttributeValue}`: the value extracted from the attribute to be compared with if this attribute was set. |
54
|
4 |
|
* - `{targetValueOrAttribute}`: the value to be compared with or, if it's absent, the name of the attribute to be |
55
|
1 |
|
* compared with. |
56
|
|
|
* - `{value}`: the value being validated. |
57
|
|
|
* |
58
|
3 |
|
* When {@see CompareType::ORIGINAL} is used with complex types (neither scalar nor `null`), `{targetValue}`, |
59
|
3 |
|
* `{targetAttributeValue}` and `{targetValueOrAttribute}` parameters might contain the actual type instead of the |
60
|
3 |
|
* value, e.g. "object" for predictable formatting. |
61
|
3 |
|
* @param string $type The type of the values being compared: |
62
|
3 |
|
* |
63
|
3 |
|
* - {@see CompareType::NUMBER}: default, both values will be converted to float numbers before comparison. |
64
|
3 |
|
* - {@see CompareType::ORIGINAL} - compare the values as is. |
65
|
|
|
* - {@see CompareType::STRING} - cast both values to strings before comparison. |
66
|
|
|
* |
67
|
3 |
|
* {@see CompareType::NUMBER} and {@see CompareType::STRING} allow only scalar and `null` values, also objects |
68
|
3 |
|
* implementing {@see Stringable} interface. |
69
|
|
|
* |
70
|
|
|
* {@see CompareType::ORIGINAL} allows any values. All PHP comparison rules apply here, see comparison operators - |
71
|
|
|
* {@see https://www.php.net/manual/en/language.operators.comparison.php} and PHP type comparison tables - |
72
|
1 |
|
* {@see https://www.php.net/manual/en/types.comparisons.php} sections in official PHP documentation. |
73
|
|
|
* |
74
|
1 |
|
* @psalm-param CompareType::ORIGINAL | CompareType::STRING | CompareType::NUMBER $type |
75
|
|
|
* |
76
|
|
|
* @param bool|callable|null $skipOnEmpty Whether to skip this rule if the value validated is empty. |
77
|
|
|
* See {@see SkipOnEmptyInterface}. |
78
|
|
|
* @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error. |
79
|
|
|
* See {@see SkipOnErrorInterface}. |
80
|
|
|
* @param Closure|null $when A callable to define a condition for applying the rule. |
81
|
|
|
* See {@see WhenInterface}. |
82
|
|
|
* |
83
|
|
|
* @psalm-param WhenType $when |
84
|
|
|
*/ |
85
|
|
|
public function __construct( |
86
|
|
|
mixed $targetValue = null, |
87
|
|
|
string|null $targetAttribute = null, |
88
|
|
|
string $incorrectInputMessage = self::DEFAULT_INCORRECT_INPUT_MESSAGE, |
89
|
|
|
string $incorrectDataSetTypeMessage = self::DEFAULT_INCORRECT_DATA_SET_TYPE_MESSAGE, |
90
|
|
|
string|null $message = null, |
91
|
|
|
string $type = CompareType::NUMBER, |
92
|
|
|
bool|callable|null $skipOnEmpty = false, |
93
|
|
|
bool $skipOnError = false, |
94
|
|
|
Closure|null $when = null, |
95
|
|
|
) { |
96
|
|
|
parent::__construct( |
97
|
|
|
targetValue: $targetValue, |
98
|
|
|
targetAttribute: $targetAttribute, |
99
|
|
|
incorrectInputMessage: $incorrectInputMessage, |
100
|
|
|
incorrectDataSetTypeMessage: $incorrectDataSetTypeMessage, |
101
|
|
|
message: $message, |
102
|
|
|
type: $type, |
103
|
|
|
operator: '>=', |
104
|
|
|
skipOnEmpty: $skipOnEmpty, |
105
|
|
|
skipOnError: $skipOnError, |
106
|
|
|
when: $when, |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|