1 | <?php |
||
2 | namespace Maknz\Slack\BlockElement; |
||
3 | |||
4 | use InvalidArgumentException; |
||
5 | use Maknz\Slack\CompositionObject\Option; |
||
6 | |||
7 | class ExternalSelect extends AbstractDynamicSelect |
||
8 | { |
||
9 | /** |
||
10 | * Block type. |
||
11 | * |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $type = '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 option. |
||
25 | * |
||
26 | * @var \Maknz\Slack\CompositionObject\Option |
||
27 | */ |
||
28 | protected $initial_option; |
||
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 | 'initial_option' => 'initial_option', |
||
39 | 'min_query_length' => 'min_query_length', |
||
40 | 'confirm' => 'confirm', |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * Get the number of characters before query is dispatched. |
||
45 | * |
||
46 | * @return int |
||
47 | */ |
||
48 | 2 | public function getMinQueryLength() |
|
49 | { |
||
50 | 2 | return $this->min_query_length; |
|
51 | } |
||
52 | |||
53 | /** |
||
54 | * Set the number of characters before query is dispatched. |
||
55 | * |
||
56 | * @param int $minQueryLength |
||
57 | * |
||
58 | * @return $this |
||
59 | * |
||
60 | * @throws InvalidArgumentException |
||
61 | */ |
||
62 | 2 | public function setMinQueryLength($minQueryLength) |
|
63 | { |
||
64 | 2 | if (is_int($minQueryLength)) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
65 | 1 | $this->min_query_length = $minQueryLength; |
|
66 | |||
67 | 1 | return $this; |
|
68 | } |
||
69 | |||
70 | 1 | throw new InvalidArgumentException('The minimum query length must be an integer'); |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * Get the initially selected option. |
||
75 | * |
||
76 | * @return \Maknz\Slack\CompositionObject\Option |
||
77 | */ |
||
78 | 3 | public function getInitialOption() |
|
79 | { |
||
80 | 3 | return $this->initial_option; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * Set the initially selected option. |
||
85 | * |
||
86 | * @param mixed $initialOption |
||
87 | * |
||
88 | * @return $this |
||
89 | * |
||
90 | * @throws InvalidArgumentException |
||
91 | */ |
||
92 | 3 | public function setInitialOption($initialOption) |
|
93 | { |
||
94 | 3 | if (is_array($initialOption)) { |
|
95 | 2 | $initialOption = new Option($initialOption); |
|
96 | } |
||
97 | |||
98 | 3 | if ($initialOption instanceof Option) { |
|
99 | 2 | $this->initial_option = $initialOption; |
|
100 | |||
101 | 2 | return $this; |
|
102 | } |
||
103 | |||
104 | 1 | throw new InvalidArgumentException('The initial option must be an instance of '.Option::class.' or a keyed array'); |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Clear the initially selected option. |
||
109 | * |
||
110 | * @return $this |
||
111 | */ |
||
112 | 1 | public function clearInitialOption() |
|
113 | { |
||
114 | 1 | $this->initial_option = null; |
|
115 | |||
116 | 1 | return $this; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Convert the block to its array representation. |
||
121 | * |
||
122 | * @return array |
||
123 | */ |
||
124 | 2 | public function toArray() |
|
125 | { |
||
126 | 2 | $data = [ |
|
127 | 2 | 'type' => $this->getType(), |
|
128 | 2 | 'placeholder' => $this->getPlaceholder()->toArray(), |
|
129 | 2 | 'action_id' => $this->getActionId(), |
|
130 | ]; |
||
131 | |||
132 | 2 | if ($this->getInitialOption()) { |
|
133 | 2 | $data['initial_option'] = $this->getInitialOption()->toArray(); |
|
134 | } |
||
135 | |||
136 | 2 | if ($this->getMinQueryLength()) { |
|
137 | 1 | $data['min_query_length'] = $this->getMinQueryLength(); |
|
138 | } |
||
139 | |||
140 | 2 | if ($this->getConfirm()) { |
|
141 | 1 | $data['confirm'] = $this->getConfirm()->toArray(); |
|
142 | } |
||
143 | |||
144 | 2 | return $data; |
|
145 | } |
||
146 | } |
||
147 |