1 | <?php |
||
2 | namespace Maknz\Slack\BlockElement; |
||
3 | |||
4 | use InvalidArgumentException; |
||
5 | use Maknz\Slack\FilterTrait; |
||
6 | |||
7 | class MultiConversationsSelect extends MultiDynamicSelect |
||
8 | { |
||
9 | use FilterTrait; |
||
10 | |||
11 | /** |
||
12 | * Block type. |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $type = 'multi_conversations_select'; |
||
17 | |||
18 | /** |
||
19 | * Initially selected conversations. |
||
20 | * |
||
21 | * @var string[] |
||
22 | */ |
||
23 | protected $initial_conversations = []; |
||
24 | |||
25 | /** |
||
26 | * Whether to default to the current conversation. |
||
27 | * |
||
28 | * @var bool |
||
29 | */ |
||
30 | protected $default_to_current_conversation = false; |
||
31 | |||
32 | /** |
||
33 | * Internal attribute to property map. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected static $availableAttributes = [ |
||
38 | 'placeholder' => 'placeholder', |
||
39 | 'action_id' => 'action_id', |
||
40 | 'initial_conversations' => 'initial_conversations', |
||
41 | 'default_to_current_conversation' => 'default_to_current_conversation', |
||
42 | 'confirm' => 'confirm', |
||
43 | 'max_selected_items' => 'max_selected_items', |
||
44 | 'filter' => 'filter', |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * Get the initially selected conversations. |
||
49 | * |
||
50 | * @return string[] |
||
51 | */ |
||
52 | 6 | public function getInitialConversations() |
|
53 | { |
||
54 | 6 | return $this->initial_conversations; |
|
55 | } |
||
56 | |||
57 | /** |
||
58 | * Set the initially selected conversations. |
||
59 | * |
||
60 | * @param string[] $initialConversations |
||
61 | * |
||
62 | * @return $this |
||
63 | * |
||
64 | * @throws InvalidArgumentException |
||
65 | */ |
||
66 | 5 | public function setInitialConversations(array $initialConversations) |
|
67 | { |
||
68 | 5 | $this->clearInitialConversations(); |
|
69 | |||
70 | 5 | foreach ($initialConversations as $initialConversation) { |
|
71 | 5 | $this->addInitialConversation($initialConversation); |
|
72 | } |
||
73 | |||
74 | 4 | return $this; |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * Clear the initially selected conversations. |
||
79 | * |
||
80 | * @return $this |
||
81 | */ |
||
82 | 5 | public function clearInitialConversations() |
|
83 | { |
||
84 | 5 | $this->initial_conversations = []; |
|
85 | |||
86 | 5 | return $this; |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * Add an initially selected conversation. |
||
91 | * |
||
92 | * @param string $initialConversation |
||
93 | * |
||
94 | * @return $this |
||
95 | * |
||
96 | * @throws InvalidArgumentException |
||
97 | */ |
||
98 | 5 | public function addInitialConversation($initialConversation) |
|
99 | { |
||
100 | 5 | if (is_string($initialConversation)) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
101 | 4 | $this->initial_conversations[] = $initialConversation; |
|
102 | |||
103 | 4 | return $this; |
|
104 | } |
||
105 | |||
106 | 1 | throw new InvalidArgumentException('The initial conversation ID must be a string'); |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * Get whether to default to the current conversation. |
||
111 | * |
||
112 | * @return bool |
||
113 | */ |
||
114 | 3 | public function getDefaultToCurrentConversation() |
|
115 | { |
||
116 | 3 | return $this->default_to_current_conversation; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Set whether to default to the current conversation. |
||
121 | * |
||
122 | * @param bool $defaultToCurrentConversation |
||
123 | * |
||
124 | * @return $this |
||
125 | */ |
||
126 | 1 | public function setDefaultToCurrentConversation($defaultToCurrentConversation) |
|
127 | { |
||
128 | 1 | $this->default_to_current_conversation = (bool)$defaultToCurrentConversation; |
|
129 | |||
130 | 1 | return $this; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Convert the block to its array representation. |
||
135 | * |
||
136 | * @return array |
||
137 | */ |
||
138 | 3 | public function toArray() |
|
139 | { |
||
140 | 3 | $data = [ |
|
141 | 3 | 'type' => $this->getType(), |
|
142 | 3 | 'placeholder' => $this->getPlaceholder()->toArray(), |
|
143 | 3 | 'action_id' => $this->getActionId(), |
|
144 | ]; |
||
145 | |||
146 | 3 | $initialConversations = $this->getInitialConversations(); |
|
147 | |||
148 | 3 | if ($this->getDefaultToCurrentConversation()) { |
|
149 | 1 | $data['default_to_current_conversation'] = true; |
|
150 | 3 | } elseif (count($initialConversations)) { |
|
151 | 2 | $data['initial_conversations'] = $initialConversations; |
|
152 | } |
||
153 | |||
154 | 3 | if ($this->getConfirm()) { |
|
155 | 1 | $data['confirm'] = $this->getConfirm()->toArray(); |
|
156 | } |
||
157 | |||
158 | 3 | if ($this->getMaxSelectedItems()) { |
|
159 | 1 | $data['max_selected_items'] = $this->getMaxSelectedItems(); |
|
160 | } |
||
161 | |||
162 | 3 | if ($this->getFilter()) { |
|
163 | 1 | $data['filter'] = $this->getFilter()->toArray(); |
|
164 | } |
||
165 | |||
166 | 3 | return $data; |
|
167 | } |
||
168 | } |
||
169 |