-
Notifications
You must be signed in to change notification settings - Fork 638
/
Copy pathOptionsFieldConditionRule.php
57 lines (50 loc) · 1.54 KB
/
OptionsFieldConditionRule.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
<?php
namespace craft\fields\conditions;
use craft\base\conditions\BaseMultiSelectConditionRule;
use craft\fields\BaseOptionsField;
use craft\fields\data\MultiOptionsFieldData;
use craft\fields\data\OptionData;
use craft\fields\data\SingleOptionFieldData;
use Illuminate\Support\Collection;
/**
* Options field condition rule.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 4.0.0
*/
class OptionsFieldConditionRule extends BaseMultiSelectConditionRule implements FieldConditionRuleInterface
{
use FieldConditionRuleTrait;
protected function options(): array
{
/** @var BaseOptionsField $field */
$field = $this->_field;
return Collection::make($field->options)
->filter(fn(array $option) => !empty($option['value']) && !empty($option['label']))
->map(fn(array $option) => [
'value' => $option['value'],
'label' => $option['label'],
])
->all();
}
/**
* @inheritdoc
*/
protected function elementQueryParam(): array
{
return $this->paramValue();
}
/**
* @inheritdoc
*/
protected function matchFieldValue($value): bool
{
if ($value instanceof MultiOptionsFieldData) {
/** @phpstan-ignore-next-line */
$value = array_map(fn(OptionData $option) => $option->value, (array)$value);
} elseif ($value instanceof SingleOptionFieldData) {
$value = $value->value;
}
return $this->matchValue($value);
}
}