1
|
|
|
<?php |
2
|
|
|
namespace Maknz\Slack\BlockElement; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
use Maknz\Slack\CompositionObject\Option; |
6
|
|
|
|
7
|
|
|
class RadioButtons extends Options |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Block type. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $type = 'radio_buttons'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Whether one of the radio buttons 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
|
|
|
'action_id' => 'action_id', |
30
|
|
|
'options' => 'options', |
31
|
|
|
'confirm' => 'confirm', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get the intially selected option. |
36
|
|
|
* |
37
|
|
|
* @return \Maknz\Slack\CompositionObject\Option|null |
38
|
|
|
*/ |
39
|
3 |
|
public function getInitialOption() |
40
|
|
|
{ |
41
|
3 |
|
foreach ($this->getOptions() as $option) { |
42
|
2 |
|
if ($option->isInitiallySelected()) { |
43
|
2 |
|
return $option; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
return null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Add an option to the radio buttons. |
52
|
|
|
* |
53
|
|
|
* @param mixed $option |
54
|
|
|
* |
55
|
|
|
* @return $this |
56
|
|
|
* |
57
|
|
|
* @throws \InvalidArgumentException |
58
|
|
|
*/ |
59
|
6 |
|
public function addOption($option) |
60
|
|
|
{ |
61
|
6 |
|
if ((is_array($option) && ! empty($option['selected'])) |
62
|
6 |
|
|| ($option instanceof Option && ! $option->isInitiallySelected()) |
63
|
|
|
) { |
64
|
5 |
|
if ($this->hasInitialOption) { |
65
|
2 |
|
throw new InvalidArgumentException('Only one option can be initially selected'); |
66
|
|
|
} |
67
|
|
|
|
68
|
5 |
|
$this->hasInitialOption = true; |
69
|
|
|
} |
70
|
|
|
|
71
|
6 |
|
return parent::addOption($option); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Clear options available. |
76
|
|
|
* |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
5 |
|
public function clearOptions() |
80
|
|
|
{ |
81
|
5 |
|
$this->hasInitialOption = false; |
82
|
|
|
|
83
|
5 |
|
return parent::clearOptions(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Convert the block to its array representation. |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
1 |
|
public function toArray() |
92
|
|
|
{ |
93
|
1 |
|
$data = [ |
94
|
1 |
|
'type' => $this->getType(), |
95
|
1 |
|
'action_id' => $this->getActionId(), |
96
|
1 |
|
'options' => $this->getOptionsAsArrays(), |
97
|
|
|
]; |
98
|
|
|
|
99
|
1 |
|
if ($this->hasInitialOption) { |
100
|
1 |
|
$initialOption = $this->getInitialOption(); |
101
|
1 |
|
if ($initialOption !== null) { |
102
|
1 |
|
$data['initial_option'] = $initialOption->toArray(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
$confirm = $this->getConfirm(); |
107
|
1 |
|
if ($confirm !== null) { |
108
|
1 |
|
$data['confirm'] = $confirm->toArray(); |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
return $data; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|