1 | <?php |
||
2 | namespace Maknz\Slack\BlockElement; |
||
3 | |||
4 | use InvalidArgumentException; |
||
5 | use Maknz\Slack\CompositionObject\Option; |
||
6 | |||
7 | class MultiExternalSelect extends MultiDynamicSelect |
||
8 | { |
||
9 | /** |
||
10 | * Block type. |
||
11 | * |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $type = 'multi_external_select'; |
||
15 | |||
16 | /** |
||
17 | * Fewest number of characters before query is dispatched. |
||
18 | * |
||
19 | * @var int |
||
20 | */ |
||
21 | protected $min_query_length; |
||
22 | |||
23 | /** |
||
24 | * Initially selected options. |
||
25 | * |
||
26 | * @var \Maknz\Slack\CompositionObject\Option[] |
||
27 | */ |
||
28 | protected $initial_options = []; |
||
29 | |||
30 | /** |
||
31 | * Internal attribute to property map. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | protected static $availableAttributes = [ |
||
36 | 'placeholder' => 'placeholder', |
||
37 | 'action_id' => 'action_id', |
||
38 | 'min_query_length' => 'min_query_length', |
||
39 | 'initial_options' => 'initial_options', |
||
40 | 'confirm' => 'confirm', |
||
41 | 'max_selected_items' => 'max_selected_items', |
||
42 | ]; |
||
43 | |||
44 | /** |
||
45 | * Get the number of characters before query is dispatched. |
||
46 | * |
||
47 | * @return int |
||
48 | */ |
||
49 | 3 | public function getMinQueryLength() |
|
50 | { |
||
51 | 3 | return $this->min_query_length; |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * Set the number of characters before query is dispatched. |
||
56 | * |
||
57 | * @param int $minQueryLength |
||
58 | * |
||
59 | * @return $this |
||
60 | * |
||
61 | * @throws InvalidArgumentException |
||
62 | */ |
||
63 | 2 | public function setMinQueryLength($minQueryLength) |
|
64 | { |
||
65 | 2 | if (is_int($minQueryLength)) { |
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
66 | 1 | $this->min_query_length = $minQueryLength; |
|
67 | |||
68 | 1 | return $this; |
|
69 | } |
||
70 | |||
71 | 1 | throw new InvalidArgumentException('The minimum query length must be an integer'); |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * Get the initially selected options. |
||
76 | * |
||
77 | * @return \Maknz\Slack\CompositionObject\Option[] |
||
78 | */ |
||
79 | 6 | public function getInitialOptions() |
|
80 | { |
||
81 | 6 | return $this->initial_options; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * Set the initially selected options. |
||
86 | * |
||
87 | * @param array $initialOption |
||
88 | * |
||
89 | * @return $this |
||
90 | * |
||
91 | * @throws InvalidArgumentException |
||
92 | */ |
||
93 | 5 | public function setInitialOptions(array $initialOptions) |
|
94 | { |
||
95 | 5 | $this->clearInitialOptions(); |
|
96 | |||
97 | 5 | foreach ($initialOptions as $initialOption) { |
|
98 | 5 | $this->addInitialOption($initialOption); |
|
99 | } |
||
100 | |||
101 | 4 | return $this; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Clear the initially selected options. |
||
106 | * |
||
107 | * @return $this |
||
108 | */ |
||
109 | 5 | public function clearInitialOptions() |
|
110 | { |
||
111 | 5 | $this->initial_options = []; |
|
112 | |||
113 | 5 | return $this; |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * Add an initially selected option. |
||
118 | * |
||
119 | * @param mixed $initialOption |
||
120 | * |
||
121 | * @return $this |
||
122 | * |
||
123 | * @throws InvalidArgumentException |
||
124 | */ |
||
125 | 5 | public function addInitialOption($initialOption) |
|
126 | { |
||
127 | 5 | if (is_array($initialOption)) { |
|
128 | 4 | $initialOption = new Option($initialOption); |
|
129 | } |
||
130 | |||
131 | 5 | if ($initialOption instanceof Option) { |
|
132 | 4 | $this->initial_options[] = $initialOption; |
|
133 | |||
134 | 4 | return $this; |
|
135 | } |
||
136 | |||
137 | 1 | throw new InvalidArgumentException('The initial option must be an instance of '.Option::class.' or a keyed array'); |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * Convert the block to its array representation. |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | 3 | public function toArray() |
|
146 | { |
||
147 | 3 | $data = [ |
|
148 | 3 | 'type' => $this->getType(), |
|
149 | 3 | 'placeholder' => $this->getPlaceholder()->toArray(), |
|
150 | 3 | 'action_id' => $this->getActionId(), |
|
151 | ]; |
||
152 | |||
153 | 3 | if ($this->getMinQueryLength()) { |
|
154 | 1 | $data['min_query_length'] = $this->getMinQueryLength(); |
|
155 | } |
||
156 | |||
157 | 3 | $initialOptions = $this->getInitialOptions(); |
|
158 | |||
159 | 3 | if (count($initialOptions)) { |
|
160 | 2 | $data['initial_options'] = array_map(function (Option $o) { |
|
161 | 2 | return $o->toArray(); |
|
162 | 2 | }, $initialOptions); |
|
163 | } |
||
164 | |||
165 | 3 | if ($this->getConfirm()) { |
|
166 | 1 | $data['confirm'] = $this->getConfirm()->toArray(); |
|
167 | } |
||
168 | |||
169 | 3 | if ($this->getMaxSelectedItems()) { |
|
170 | 1 | $data['max_selected_items'] = $this->getMaxSelectedItems(); |
|
171 | } |
||
172 | |||
173 | 3 | return $data; |
|
174 | } |
||
175 | } |
||
176 |