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