1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\SlashCommand; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Spatie\SlashCommand\Exceptions\FieldCannotBeAdded; |
8
|
|
|
use Spatie\SlashCommand\Exceptions\ActionCannotBeAdded; |
9
|
|
|
|
10
|
|
|
class Attachment |
11
|
|
|
{ |
12
|
|
|
const COLOR_GOOD = 'good'; |
13
|
|
|
const COLOR_WARNING = 'warning'; |
14
|
|
|
const COLOR_DANGER = 'danger'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The fallback text to use for clients that don't support attachments. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $fallback; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Optional text that should appear within the attachment. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $text = ''; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Optional image that should appear within the attachment. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $imageUrl; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Optional thumbnail that should appear within the attachment. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $thumbUrl; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Optional text that should appear above the formatted data. |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $preText; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Optional title for the attachment. |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $title; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Optional title link for the attachment. |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $titleLink; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Optional author name for the attachment. |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
protected $authorName; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Optional author link for the attachment. |
74
|
|
|
* |
75
|
|
|
* @var string |
76
|
|
|
*/ |
77
|
|
|
protected $authorLink; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Optional author icon for the attachment. |
81
|
|
|
* |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
protected $authorIcon; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* The color to use for the attachment. |
88
|
|
|
* |
89
|
|
|
* @var string |
90
|
|
|
*/ |
91
|
|
|
protected $color; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* The text to use for the attachment footer. |
95
|
|
|
* |
96
|
|
|
* @var string |
97
|
|
|
*/ |
98
|
|
|
protected $footer; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* The icon to use for the attachment footer. |
102
|
|
|
* |
103
|
|
|
* @var string |
104
|
|
|
*/ |
105
|
|
|
protected $footerIcon; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* The timestamp to use for the attachment. |
109
|
|
|
* |
110
|
|
|
* @var \DateTime |
111
|
|
|
*/ |
112
|
|
|
protected $timestamp; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* The callback id to use for the attachment. |
116
|
|
|
* |
117
|
|
|
* @var string |
118
|
|
|
*/ |
119
|
|
|
protected $callbackId; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* The fields of the attachment. |
123
|
|
|
* |
124
|
|
|
* @var \Illuminate\Support\Collection |
125
|
|
|
*/ |
126
|
|
|
protected $fields; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Use message formatting. |
130
|
|
|
* |
131
|
|
|
* @var bool |
132
|
|
|
*/ |
133
|
|
|
protected $useMarkdown = false; |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* The actions of the attachment. |
137
|
|
|
* |
138
|
|
|
* @var \Illuminate\Support\Collection |
139
|
|
|
*/ |
140
|
|
|
protected $actions; |
141
|
|
|
|
142
|
|
|
public static function create() |
143
|
|
|
{ |
144
|
|
|
return new static(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function __construct() |
148
|
|
|
{ |
149
|
|
|
$this->fields = new Collection(); |
150
|
|
|
|
151
|
|
|
$this->actions = new Collection(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set the fallback text. |
156
|
|
|
* |
157
|
|
|
* @param string $fallback |
158
|
|
|
* |
159
|
|
|
* @return $this |
160
|
|
|
*/ |
161
|
|
|
public function setFallback(string $fallback) |
162
|
|
|
{ |
163
|
|
|
$this->fallback = $fallback; |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Set the optional text to appear within the attachment. |
170
|
|
|
* |
171
|
|
|
* @param string $text |
172
|
|
|
* |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
|
|
public function setText($text) |
176
|
|
|
{ |
177
|
|
|
$this->text = $text; |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Set the optional image to appear within the attachment. |
184
|
|
|
* |
185
|
|
|
* @param string $imageUrl |
186
|
|
|
* |
187
|
|
|
* @return $this |
188
|
|
|
*/ |
189
|
|
|
public function setImageUrl(string $imageUrl) |
190
|
|
|
{ |
191
|
|
|
$this->imageUrl = $imageUrl; |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Set the optional thumbnail to appear within the attachment. |
198
|
|
|
* |
199
|
|
|
* @param string $thumbUrl |
200
|
|
|
* |
201
|
|
|
* @return $this |
202
|
|
|
*/ |
203
|
|
|
public function setThumbUrl(string $thumbUrl) |
204
|
|
|
{ |
205
|
|
|
$this->thumbUrl = $thumbUrl; |
206
|
|
|
|
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Set the text that should appear above the formatted data. |
212
|
|
|
* |
213
|
|
|
* @param string $preText |
214
|
|
|
* |
215
|
|
|
* @return $this |
216
|
|
|
*/ |
217
|
|
|
public function setPreText(string $preText) |
218
|
|
|
{ |
219
|
|
|
$this->preText = $preText; |
220
|
|
|
|
221
|
|
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Set the color to use for the attachment. |
226
|
|
|
* |
227
|
|
|
* @param string $color |
228
|
|
|
* |
229
|
|
|
* @return $this |
230
|
|
|
*/ |
231
|
|
|
public function setColor($color) |
232
|
|
|
{ |
233
|
|
|
$this->color = $color; |
234
|
|
|
|
235
|
|
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Set the footer text to use for the attachment. |
240
|
|
|
* |
241
|
|
|
* @param string $footer |
242
|
|
|
* |
243
|
|
|
* @return $this |
244
|
|
|
*/ |
245
|
|
|
public function setFooter(string $footer) |
246
|
|
|
{ |
247
|
|
|
$this->footer = $footer; |
248
|
|
|
|
249
|
|
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Set the footer icon to use for the attachment. |
254
|
|
|
* |
255
|
|
|
* @param string $footerIcon |
256
|
|
|
* |
257
|
|
|
* @return $this |
258
|
|
|
*/ |
259
|
|
|
public function setFooterIcon(string $footerIcon) |
260
|
|
|
{ |
261
|
|
|
$this->footerIcon = $footerIcon; |
262
|
|
|
|
263
|
|
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Set the timestamp to use for the attachment. |
268
|
|
|
* |
269
|
|
|
* @param \DateTime $timestamp |
270
|
|
|
* |
271
|
|
|
* @return $this |
272
|
|
|
*/ |
273
|
|
|
public function setTimestamp(DateTime $timestamp) |
274
|
|
|
{ |
275
|
|
|
$this->timestamp = $timestamp; |
276
|
|
|
|
277
|
|
|
return $this; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Set the title to use for the attachment. |
282
|
|
|
* |
283
|
|
|
* @param string $title |
284
|
|
|
* |
285
|
|
|
* @return $this |
286
|
|
|
*/ |
287
|
|
|
public function setTitle(string $title) |
288
|
|
|
{ |
289
|
|
|
$this->title = $title; |
290
|
|
|
|
291
|
|
|
return $this; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Set the title link to use for the attachment. |
296
|
|
|
* |
297
|
|
|
* @param string $titleLink |
298
|
|
|
* |
299
|
|
|
* @return $this |
300
|
|
|
*/ |
301
|
|
|
public function setTitleLink(string $titleLink) |
302
|
|
|
{ |
303
|
|
|
$this->titleLink = $titleLink; |
304
|
|
|
|
305
|
|
|
return $this; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Set the author name to use for the attachment. |
310
|
|
|
* |
311
|
|
|
* @param string $authorName |
312
|
|
|
* |
313
|
|
|
* @return $this |
314
|
|
|
*/ |
315
|
|
|
public function setAuthorName(string $authorName) |
316
|
|
|
{ |
317
|
|
|
$this->authorName = $authorName; |
318
|
|
|
|
319
|
|
|
return $this; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Enable simple markup language. |
324
|
|
|
* |
325
|
|
|
* @return $this |
326
|
|
|
*/ |
327
|
|
|
public function useMarkdown() |
328
|
|
|
{ |
329
|
|
|
$this->useMarkdown = true; |
330
|
|
|
|
331
|
|
|
return $this; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Set the auhtor link to use for the attachment. |
336
|
|
|
* |
337
|
|
|
* @param string $authorLink |
338
|
|
|
* |
339
|
|
|
* @return $this |
340
|
|
|
*/ |
341
|
|
|
public function setAuthorLink(string $authorLink) |
342
|
|
|
{ |
343
|
|
|
$this->authorLink = $authorLink; |
344
|
|
|
|
345
|
|
|
return $this; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Set the author icon to use for the attachment. |
350
|
|
|
* |
351
|
|
|
* @param string $authorIcon |
352
|
|
|
* |
353
|
|
|
* @return $this |
354
|
|
|
*/ |
355
|
|
|
public function setAuthorIcon(string $authorIcon) |
356
|
|
|
{ |
357
|
|
|
$this->authorIcon = $authorIcon; |
358
|
|
|
|
359
|
|
|
return $this; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Set the callback id to use for the attachment. |
364
|
|
|
* |
365
|
|
|
* @param string $callbackId |
366
|
|
|
* |
367
|
|
|
* @return $this |
368
|
|
|
*/ |
369
|
|
|
public function setCallbackId(string $callbackId) |
370
|
|
|
{ |
371
|
|
|
$this->callbackId = $callbackId; |
372
|
|
|
|
373
|
|
|
return $this; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Add a field to the attachment. |
378
|
|
|
* |
379
|
|
|
* @param \Spatie\SlashCommand\AttachmentField|array $field |
380
|
|
|
* |
381
|
|
|
* @return $this |
382
|
|
|
* |
383
|
|
|
* @throws \Spatie\SlashCommand\Exceptions\FieldCannotBeAdded |
384
|
|
|
*/ |
385
|
|
|
public function addField($field) |
386
|
|
|
{ |
387
|
|
|
if (! is_array($field) && ! $field instanceof AttachmentField) { |
388
|
|
|
throw FieldCannotBeAdded::invalidType(); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
if (is_array($field)) { |
392
|
|
|
$title = array_keys($field)[0]; |
393
|
|
|
$value = $field[$title]; |
394
|
|
|
|
395
|
|
|
$field = AttachmentField::create($title, $value); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
$this->fields->push($field); |
399
|
|
|
|
400
|
|
|
return $this; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* Add the fields for the attachment. |
405
|
|
|
* |
406
|
|
|
* @param array $fields |
407
|
|
|
* |
408
|
|
|
* @return $this |
409
|
|
|
*/ |
410
|
|
|
public function addFields(array $fields) |
411
|
|
|
{ |
412
|
|
|
collect($fields)->each(function ($field, $key) { |
413
|
|
|
if (! $field instanceof AttachmentField) { |
414
|
|
|
$field = [$key => $field]; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
$this->addField($field); |
418
|
|
|
}); |
419
|
|
|
|
420
|
|
|
return $this; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* Set the fields for the attachment. |
425
|
|
|
* |
426
|
|
|
* @param array $fields |
427
|
|
|
* |
428
|
|
|
* @return $this |
429
|
|
|
*/ |
430
|
|
|
public function setFields(array $fields) |
431
|
|
|
{ |
432
|
|
|
$this->clearFields(); |
433
|
|
|
|
434
|
|
|
$this->addFields($fields); |
435
|
|
|
|
436
|
|
|
return $this; |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Clear all fields for this attachment. |
441
|
|
|
* |
442
|
|
|
* @return $this |
443
|
|
|
*/ |
444
|
|
|
public function clearFields() |
445
|
|
|
{ |
446
|
|
|
$this->fields = new Collection(); |
447
|
|
|
|
448
|
|
|
return $this; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* @param \Spatie\SlashCommand\AttachmentAction|array $action |
453
|
|
|
* |
454
|
|
|
* @return $this |
455
|
|
|
* @throws \Spatie\SlashCommand\Exceptions\ActionCannotBeAdded |
456
|
|
|
*/ |
457
|
|
|
public function addAction($action) |
458
|
|
|
{ |
459
|
|
|
if (! is_array($action) && ! $action instanceof AttachmentAction) { |
460
|
|
|
throw ActionCannotBeAdded::invalidType(); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
if (is_array($action)) { |
464
|
|
|
$name = $action['name']; |
465
|
|
|
$text = $action['text']; |
466
|
|
|
$type = $action['type']; |
467
|
|
|
|
468
|
|
|
$action = AttachmentAction::create($name, $text, $type); |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
$this->actions->push($action); |
472
|
|
|
|
473
|
|
|
return $this; |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* Add the actions for the attachment. |
478
|
|
|
* |
479
|
|
|
* @param array $actions |
480
|
|
|
* |
481
|
|
|
* @return $this |
482
|
|
|
*/ |
483
|
|
|
public function addActions(array $actions) |
484
|
|
|
{ |
485
|
|
|
collect($actions)->each(function ($action) { |
486
|
|
|
$this->addAction($action); |
487
|
|
|
}); |
488
|
|
|
|
489
|
|
|
return $this; |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
/** |
493
|
|
|
* Set the actions for the attachment. |
494
|
|
|
* |
495
|
|
|
* @param array $actions |
496
|
|
|
* |
497
|
|
|
* @return $this |
498
|
|
|
*/ |
499
|
|
|
public function setActions(array $actions) |
500
|
|
|
{ |
501
|
|
|
$this->clearActions(); |
502
|
|
|
|
503
|
|
|
$this->addActions($actions); |
504
|
|
|
|
505
|
|
|
return $this; |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* Clear all actions for this attachment. |
510
|
|
|
* |
511
|
|
|
* @return $this |
512
|
|
|
*/ |
513
|
|
|
public function clearActions() |
514
|
|
|
{ |
515
|
|
|
$this->actions = new Collection(); |
516
|
|
|
|
517
|
|
|
return $this; |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* Convert this attachment to its array representation. |
522
|
|
|
* |
523
|
|
|
* @return array |
524
|
|
|
*/ |
525
|
|
|
public function toArray() |
526
|
|
|
{ |
527
|
|
|
return [ |
528
|
|
|
'fallback' => $this->fallback, |
529
|
|
|
'text' => $this->text, |
530
|
|
|
'pretext' => $this->preText, |
531
|
|
|
'color' => $this->color, |
532
|
|
|
'footer' => $this->footer, |
533
|
|
|
'mrkdwn_in' => $this->useMarkdown ? ['text', 'pretext', 'footer'] : [], |
534
|
|
|
'footer_icon' => $this->footer, |
535
|
|
|
'ts' => $this->timestamp ? $this->timestamp->getTimestamp() : null, |
536
|
|
|
'image_url' => $this->imageUrl, |
537
|
|
|
'thumb_url' => $this->thumbUrl, |
538
|
|
|
'title' => $this->title, |
539
|
|
|
'title_link' => $this->titleLink, |
540
|
|
|
'author_name' => $this->authorName, |
541
|
|
|
'author_link' => $this->authorLink, |
542
|
|
|
'author_icon' => $this->authorIcon, |
543
|
|
|
'callback_id' => $this->callbackId, |
544
|
|
|
'fields' => $this->fields->map(function (AttachmentField $field) { |
545
|
|
|
return $field->toArray(); |
546
|
|
|
})->toArray(), |
547
|
|
|
'actions' => $this->actions->map(function (AttachmentAction $action) { |
548
|
|
|
return $action->toArray(); |
549
|
|
|
})->toArray(), |
550
|
|
|
]; |
551
|
|
|
} |
552
|
|
|
} |
553
|
|
|
|