ChoiceControl.php
1.74 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
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Controls;
use YahnisElsts\AdminMenuEditor\Customizable\Settings\EnumSetting;
use YahnisElsts\AdminMenuEditor\Customizable\Settings\Setting;
abstract class ChoiceControl extends ClassicControl {
protected $type = 'choice';
/**
* @var Setting
*/
protected $mainSetting;
/**
* @var ChoiceControlOption[]
*/
protected $options = [];
public function __construct($settings = [], $params = []) {
parent::__construct($settings, $params);
if ( isset($params['choices']) ) {
if ( is_callable($params['choices']) ) {
$choices = call_user_func($params['choices']);
} else {
$choices = $params['choices'];
}
foreach ($choices as $key => $item) {
if ( is_string($item) ) {
//List of [value => label] pairs.
$this->options[] = new ChoiceControlOption($key, $item);
} else if ( is_array($item) ) {
//List of arrays where each item is [value => X, label => Y, ...].
//Alternatively, [value => [label => Y, ...]] pairs.
if ( !array_key_exists('value', $item) ) {
$item['value'] = $key;
}
$this->options[$key] = ChoiceControlOption::fromArray($item);
} else if ( $item instanceof ChoiceControlOption ) {
//List of nicely predefined option objects.
$this->options[] = $item;
} else {
throw new \InvalidArgumentException("Invalid option: $item");
}
}
} else if ( $this->mainSetting instanceof EnumSetting ) {
$this->options = $this->mainSetting->generateChoiceOptions();
}
}
protected function getKoComponentParams() {
$params = parent::getKoComponentParams();
$params['options'] = array_map(
function ($option) {
return $option->serializeForJs();
},
$this->options
);
return $params;
}
}