1
|
|
|
<?php |
2
|
|
|
namespace Maknz\Slack\BlockElement; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
use Maknz\Slack\Object\OptionGroup; |
6
|
|
|
|
7
|
|
|
class Select extends AbstractSelect |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Block type. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $type = 'static_select'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Whether one of the options is initially selected. |
18
|
|
|
* |
19
|
|
|
* @var bool |
20
|
|
|
*/ |
21
|
|
|
protected $hasInitialOption = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Internal attribute to property map. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected static $availableAttributes = [ |
29
|
|
|
'placeholder' => 'placeholder', |
30
|
|
|
'action_id' => 'action_id', |
31
|
|
|
'options' => 'options', |
32
|
|
|
'option_groups' => 'option_groups', |
33
|
|
|
'confirm' => 'confirm', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Add an option to the select. |
38
|
|
|
* |
39
|
|
|
* @param mixed $option |
40
|
|
|
* |
41
|
|
|
* @return Select |
42
|
|
|
* |
43
|
|
|
* @throws \InvalidArgumentException |
44
|
|
|
*/ |
45
|
6 |
|
public function addOption($option) |
46
|
|
|
{ |
47
|
6 |
|
if ((is_array($option) && ! empty($option['selected'])) |
48
|
6 |
|
|| ($option instanceof Option && ! $option->isInitiallySelected()) |
49
|
|
|
) { |
50
|
3 |
|
if ($this->hasInitialOption) { |
51
|
1 |
|
throw new InvalidArgumentException('Only one option can be initially selected'); |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
$this->hasInitialOption = true; |
55
|
|
|
} |
56
|
|
|
|
57
|
6 |
|
return parent::addOption($option); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Clear option groups in the select. |
62
|
|
|
* |
63
|
|
|
* @return Select |
64
|
|
|
*/ |
65
|
9 |
|
public function clearOptionGroups() |
66
|
|
|
{ |
67
|
9 |
|
if (count($this->getOptions()) == 0) { |
68
|
8 |
|
$this->hasSelectedOption = false; |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
9 |
|
return parent::clearOptionGroups(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Clear options in the select. |
76
|
|
|
* |
77
|
|
|
* @return Select |
78
|
|
|
*/ |
79
|
9 |
|
public function clearOptions() |
80
|
|
|
{ |
81
|
9 |
|
if (count($this->getOptionGroups()) == 0) { |
82
|
9 |
|
$this->hasSelectedOption = false; |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
9 |
|
return parent::clearOptions(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Add an option group to the select. |
90
|
|
|
* |
91
|
|
|
* @param mixed $group |
92
|
|
|
* |
93
|
|
|
* @return AbstractSelect |
94
|
|
|
* |
95
|
|
|
* @throws \InvalidArgumentException |
96
|
|
|
*/ |
97
|
4 |
|
public function addOptionGroup($group) |
98
|
|
|
{ |
99
|
4 |
|
if (is_array($group)) { |
100
|
4 |
|
$group = new OptionGroup($group); |
101
|
|
|
} |
102
|
|
|
|
103
|
4 |
|
parent::addOptionGroup($group); |
104
|
|
|
|
105
|
4 |
|
if ($group->hasSelectedOption()) { |
106
|
3 |
|
if ($this->hasInitialOption) { |
107
|
1 |
|
throw new InvalidArgumentException('Only one option can be initially selected'); |
108
|
|
|
} |
109
|
|
|
|
110
|
3 |
|
$this->hasInitialOption = true; |
111
|
|
|
} |
112
|
|
|
|
113
|
4 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get the intially selected option. |
118
|
|
|
* |
119
|
|
|
* @return Maknz\Slack\Object\Option |
|
|
|
|
120
|
|
|
*/ |
121
|
2 |
|
public function getInitialOption() |
122
|
|
|
{ |
123
|
2 |
|
foreach ($this->getOptions() as $option) { |
124
|
1 |
|
if ($option->isInitiallySelected()) { |
125
|
1 |
|
return $option; |
|
|
|
|
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
foreach ($this->getOptionGroups() as $group) { |
130
|
1 |
|
foreach ($group->getOptions() as $option) { |
131
|
1 |
|
if ($option->isInitiallySelected()) { |
132
|
1 |
|
return $option; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Convert the block to its array representation. |
140
|
|
|
* |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
3 |
|
public function toArray() |
144
|
|
|
{ |
145
|
|
|
$data = [ |
146
|
3 |
|
'type' => $this->getType(), |
147
|
3 |
|
'placeholder' => $this->getPlaceholder()->toArray(), |
148
|
3 |
|
'action_id' => $this->getActionId(), |
149
|
|
|
]; |
150
|
|
|
|
151
|
3 |
|
if (count($this->getOptions())) { |
152
|
2 |
|
$data['options'] = $this->getOptionsAsArrays(); |
153
|
|
|
} else { |
154
|
1 |
|
$data['option_groups'] = $this->getOptionGroupsAsArrays(); |
155
|
|
|
} |
156
|
|
|
|
157
|
3 |
|
if ($this->hasInitialOption) { |
158
|
2 |
|
$data['initial_option'] = $this->getInitialOption()->toArray(); |
159
|
|
|
} |
160
|
|
|
|
161
|
3 |
|
if ($this->getConfirm()) { |
162
|
2 |
|
$data['confirm'] = $this->getConfirm()->toArray(); |
163
|
|
|
} |
164
|
|
|
|
165
|
3 |
|
return $data; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|