1
|
|
|
<?php |
2
|
|
|
namespace Maknz\Slack; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
|
6
|
|
|
class AttachmentAction |
7
|
|
|
{ |
8
|
|
|
const TYPE_BUTTON = 'button'; |
9
|
|
|
|
10
|
|
|
const STYLE_DEFAULT = 'default'; |
11
|
|
|
const STYLE_PRIMARY = 'primary'; |
12
|
|
|
const STYLE_DANGER = 'danger'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The required name field of the action. The name will be returned to your Action URL. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $name; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The required label for the action. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $text; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Button style. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $style; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The required type of the action. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $type = self::TYPE_BUTTON; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Optional value. It will be sent to your Action URL. |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $value; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Optional URL. |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $url; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Confirmation field. |
58
|
|
|
* |
59
|
|
|
* @var ActionConfirmation |
60
|
|
|
*/ |
61
|
|
|
protected $confirm; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Instantiate a new AttachmentAction. |
65
|
|
|
* |
66
|
|
|
* @param array $attributes |
67
|
|
|
* |
68
|
|
|
* @throws \InvalidArgumentException |
69
|
|
|
*/ |
70
|
4 |
|
public function __construct(array $attributes) |
71
|
|
|
{ |
72
|
4 |
|
if (isset($attributes['name'])) { |
73
|
4 |
|
$this->setName($attributes['name']); |
74
|
|
|
} |
75
|
|
|
|
76
|
4 |
|
if (isset($attributes['text'])) { |
77
|
4 |
|
$this->setText($attributes['text']); |
78
|
|
|
} |
79
|
|
|
|
80
|
4 |
|
if (isset($attributes['style'])) { |
81
|
4 |
|
$this->setStyle($attributes['style']); |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
if (isset($attributes['type'])) { |
85
|
4 |
|
$this->setType($attributes['type']); |
86
|
|
|
} |
87
|
|
|
|
88
|
4 |
|
if (isset($attributes['url'])) { |
89
|
2 |
|
$this->setUrl($attributes['url']); |
90
|
|
|
} |
91
|
|
|
|
92
|
4 |
|
if (isset($attributes['value'])) { |
93
|
4 |
|
$this->setValue($attributes['value']); |
94
|
|
|
} |
95
|
|
|
|
96
|
4 |
|
if (isset($attributes['confirm'])) { |
97
|
4 |
|
$this->setConfirm($attributes['confirm']); |
98
|
|
|
} |
99
|
4 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
2 |
|
public function getName() |
105
|
|
|
{ |
106
|
2 |
|
return $this->name; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $name |
111
|
|
|
* @return AttachmentAction |
112
|
|
|
*/ |
113
|
4 |
|
public function setName($name) |
114
|
|
|
{ |
115
|
4 |
|
$this->name = $name; |
116
|
|
|
|
117
|
4 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
3 |
|
public function getText() |
124
|
|
|
{ |
125
|
3 |
|
return $this->text; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $text |
130
|
|
|
* @return AttachmentAction |
131
|
|
|
*/ |
132
|
4 |
|
public function setText($text) |
133
|
|
|
{ |
134
|
4 |
|
$this->text = $text; |
135
|
|
|
|
136
|
4 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
2 |
|
public function getUrl() |
143
|
|
|
{ |
144
|
2 |
|
return $this->url; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $url |
149
|
|
|
* @return AttachmentAction |
150
|
|
|
*/ |
151
|
2 |
|
public function setUrl($url) |
152
|
|
|
{ |
153
|
2 |
|
$this->url = $url; |
154
|
|
|
|
155
|
2 |
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
2 |
|
public function getStyle() |
162
|
|
|
{ |
163
|
2 |
|
return $this->style; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $style |
168
|
|
|
* @return AttachmentAction |
169
|
|
|
*/ |
170
|
4 |
|
public function setStyle($style) |
171
|
|
|
{ |
172
|
4 |
|
$this->style = $style; |
173
|
|
|
|
174
|
4 |
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
2 |
|
public function getType() |
181
|
|
|
{ |
182
|
2 |
|
return $this->type; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param string $type |
187
|
|
|
* @return AttachmentAction |
188
|
|
|
*/ |
189
|
4 |
|
public function setType($type) |
190
|
|
|
{ |
191
|
4 |
|
$this->type = $type; |
192
|
|
|
|
193
|
4 |
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
2 |
|
public function getValue() |
200
|
|
|
{ |
201
|
2 |
|
return $this->value; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param string $value |
206
|
|
|
* @return AttachmentAction |
207
|
|
|
*/ |
208
|
4 |
|
public function setValue($value) |
209
|
|
|
{ |
210
|
4 |
|
$this->value = $value; |
211
|
|
|
|
212
|
4 |
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return ActionConfirmation |
217
|
|
|
*/ |
218
|
2 |
|
public function getConfirm() |
219
|
|
|
{ |
220
|
2 |
|
return $this->confirm; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param ActionConfirmation|array $confirm |
225
|
|
|
* |
226
|
|
|
* @return AttachmentAction |
227
|
|
|
* |
228
|
|
|
* @throws \InvalidArgumentException |
229
|
|
|
*/ |
230
|
4 |
|
public function setConfirm($confirm) |
231
|
|
|
{ |
232
|
4 |
|
if ($confirm instanceof ActionConfirmation) { |
233
|
|
|
$this->confirm = $confirm; |
234
|
|
|
|
235
|
|
|
return $this; |
236
|
4 |
|
} elseif (is_array($confirm)) { |
|
|
|
|
237
|
4 |
|
$this->confirm = new ActionConfirmation($confirm); |
238
|
|
|
|
239
|
4 |
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
throw new InvalidArgumentException('The action confirmation must be an instance of Maknz\Slack\ActionConfirmation or a keyed array'); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Get the array representation of this attachment action. |
247
|
|
|
* |
248
|
|
|
* @return array |
249
|
|
|
*/ |
250
|
2 |
|
public function toArray() |
251
|
|
|
{ |
252
|
|
|
$array = [ |
253
|
2 |
|
'name' => $this->getName(), |
254
|
2 |
|
'text' => $this->getText(), |
255
|
2 |
|
'style' => $this->getStyle(), |
256
|
2 |
|
'type' => $this->getType(), |
257
|
2 |
|
'value' => $this->getValue(), |
258
|
2 |
|
'url' => $this->getUrl(), |
259
|
|
|
]; |
260
|
|
|
|
261
|
2 |
|
if (($confirm = $this->getConfirm()) !== null) { |
262
|
2 |
|
$array['confirm'] = $confirm->toArray(); |
263
|
|
|
} |
264
|
|
|
|
265
|
2 |
|
return $array; |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|