-
Notifications
You must be signed in to change notification settings - Fork 689
Expand file tree
/
Copy pathBaseTextConditionRule.php
More file actions
186 lines (167 loc) · 4.83 KB
/
BaseTextConditionRule.php
File metadata and controls
186 lines (167 loc) · 4.83 KB
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
179
180
181
182
183
184
185
186
<?php
namespace craft\base\conditions;
use craft\helpers\Cp;
use craft\helpers\Db;
use craft\helpers\Html;
use craft\helpers\StringHelper;
use yii\base\InvalidConfigException;
/**
* BaseTextConditionRule provides a base implementation for condition rules that are composed of an operator menu and text input.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 4.0.0
*/
abstract class BaseTextConditionRule extends BaseConditionRule
{
/**
* @inheritdoc
*/
public string $operator = self::OPERATOR_EQ;
/**
* @var string The input value.
*/
public string $value = '';
/**
* @inheritdoc
*/
protected bool $reloadOnOperatorChange = true;
/**
* @inheritdoc
*/
public function getConfig(): array
{
return array_merge(parent::getConfig(), [
'value' => $this->value,
]);
}
/**
* Returns the operators that should be allowed for this rule.
*
* @return array
*/
protected function operators(): array
{
return [
self::OPERATOR_EQ,
self::OPERATOR_NE,
self::OPERATOR_BEGINS_WITH,
self::OPERATOR_ENDS_WITH,
self::OPERATOR_CONTAINS,
self::OPERATOR_NOT_EMPTY,
self::OPERATOR_EMPTY,
];
}
/**
* Returns the input type that should be used.
*
* @return string
*/
protected function inputType(): string
{
return 'text';
}
/**
* @inheritdoc
*/
protected function inputHtml(): string
{
// don't show the value input if the condition checks for empty/notempty
if ($this->operator === self::OPERATOR_EMPTY || $this->operator === self::OPERATOR_NOT_EMPTY) {
return '';
}
return
Html::hiddenLabel(Html::encode($this->getLabel()), 'value') .
Cp::textHtml($this->inputOptions());
}
/**
* Returns the input options that should be used.
*
* @return array
* @since 4.3.0
*/
protected function inputOptions(): array
{
return [
'type' => $this->inputType(),
'id' => 'value',
'name' => 'value',
'value' => $this->value,
'autocomplete' => false,
'class' => 'flex-grow flex-shrink',
];
}
/**
* @inheritdoc
*/
protected function defineRules(): array
{
return array_merge(parent::defineRules(), [
[['value'], 'safe'],
]);
}
/**
* Returns the rule’s value, prepped for [[Db::parseParam()]] based on the selected operator.
*
* @return string|null
*/
protected function paramValue(): ?string
{
switch ($this->operator) {
case self::OPERATOR_EMPTY:
return ':empty:';
case self::OPERATOR_NOT_EMPTY:
return 'not :empty:';
}
if ($this->value === '') {
return null;
}
$value = Db::escapeParam($this->value);
return match ($this->operator) {
self::OPERATOR_BEGINS_WITH => "$value*",
self::OPERATOR_ENDS_WITH => "*$value",
self::OPERATOR_CONTAINS => "*$value*",
default => "$this->operator $value",
};
}
/**
* Returns whether the condition rule matches the given value.
*
* @param mixed $value
* @return bool
*/
protected function matchValue(mixed $value): bool
{
switch ($this->operator) {
case self::OPERATOR_EMPTY:
return $this->isEmpty($value);
case self::OPERATOR_NOT_EMPTY:
return !$this->isEmpty($value);
}
if ($this->value === '') {
return true;
}
return match ($this->operator) {
self::OPERATOR_EQ => $value == $this->value,
self::OPERATOR_NE => $value != $this->value,
self::OPERATOR_LT => $value < $this->value,
self::OPERATOR_LTE => $value <= $this->value,
self::OPERATOR_GT => $value > $this->value,
self::OPERATOR_GTE => $value >= $this->value,
self::OPERATOR_BEGINS_WITH => is_string($value) && StringHelper::startsWith($value, $this->value, false),
self::OPERATOR_ENDS_WITH => is_string($value) && StringHelper::endsWith($value, $this->value, false),
self::OPERATOR_CONTAINS => is_string($value) && StringHelper::contains($value, $this->value, false),
default => throw new InvalidConfigException("Invalid operator: $this->operator"),
};
}
/**
* Returns whether the given value should be considered empty.
*
* @param mixed $value
* @return bool
* @since 5.6.11
*/
protected function isEmpty(mixed $value): bool
{
return !$value;
}
}