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