/
BaseNumberConditionRule.php
178 lines (155 loc) · 4.34 KB
/
BaseNumberConditionRule.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
170
171
172
173
174
175
176
177
178
<?php
namespace craft\base\conditions;
use Craft;
use craft\helpers\Cp;
use craft\helpers\Db;
use craft\helpers\Html;
/**
* BaseNumberConditionRule provides a base implementation for condition rules that are composed of a number input.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 4.0.0
*/
abstract class BaseNumberConditionRule extends BaseTextConditionRule
{
/**
* @since 4.3.0
*/
protected const OPERATOR_BETWEEN = 'between';
/**
* @var string
* @since 4.3.0
*/
public string $maxValue = '';
/**
* @inheritdoc
*/
public function getConfig(): array
{
return array_merge(parent::getConfig(), [
'maxValue' => $this->maxValue,
]);
}
/**
* @inheritdoc
*/
protected function operators(): array
{
return [
self::OPERATOR_EQ,
self::OPERATOR_NE,
self::OPERATOR_LT,
self::OPERATOR_LTE,
self::OPERATOR_GT,
self::OPERATOR_GTE,
self::OPERATOR_BETWEEN,
self::OPERATOR_NOT_EMPTY,
self::OPERATOR_EMPTY,
];
}
/**
* @inerhitdoc
*/
protected function operatorLabel(string $operator): string
{
if ($operator === self::OPERATOR_BETWEEN) {
return Craft::t('app', 'is between…');
}
return parent::operatorLabel($operator);
}
/**
* @inheritdoc
*/
protected function inputType(): string
{
return 'number';
}
/**
* @inheritdoc
*/
protected function defineRules(): array
{
return array_merge(parent::defineRules(), [
[['maxValue'], 'safe'],
]);
}
/**
* @inheritdoc
*/
protected function inputHtml(): string
{
if ($this->operator === self::OPERATOR_BETWEEN) {
return Html::tag('div',
Html::hiddenLabel(Craft::t('app', 'Min Value'), 'min') .
Cp::textHtml([
'type' => $this->inputType(),
'id' => 'min',
'name' => 'value',
'value' => $this->value,
'autocomplete' => false,
'class' => 'flex-grow flex-shrink',
]) .
Html::tag('span', Craft::t('app', 'and')) .
Html::hiddenLabel(Craft::t('app', 'Max Value'), 'max') .
Cp::textHtml([
'type' => $this->inputType(),
'id' => 'max',
'name' => 'maxValue',
'value' => $this->maxValue,
'autocomplete' => false,
'class' => 'flex-grow flex-shrink',
]) .
Html::tag('span', Craft::t('app', 'The values are matched inclusively.'), ['class' => 'info']),
['class' => 'flex flex-center']
);
}
return parent::inputHtml();
}
/**
* @inheritdoc
*/
protected function paramValue(): ?string
{
if ($this->operator === self::OPERATOR_BETWEEN) {
if (empty($this->value) && empty($this->maxValue)) {
return null;
}
if (empty($this->maxValue)) {
return '>= ' . Db::escapeParam($this->value);
}
if (empty($this->value)) {
return '<= ' . Db::escapeParam($this->maxValue);
}
return sprintf('and, >= %s, <= %s', Db::escapeParam($this->value), Db::escapeParam($this->maxValue));
}
return parent::paramValue();
}
/**
* @inheritdoc
*/
protected function matchValue(mixed $value): bool
{
if ($this->operator === self::OPERATOR_BETWEEN) {
if (empty($this->value) && empty($this->maxValue)) {
return true;
}
if (!empty($this->value) && $value < $this->value) {
return false;
}
if (!empty($this->maxValue) && $value > $this->maxValue) {
return false;
}
return true;
}
return parent::matchValue($value);
}
/**
* @inheritdoc
*/
protected function inputOptions(): array
{
return array_merge(parent::inputOptions(), [
'step' => '1',
]);
}
}