1
|
|
|
<?php |
2
|
|
|
namespace Maknz\Slack\Block; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
use Maknz\Slack\Block; |
6
|
|
|
use Maknz\Slack\BlockElement; |
7
|
|
|
use Maknz\Slack\BlockElement\Text; |
8
|
|
|
|
9
|
|
|
class Input extends Block |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Block type. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $type = 'input'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Label that appears above the input. |
20
|
|
|
* |
21
|
|
|
* @var \Maknz\Slack\BlockElement\Text |
22
|
|
|
*/ |
23
|
|
|
protected $label; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Input element. |
27
|
|
|
* |
28
|
|
|
* @var \Maknz\Slack\BlockElement |
29
|
|
|
*/ |
30
|
|
|
protected $element; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* A hint that appears below the input. |
34
|
|
|
* |
35
|
|
|
* @var \Maknz\Slack\BlockElement\Text |
36
|
|
|
*/ |
37
|
|
|
protected $hint; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Whether the input may be empty. |
41
|
|
|
* |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
protected $optional = false; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Internal attribute to property map. |
48
|
|
|
* |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected static $availableAttributes = [ |
52
|
|
|
'label' => 'label', |
53
|
|
|
'element' => 'element', |
54
|
|
|
'block_id' => 'block_id', |
55
|
|
|
'hint' => 'hint', |
56
|
|
|
'optional' => 'optional', |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the input label. |
61
|
|
|
* |
62
|
|
|
* @return \Maknz\Slack\BlockElement\Text |
63
|
|
|
*/ |
64
|
3 |
|
public function getLabel() |
65
|
|
|
{ |
66
|
3 |
|
return $this->label; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set the input label. |
71
|
|
|
* |
72
|
|
|
* @param mixed |
73
|
|
|
* |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
3 |
|
public function setLabel($label) |
77
|
|
|
{ |
78
|
3 |
|
$this->label = Text::create($label); |
79
|
|
|
|
80
|
3 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the input element. |
85
|
|
|
* |
86
|
|
|
* @return \Maknz\Slack\BlockElement |
87
|
|
|
*/ |
88
|
4 |
|
public function getElement() |
89
|
|
|
{ |
90
|
4 |
|
return $this->element; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set the input element. |
95
|
|
|
* |
96
|
|
|
* @param mixed |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
* |
100
|
|
|
* @throws \InvalidArgumentException |
101
|
|
|
*/ |
102
|
5 |
|
public function setElement($element) |
103
|
|
|
{ |
104
|
5 |
|
$element = BlockElement::factory($element); |
105
|
|
|
|
106
|
5 |
|
if ( ! $element->isValidFor($this)) { |
107
|
1 |
|
throw new InvalidArgumentException('Block element '.get_class($element).' is not valid for '.static::class); |
108
|
|
|
} |
109
|
|
|
|
110
|
4 |
|
$this->element = $element; |
111
|
|
|
|
112
|
4 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the input hint. |
117
|
|
|
* |
118
|
|
|
* @return \Maknz\Slack\BlockElement\Text |
119
|
|
|
*/ |
120
|
4 |
|
public function getHint() |
121
|
|
|
{ |
122
|
4 |
|
return $this->hint; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Set the input hint. |
127
|
|
|
* |
128
|
|
|
* @param mixed |
129
|
|
|
* |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
2 |
|
public function setHint($hint) |
133
|
|
|
{ |
134
|
2 |
|
$this->hint = Text::create($hint); |
135
|
|
|
|
136
|
2 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get whether the input is optional. |
141
|
|
|
* |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
3 |
|
public function getOptional() |
145
|
|
|
{ |
146
|
3 |
|
return $this->optional; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Set whether the input is optional. |
151
|
|
|
* |
152
|
|
|
* @param bool |
153
|
|
|
* |
154
|
|
|
* @return $this |
155
|
|
|
*/ |
156
|
3 |
|
public function setOptional($optional) |
157
|
|
|
{ |
158
|
3 |
|
$this->optional = (bool)$optional; |
159
|
|
|
|
160
|
3 |
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Convert the block to its array representation. |
165
|
|
|
* |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
3 |
|
public function toArray() |
169
|
|
|
{ |
170
|
|
|
$data = [ |
171
|
3 |
|
'type' => $this->getType(), |
172
|
3 |
|
'label' => $this->getLabel()->toArray(), |
173
|
3 |
|
'element' => $this->getElement()->toArray(), |
174
|
|
|
]; |
175
|
|
|
|
176
|
3 |
|
if ($this->getBlockId()) { |
177
|
1 |
|
$data['block_id'] = $this->getBlockId(); |
178
|
|
|
} |
179
|
|
|
|
180
|
3 |
|
if ($this->getHint()) { |
181
|
1 |
|
$data['hint'] = $this->getHint()->toArray(); |
182
|
|
|
} |
183
|
|
|
|
184
|
3 |
|
if ($this->getOptional() != null) { |
185
|
3 |
|
$data['optional'] = $this->getOptional(); |
186
|
|
|
} |
187
|
|
|
|
188
|
3 |
|
return $data; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|