/
DateCompareValidator.php
169 lines (150 loc) · 6.53 KB
/
DateCompareValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\validators;
use Craft;
use craft\i18n\Formatter;
use DateTime;
use yii\base\InvalidConfigException;
use yii\base\InvalidValueException;
use yii\validators\Validator;
/**
* Class DateCompareValidator.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.7.21
*/
class DateCompareValidator extends Validator
{
/**
* @var string|null the name of the attribute to be compared with. When both this property
* and [[compareValue]] are set, the latter takes precedence.
* @see compareValue
*/
public ?string $compareAttribute = null;
/**
* @var DateTime|callable|null the constant value to be compared with. When both this property
* and [[compareAttribute]] are set, this property takes precedence.
* @see compareAttribute
*/
public $compareValue;
/**
* @var string the operator for comparison. The following operators are supported:
*
* - `==`: check if two values are equal. The comparison is done is non-strict mode.
* - `!=`: check if two values are NOT equal. The comparison is done is non-strict mode.
* - `>`: check if value being validated is greater than the value being compared with.
* - `>=`: check if value being validated is greater than or equal to the value being compared with.
* - `<`: check if value being validated is less than the value being compared with.
* - `<=`: check if value being validated is less than or equal to the value being compared with.
*/
public string $operator = '==';
/**
* @var string|null the user-defined error message. It may contain the following placeholders which
* will be replaced accordingly by the validator:
*
* - `{attribute}`: the label of the attribute being validated
* - `{value}`: the value of the attribute being validated
* - `{compareValue}`: the value or the attribute label to be compared with
* - `{compareAttribute}`: the label of the attribute to be compared with
* - `{compareValueOrAttribute}`: the value or the attribute label to be compared with
*/
public $message;
/**
* @inheritdoc
*/
public function init(): void
{
parent::init();
if (!isset($this->message)) {
$this->message = match ($this->operator) {
'==' => Craft::t('yii', '{attribute} must be equal to "{compareValueOrAttribute}".'),
'!=' => Craft::t('yii', '{attribute} must not be equal to "{compareValueOrAttribute}".'),
'>' => Craft::t('yii', '{attribute} must be greater than "{compareValueOrAttribute}".'),
'>=' => Craft::t('yii', '{attribute} must be greater than or equal to "{compareValueOrAttribute}".'),
'<' => Craft::t('yii', '{attribute} must be less than "{compareValueOrAttribute}".'),
'<=' => Craft::t('yii', '{attribute} must be less than or equal to "{compareValueOrAttribute}".'),
default => throw new InvalidConfigException("Unknown operator: $this->operator"),
};
}
}
/**
* @inheritdoc
*/
public function validateAttribute($model, $attribute): void
{
if (!isset($this->compareValue) && !isset($this->compareAttribute)) {
throw new InvalidConfigException('DateCompareValidator::compareValue or compareAttribute must be set.');
}
$value = $model->$attribute;
if (isset($this->compareValue)) {
if (is_callable($this->compareValue)) {
$this->compareValue = call_user_func($this->compareValue);
}
$compareValue = $this->compareValue;
} else {
$compareValue = $model->{$this->compareAttribute};
$compareLabel = $compareValueOrAttribute = $model->getAttributeLabel($this->compareAttribute);
}
if (!$value instanceof DateTime || !$compareValue instanceof DateTime) {
throw new InvalidValueException('DateCompareValidator expects both values to be DateTime objects.');
}
if (!$this->compareValues($this->operator, $value, $compareValue)) {
$formattedCompareValue = Craft::$app->getFormatter()->asDatetime($compareValue, Formatter::FORMAT_WIDTH_SHORT);
$this->addError($model, $attribute, $this->message, [
'compareAttribute' => $compareLabel ?? $formattedCompareValue,
'compareValue' => $formattedCompareValue,
'compareValueOrAttribute' => $compareValueOrAttribute ?? $formattedCompareValue,
]);
}
}
/**
* @inheritdoc
*/
protected function validateValue($value): ?array
{
if (!isset($this->compareValue)) {
throw new InvalidConfigException('CompareValidator::compareValue must be set.');
}
if (is_callable($this->compareValue)) {
$this->compareValue = call_user_func($this->compareValue);
}
if (!$value instanceof DateTime || !$this->compareValue instanceof DateTime) {
throw new InvalidValueException('DateCompareValidator expects both values to be DateTime objects.');
}
if (!$this->compareValues($this->operator, $value, $this->compareValue)) {
$formattedCompareValue = Craft::$app->getFormatter()->asDatetime($this->compareValue, Formatter::FORMAT_WIDTH_SHORT);
return [
$this->message, [
'compareAttribute' => $formattedCompareValue,
'compareValue' => $formattedCompareValue,
'compareValueOrAttribute' => $formattedCompareValue,
],
];
}
return null;
}
/**
* Compares two values with the specified operator.
*
* @param string $operator the comparison operator
* @param DateTime $value the value being compared
* @param DateTime $compareValue another value being compared
* @return bool whether the comparison using the specified operator is true.
*/
protected function compareValues(string $operator, DateTime $value, DateTime $compareValue): bool
{
return match ($operator) {
'==' => $value == $compareValue,
'!=' => $value != $compareValue,
'>' => $value > $compareValue,
'>=' => $value >= $compareValue,
'<' => $value < $compareValue,
'<=' => $value <= $compareValue,
default => false,
};
}
}