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