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