1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maknz\Slack; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
class Attachment |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The fallback text to use for clients that don't support attachments. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $fallback; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Optional text that should appear within the attachment. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $text; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Optional image that should appear within the attachment. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $image_url; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Optional thumbnail that should appear within the attachment. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $thumb_url; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Optional text that should appear above the formatted data. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $pretext; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Optional title for the attachment. |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $title; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Optional title link for the attachment. |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $title_link; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Optional author name for the attachment. |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $author_name; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Optional author link for the attachment. |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
protected $author_link; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Optional author icon for the attachment. |
74
|
|
|
* |
75
|
|
|
* @var string |
76
|
|
|
*/ |
77
|
|
|
protected $author_icon; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* The color to use for the attachment. |
81
|
|
|
* |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
protected $color = 'good'; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* The text to use for the attachment footer. |
88
|
|
|
* |
89
|
|
|
* @var string |
90
|
|
|
*/ |
91
|
|
|
protected $footer; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* The icon to use for the attachment footer. |
95
|
|
|
* |
96
|
|
|
* @var string |
97
|
|
|
*/ |
98
|
|
|
protected $footer_icon; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* The timestamp to use for the attachment. |
102
|
|
|
* |
103
|
|
|
* @var \DateTime |
104
|
|
|
*/ |
105
|
|
|
protected $timestamp; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* The fields of the attachment. |
109
|
|
|
* |
110
|
|
|
* @var array |
111
|
|
|
*/ |
112
|
|
|
protected $fields = []; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* The fields of the attachment that Slack should interpret |
116
|
|
|
* with its Markdown-like language. |
117
|
|
|
* |
118
|
|
|
* @var array |
119
|
|
|
*/ |
120
|
|
|
protected $markdown_fields = []; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* The callback_id used by the API receiving |
124
|
|
|
* the POST request to respond to interactive buttons |
125
|
|
|
* |
126
|
|
|
* @var string |
127
|
|
|
*/ |
128
|
|
|
protected $callback_id; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* A collection of actions (buttons) to include in the attachment. |
132
|
|
|
* A maximum of 5 actions may be provided. |
133
|
|
|
* |
134
|
|
|
* @var array |
135
|
|
|
*/ |
136
|
|
|
protected $actions = []; |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Instantiate a new Attachment. |
140
|
|
|
* |
141
|
|
|
* @param array $attributes |
142
|
|
|
* @return void |
|
|
|
|
143
|
|
|
*/ |
144
|
|
|
public function __construct(array $attributes) |
145
|
|
|
{ |
146
|
|
|
if (isset($attributes['fallback'])) { |
147
|
|
|
$this->setFallback($attributes['fallback']); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if (isset($attributes['text'])) { |
151
|
|
|
$this->setText($attributes['text']); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if (isset($attributes['image_url'])) { |
155
|
|
|
$this->setImageUrl($attributes['image_url']); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (isset($attributes['thumb_url'])) { |
159
|
|
|
$this->setThumbUrl($attributes['thumb_url']); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (isset($attributes['pretext'])) { |
163
|
|
|
$this->setPretext($attributes['pretext']); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if (isset($attributes['color'])) { |
167
|
|
|
$this->setColor($attributes['color']); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if (isset($attributes['footer'])) { |
171
|
|
|
$this->setFooter($attributes['footer']); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if (isset($attributes['footer_icon'])) { |
175
|
|
|
$this->setFooterIcon($attributes['footer_icon']); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if (isset($attributes['timestamp'])) { |
179
|
|
|
$this->setTimestamp($attributes['timestamp']); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if (isset($attributes['fields'])) { |
183
|
|
|
$this->setFields($attributes['fields']); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if (isset($attributes['mrkdwn_in'])) { |
187
|
|
|
$this->setMarkdownFields($attributes['mrkdwn_in']); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
if (isset($attributes['title'])) { |
191
|
|
|
$this->setTitle($attributes['title']); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if (isset($attributes['title_link'])) { |
195
|
|
|
$this->setTitleLink($attributes['title_link']); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if (isset($attributes['author_name'])) { |
199
|
|
|
$this->setAuthorName($attributes['author_name']); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
if (isset($attributes['author_link'])) { |
203
|
|
|
$this->setAuthorLink($attributes['author_link']); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if (isset($attributes['author_icon'])) { |
207
|
|
|
$this->setAuthorIcon($attributes['author_icon']); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
if (isset($attributes['callback_id'])) { |
211
|
|
|
$this->setCallbackId($attributes['callback_id']); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
if (isset($attributes['actions'])) { |
215
|
|
|
$this->setActions($attributes['actions']); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Get the fallback text. |
221
|
|
|
* |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
|
|
public function getFallback() |
225
|
|
|
{ |
226
|
|
|
return $this->fallback; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Set the fallback text. |
231
|
|
|
* |
232
|
|
|
* @param string $fallback |
233
|
|
|
* @return $this |
234
|
|
|
*/ |
235
|
|
|
public function setFallback($fallback) |
236
|
|
|
{ |
237
|
|
|
$this->fallback = $fallback; |
238
|
|
|
|
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Get the optional text to appear within the attachment. |
244
|
|
|
* |
245
|
|
|
* @return string |
246
|
|
|
*/ |
247
|
|
|
public function getText() |
248
|
|
|
{ |
249
|
|
|
return $this->text; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Set the optional text to appear within the attachment. |
254
|
|
|
* |
255
|
|
|
* @param string $text |
256
|
|
|
* @return $this |
257
|
|
|
*/ |
258
|
|
|
public function setText($text) |
259
|
|
|
{ |
260
|
|
|
$this->text = $text; |
261
|
|
|
|
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Get the optional image to appear within the attachment. |
267
|
|
|
* |
268
|
|
|
* @return string |
269
|
|
|
*/ |
270
|
|
|
public function getImageUrl() |
271
|
|
|
{ |
272
|
|
|
return $this->image_url; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Set the optional image to appear within the attachment. |
277
|
|
|
* |
278
|
|
|
* @param string $image_url |
279
|
|
|
* @return $this |
280
|
|
|
*/ |
281
|
|
|
public function setImageUrl($image_url) |
282
|
|
|
{ |
283
|
|
|
$this->image_url = $image_url; |
284
|
|
|
|
285
|
|
|
return $this; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Get the optional thumbnail to appear within the attachment. |
290
|
|
|
* |
291
|
|
|
* @return string |
292
|
|
|
*/ |
293
|
|
|
public function getThumbUrl() |
294
|
|
|
{ |
295
|
|
|
return $this->thumb_url; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Set the optional thumbnail to appear within the attachment. |
300
|
|
|
* |
301
|
|
|
* @param string $thumb_url |
302
|
|
|
* @return $this |
303
|
|
|
*/ |
304
|
|
|
public function setThumbUrl($thumb_url) |
305
|
|
|
{ |
306
|
|
|
$this->thumb_url = $thumb_url; |
307
|
|
|
|
308
|
|
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Get the text that should appear above the formatted data. |
313
|
|
|
* |
314
|
|
|
* @return string |
315
|
|
|
*/ |
316
|
|
|
public function getPretext() |
317
|
|
|
{ |
318
|
|
|
return $this->pretext; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Set the text that should appear above the formatted data. |
323
|
|
|
* |
324
|
|
|
* @param string $pretext |
325
|
|
|
* @return $this |
326
|
|
|
*/ |
327
|
|
|
public function setPretext($pretext) |
328
|
|
|
{ |
329
|
|
|
$this->pretext = $pretext; |
330
|
|
|
|
331
|
|
|
return $this; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Get the color to use for the attachment. |
336
|
|
|
* |
337
|
|
|
* @return string |
338
|
|
|
*/ |
339
|
|
|
public function getColor() |
340
|
|
|
{ |
341
|
|
|
return $this->color; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Set the color to use for the attachment. |
346
|
|
|
* |
347
|
|
|
* @param string $color |
348
|
|
|
* @return $this |
349
|
|
|
*/ |
350
|
|
|
public function setColor($color) |
351
|
|
|
{ |
352
|
|
|
$this->color = $color; |
353
|
|
|
|
354
|
|
|
return $this; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Get the footer to use for the attachment. |
359
|
|
|
* |
360
|
|
|
* @return string |
361
|
|
|
*/ |
362
|
|
|
public function getFooter() |
363
|
|
|
{ |
364
|
|
|
return $this->footer; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Set the footer text to use for the attachment. |
369
|
|
|
* |
370
|
|
|
* @param string $footer |
371
|
|
|
* @return $this |
372
|
|
|
*/ |
373
|
|
|
public function setFooter($footer) |
374
|
|
|
{ |
375
|
|
|
$this->footer = $footer; |
376
|
|
|
|
377
|
|
|
return $this; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Get the footer icon to use for the attachment. |
382
|
|
|
* |
383
|
|
|
* @return string |
384
|
|
|
*/ |
385
|
|
|
public function getFooterIcon() |
386
|
|
|
{ |
387
|
|
|
return $this->footer_icon; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Set the footer icon to use for the attachment. |
392
|
|
|
* |
393
|
|
|
* @param string $footerIcon |
394
|
|
|
* @return $this |
395
|
|
|
*/ |
396
|
|
|
public function setFooterIcon($footerIcon) |
397
|
|
|
{ |
398
|
|
|
$this->footer_icon = $footerIcon; |
399
|
|
|
|
400
|
|
|
return $this; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* Get the timestamp to use for the attachment. |
405
|
|
|
* |
406
|
|
|
* @return \DateTime |
407
|
|
|
*/ |
408
|
|
|
public function getTimestamp() |
409
|
|
|
{ |
410
|
|
|
return $this->timestamp; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* Set the timestamp to use for the attachment. |
415
|
|
|
* |
416
|
|
|
* @param \DateTime $timestamp |
417
|
|
|
* @return $this |
418
|
|
|
*/ |
419
|
|
|
public function setTimestamp($timestamp) |
420
|
|
|
{ |
421
|
|
|
$this->timestamp = $timestamp; |
422
|
|
|
|
423
|
|
|
return $this; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Get the title to use for the attachment. |
428
|
|
|
* |
429
|
|
|
* @return string |
430
|
|
|
*/ |
431
|
|
|
public function getTitle() |
432
|
|
|
{ |
433
|
|
|
return $this->title; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Set the title to use for the attachment. |
438
|
|
|
* |
439
|
|
|
* @param string $title |
440
|
|
|
* @return $this |
441
|
|
|
*/ |
442
|
|
|
public function setTitle($title) |
443
|
|
|
{ |
444
|
|
|
$this->title = $title; |
445
|
|
|
|
446
|
|
|
return $this; |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* Get the title link to use for the attachment. |
451
|
|
|
* |
452
|
|
|
* @return string |
453
|
|
|
*/ |
454
|
|
|
public function getTitleLink() |
455
|
|
|
{ |
456
|
|
|
return $this->title_link; |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* Set the title link to use for the attachment. |
461
|
|
|
* |
462
|
|
|
* @param string $title_link |
463
|
|
|
* @return $this |
464
|
|
|
*/ |
465
|
|
|
public function setTitleLink($title_link) |
466
|
|
|
{ |
467
|
|
|
$this->title_link = $title_link; |
468
|
|
|
|
469
|
|
|
return $this; |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* Get the author name to use for the attachment. |
474
|
|
|
* |
475
|
|
|
* @return string |
476
|
|
|
*/ |
477
|
|
|
public function getAuthorName() |
478
|
|
|
{ |
479
|
|
|
return $this->author_name; |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* Set the author name to use for the attachment. |
484
|
|
|
* |
485
|
|
|
* @param string $author_name |
486
|
|
|
* @return $this |
487
|
|
|
*/ |
488
|
|
|
public function setAuthorName($author_name) |
489
|
|
|
{ |
490
|
|
|
$this->author_name = $author_name; |
491
|
|
|
|
492
|
|
|
return $this; |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* Get the author link to use for the attachment. |
497
|
|
|
* |
498
|
|
|
* @return string |
499
|
|
|
*/ |
500
|
|
|
public function getAuthorLink() |
501
|
|
|
{ |
502
|
|
|
return $this->author_link; |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
/** |
506
|
|
|
* Set the auhtor link to use for the attachment. |
507
|
|
|
* |
508
|
|
|
* @param string $author_link |
509
|
|
|
* @return $this |
510
|
|
|
*/ |
511
|
|
|
public function setAuthorLink($author_link) |
512
|
|
|
{ |
513
|
|
|
$this->author_link = $author_link; |
514
|
|
|
|
515
|
|
|
return $this; |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* Get the author icon to use for the attachment. |
520
|
|
|
* |
521
|
|
|
* @return string |
522
|
|
|
*/ |
523
|
|
|
public function getAuthorIcon() |
524
|
|
|
{ |
525
|
|
|
return $this->author_icon; |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* Set the author icon to use for the attachment. |
530
|
|
|
* |
531
|
|
|
* @param string $author_icon |
532
|
|
|
* @return $this |
533
|
|
|
*/ |
534
|
|
|
public function setAuthorIcon($author_icon) |
535
|
|
|
{ |
536
|
|
|
$this->author_icon = $author_icon; |
537
|
|
|
|
538
|
|
|
return $this; |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
/** |
542
|
|
|
* Get the fields for the attachment. |
543
|
|
|
* |
544
|
|
|
* @return array |
545
|
|
|
*/ |
546
|
|
|
public function getFields() |
547
|
|
|
{ |
548
|
|
|
return $this->fields; |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
/** |
552
|
|
|
* Set the fields for the attachment. |
553
|
|
|
* |
554
|
|
|
* @param array $fields |
555
|
|
|
* @return $this |
556
|
|
|
*/ |
557
|
|
|
public function setFields(array $fields) |
558
|
|
|
{ |
559
|
|
|
$this->clearFields(); |
560
|
|
|
|
561
|
|
|
foreach ($fields as $field) { |
562
|
|
|
$this->addField($field); |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
return $this; |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* Add a field to the attachment. |
570
|
|
|
* |
571
|
|
|
* @param mixed $field |
572
|
|
|
* @return $this |
573
|
|
|
*/ |
574
|
|
|
public function addField($field) |
575
|
|
|
{ |
576
|
|
|
if ($field instanceof AttachmentField) { |
577
|
|
|
$this->fields[] = $field; |
578
|
|
|
|
579
|
|
|
return $this; |
580
|
|
|
} elseif (is_array($field)) { |
581
|
|
|
$this->fields[] = new AttachmentField($field); |
582
|
|
|
|
583
|
|
|
return $this; |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
throw new InvalidArgumentException('The attachment field must be an instance of Maknz\Slack\AttachmentField or a keyed array'); |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* Clear the fields for the attachment. |
591
|
|
|
* |
592
|
|
|
* @return $this |
593
|
|
|
*/ |
594
|
|
|
public function clearFields() |
595
|
|
|
{ |
596
|
|
|
$this->fields = []; |
597
|
|
|
|
598
|
|
|
return $this; |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
/** |
602
|
|
|
* Clear the actions for the attachment. |
603
|
|
|
* |
604
|
|
|
* @return $this |
605
|
|
|
*/ |
606
|
|
|
public function clearActions() |
607
|
|
|
{ |
608
|
|
|
$this->actions = []; |
609
|
|
|
|
610
|
|
|
return $this; |
611
|
|
|
} |
612
|
|
|
|
613
|
|
|
/** |
614
|
|
|
* Get the fields Slack should interpret in its |
615
|
|
|
* Markdown-like language. |
616
|
|
|
* |
617
|
|
|
* @return array |
618
|
|
|
*/ |
619
|
|
|
public function getMarkdownFields() |
620
|
|
|
{ |
621
|
|
|
return $this->markdown_fields; |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
/** |
625
|
|
|
* Set the fields Slack should interpret in its |
626
|
|
|
* Markdown-like language. |
627
|
|
|
* |
628
|
|
|
* @param array $fields |
629
|
|
|
* @return $this |
630
|
|
|
*/ |
631
|
|
|
public function setMarkdownFields(array $fields) |
632
|
|
|
{ |
633
|
|
|
$this->markdown_fields = $fields; |
634
|
|
|
|
635
|
|
|
return $this; |
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
/** |
639
|
|
|
* Get the callback_id specified for the attachment |
640
|
|
|
* |
641
|
|
|
* @return string |
642
|
|
|
*/ |
643
|
|
|
public function getCallbackId() |
644
|
|
|
{ |
645
|
|
|
return $this->callback_id; |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
/** |
649
|
|
|
* Set the callback_id for the attachment |
650
|
|
|
* |
651
|
|
|
* @param string |
652
|
|
|
* @return $this |
653
|
|
|
*/ public function setCallbackId($callback_id) |
654
|
|
|
{ |
655
|
|
|
$this->callback_id = $callback_id; |
656
|
|
|
|
657
|
|
|
return $this; |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
/** |
661
|
|
|
* Get the collection of actions (buttons) to include in the attachment. |
662
|
|
|
* |
663
|
|
|
* @return AttachmentAction[] |
664
|
|
|
*/ |
665
|
|
|
public function getActions() |
666
|
|
|
{ |
667
|
|
|
return $this->actions; |
668
|
|
|
} |
669
|
|
|
|
670
|
|
|
/** |
671
|
|
|
* Set the collection of actions (buttons) to include in the attachment. |
672
|
|
|
* |
673
|
|
|
* @param array $actions |
674
|
|
|
* @return Attachment |
675
|
|
|
*/ |
676
|
|
|
public function setActions($actions) |
677
|
|
|
{ |
678
|
|
|
$this->clearActions(); |
679
|
|
|
|
680
|
|
|
foreach ($actions as $action) { |
681
|
|
|
$this->addAction($action); |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
return $this; |
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
/** |
688
|
|
|
* Add an action to the attachment. |
689
|
|
|
* |
690
|
|
|
* @param mixed $action |
691
|
|
|
* @return $this |
692
|
|
|
*/ |
693
|
|
|
public function addAction($action) |
694
|
|
|
{ |
695
|
|
|
if ($action instanceof AttachmentAction) { |
696
|
|
|
$this->actions[] = $action; |
697
|
|
|
|
698
|
|
|
return $this; |
699
|
|
|
} elseif (is_array($action)) { |
700
|
|
|
$this->actions[] = new AttachmentAction($action); |
701
|
|
|
|
702
|
|
|
return $this; |
703
|
|
|
} |
704
|
|
|
|
705
|
|
|
throw new InvalidArgumentException('The attachment action must be an instance of Maknz\Slack\AttachmentAction or a keyed array'); |
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
/** |
709
|
|
|
* Convert this attachment to its array representation. |
710
|
|
|
* |
711
|
|
|
* @return array |
712
|
|
|
*/ |
713
|
|
|
public function toArray() |
714
|
|
|
{ |
715
|
|
|
$data = [ |
716
|
|
|
'fallback' => $this->getFallback(), |
717
|
|
|
'text' => $this->getText(), |
718
|
|
|
'pretext' => $this->getPretext(), |
719
|
|
|
'color' => $this->getColor(), |
720
|
|
|
'footer' => $this->getFooter(), |
721
|
|
|
'footer_icon' => $this->getFooterIcon(), |
722
|
|
|
'ts' => $this->getTimestamp() ? $this->getTimestamp()->getTimestamp() : null, |
723
|
|
|
'mrkdwn_in' => $this->getMarkdownFields(), |
724
|
|
|
'image_url' => $this->getImageUrl(), |
725
|
|
|
'thumb_url' => $this->getThumbUrl(), |
726
|
|
|
'title' => $this->getTitle(), |
727
|
|
|
'title_link' => $this->getTitleLink(), |
728
|
|
|
'author_name' => $this->getAuthorName(), |
729
|
|
|
'author_link' => $this->getAuthorLink(), |
730
|
|
|
'author_icon' => $this->getAuthorIcon(), |
731
|
|
|
'callback_id' => $this->getCallbackId(), |
732
|
|
|
]; |
733
|
|
|
|
734
|
|
|
$data['fields'] = $this->getFieldsAsArrays(); |
735
|
|
|
$data['actions'] = $this->getActionsAsArrays(); |
736
|
|
|
|
737
|
|
|
return $data; |
738
|
|
|
} |
739
|
|
|
|
740
|
|
|
/** |
741
|
|
|
* Iterates over all fields in this attachment and returns |
742
|
|
|
* them in their array form. |
743
|
|
|
* |
744
|
|
|
* @return array |
745
|
|
|
*/ |
746
|
|
|
protected function getFieldsAsArrays() |
747
|
|
|
{ |
748
|
|
|
$fields = []; |
749
|
|
|
|
750
|
|
|
foreach ($this->getFields() as $field) { |
751
|
|
|
$fields[] = $field->toArray(); |
752
|
|
|
} |
753
|
|
|
|
754
|
|
|
return $fields; |
755
|
|
|
} |
756
|
|
|
|
757
|
|
|
/** |
758
|
|
|
* Iterates over all actions in this attachment and returns |
759
|
|
|
* them in their array form. |
760
|
|
|
* |
761
|
|
|
* @return array |
762
|
|
|
*/ |
763
|
|
|
protected function getActionsAsArrays() |
764
|
|
|
{ |
765
|
|
|
$actions = []; |
766
|
|
|
|
767
|
|
|
foreach ($this->getActions() as $action) { |
768
|
|
|
$actions[] = $action->toArray(); |
769
|
|
|
} |
770
|
|
|
|
771
|
|
|
return $actions; |
772
|
|
|
} |
773
|
|
|
} |
774
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.