-
Notifications
You must be signed in to change notification settings - Fork 611
/
BaseMultiSelectConditionRule.php
165 lines (147 loc) · 4.08 KB
/
BaseMultiSelectConditionRule.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
<?php
namespace craft\base\conditions;
use craft\helpers\ArrayHelper;
use craft\helpers\Cp;
use craft\helpers\Db;
use craft\helpers\Html;
use yii\base\InvalidConfigException;
/**
* BaseMultiSelectConditionRule provides a base implementation for condition rules that are composed of a multi-select input.
*
* @property string[] $values
* @since 4.0.0
*/
abstract class BaseMultiSelectConditionRule extends BaseConditionRule
{
/**
* @inheritdoc
*/
public string $operator = self::OPERATOR_IN;
/**
* @var string[]
*/
private array $_values = [];
/**
* Returns the operators that should be allowed for this rule.
*
* @return array
*/
protected function operators(): array
{
return [
self::OPERATOR_IN,
self::OPERATOR_NOT_IN,
];
}
/**
* @return string[]
*/
public function getValues(): array
{
return $this->_values;
}
/**
* @param string|string[] $values
*/
public function setValues(array|string $values): void
{
if ($values === '') {
$this->_values = [];
} else {
$this->_values = ArrayHelper::toArray($values);
}
}
/**
* @inheritdoc
*/
public function getConfig(): array
{
return array_merge(parent::getConfig(), [
'values' => $this->_values,
]);
}
/**
* Defines the selectable options.
*
* Options can be expressed as value/label pairs, or as arrays with `value` and `label` keys.
*
* @return string[]|array[]
* @phpstan-return string[]|array{value:string,label:string}[]
*/
abstract protected function options(): array;
/**
* @inheritdoc
*/
protected function inputHtml(): string
{
$multiSelectId = 'multiselect';
return
Html::hiddenLabel(Html::encode($this->getLabel()), $multiSelectId) .
Cp::selectizeHtml([
'id' => $multiSelectId,
'class' => 'flex-grow',
'name' => 'values',
'values' => $this->_values,
'options' => $this->options(),
'multi' => true,
]);
}
/**
* @inheritdoc
*/
protected function defineRules(): array
{
return array_merge(parent::defineRules(), [
[['values'], 'safe'],
]);
}
/**
* Returns the rule’s value, prepped for [[Db::parseParam()]] based on the selected operator.
*
* @param callable|null $normalizeValue Method for normalizing a given selected value.
* @return array|null
*/
protected function paramValue(?callable $normalizeValue = null): ?array
{
$values = [];
foreach ($this->_values as $value) {
if ($normalizeValue !== null) {
$value = $normalizeValue($value);
if ($value === null) {
continue;
}
}
$values[] = Db::escapeParam($value);
}
if (!$values) {
return null;
}
return match ($this->operator) {
self::OPERATOR_IN => $values,
self::OPERATOR_NOT_IN => array_merge(['not'], $values),
default => throw new InvalidConfigException("Invalid operator: $this->operator"),
};
}
/**
* Returns whether the condition rule matches the given value.
*
* @param string|string[]|null $value
* @return bool
*/
protected function matchValue(array|string|null $value): bool
{
if (!$this->_values) {
return true;
}
if ($value === '' || $value === null) {
$value = [];
} else {
$value = (array)$value;
}
return match ($this->operator) {
self::OPERATOR_IN => !empty(array_intersect($value, $this->_values)),
self::OPERATOR_NOT_IN => empty(array_intersect($value, $this->_values)),
default => throw new InvalidConfigException("Invalid operator: $this->operator"),
};
}
}