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
|
|
|
* The actions of the attachment. |
130
|
|
|
* |
131
|
|
|
* @var \Illuminate\Support\Collection |
132
|
|
|
*/ |
133
|
|
|
protected $actions; |
134
|
|
|
|
135
|
|
|
public static function create() |
136
|
|
|
{ |
137
|
|
|
return new static(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function __construct() |
141
|
|
|
{ |
142
|
|
|
$this->fields = new Collection(); |
143
|
|
|
|
144
|
|
|
$this->actions = new Collection(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set the fallback text. |
149
|
|
|
* |
150
|
|
|
* @param string $fallback |
151
|
|
|
* |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
|
|
public function setFallback(string $fallback) |
155
|
|
|
{ |
156
|
|
|
$this->fallback = $fallback; |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set the optional text to appear within the attachment. |
163
|
|
|
* |
164
|
|
|
* @param string $text |
165
|
|
|
* |
166
|
|
|
* @return $this |
167
|
|
|
*/ |
168
|
|
|
public function setText($text) |
169
|
|
|
{ |
170
|
|
|
$this->text = $text; |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Set the optional image to appear within the attachment. |
177
|
|
|
* |
178
|
|
|
* @param string $imageUrl |
179
|
|
|
* |
180
|
|
|
* @return $this |
181
|
|
|
*/ |
182
|
|
|
public function setImageUrl(string $imageUrl) |
183
|
|
|
{ |
184
|
|
|
$this->imageUrl = $imageUrl; |
185
|
|
|
|
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Set the optional thumbnail to appear within the attachment. |
191
|
|
|
* |
192
|
|
|
* @param string $thumbUrl |
193
|
|
|
* |
194
|
|
|
* @return $this |
195
|
|
|
*/ |
196
|
|
|
public function setThumbUrl(string $thumbUrl) |
197
|
|
|
{ |
198
|
|
|
$this->thumbUrl = $thumbUrl; |
199
|
|
|
|
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Set the text that should appear above the formatted data. |
205
|
|
|
* |
206
|
|
|
* @param string $preText |
207
|
|
|
* |
208
|
|
|
* @return $this |
209
|
|
|
*/ |
210
|
|
|
public function setPreText(string $preText) |
211
|
|
|
{ |
212
|
|
|
$this->preText = $preText; |
213
|
|
|
|
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Set the color to use for the attachment. |
219
|
|
|
* |
220
|
|
|
* @param string $color |
221
|
|
|
* |
222
|
|
|
* @return $this |
223
|
|
|
*/ |
224
|
|
|
public function setColor($color) |
225
|
|
|
{ |
226
|
|
|
$this->color = $color; |
227
|
|
|
|
228
|
|
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Set the footer text to use for the attachment. |
233
|
|
|
* |
234
|
|
|
* @param string $footer |
235
|
|
|
* |
236
|
|
|
* @return $this |
237
|
|
|
*/ |
238
|
|
|
public function setFooter(string $footer) |
239
|
|
|
{ |
240
|
|
|
$this->footer = $footer; |
241
|
|
|
|
242
|
|
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Set the footer icon to use for the attachment. |
247
|
|
|
* |
248
|
|
|
* @param string $footerIcon |
249
|
|
|
* |
250
|
|
|
* @return $this |
251
|
|
|
*/ |
252
|
|
|
public function setFooterIcon(string $footerIcon) |
253
|
|
|
{ |
254
|
|
|
$this->footerIcon = $footerIcon; |
255
|
|
|
|
256
|
|
|
return $this; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Set the timestamp to use for the attachment. |
261
|
|
|
* |
262
|
|
|
* @param \DateTime $timestamp |
263
|
|
|
* |
264
|
|
|
* @return $this |
265
|
|
|
*/ |
266
|
|
|
public function setTimestamp(DateTime $timestamp) |
267
|
|
|
{ |
268
|
|
|
$this->timestamp = $timestamp; |
269
|
|
|
|
270
|
|
|
return $this; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Set the title to use for the attachment. |
275
|
|
|
* |
276
|
|
|
* @param string $title |
277
|
|
|
* |
278
|
|
|
* @return $this |
279
|
|
|
*/ |
280
|
|
|
public function setTitle(string $title) |
281
|
|
|
{ |
282
|
|
|
$this->title = $title; |
283
|
|
|
|
284
|
|
|
return $this; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Set the title link to use for the attachment. |
289
|
|
|
* |
290
|
|
|
* @param string $titleLink |
291
|
|
|
* |
292
|
|
|
* @return $this |
293
|
|
|
*/ |
294
|
|
|
public function setTitleLink(string $titleLink) |
295
|
|
|
{ |
296
|
|
|
$this->titleLink = $titleLink; |
297
|
|
|
|
298
|
|
|
return $this; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Set the author name to use for the attachment. |
303
|
|
|
* |
304
|
|
|
* @param string $authorName |
305
|
|
|
* |
306
|
|
|
* @return $this |
307
|
|
|
*/ |
308
|
|
|
public function setAuthorName(string $authorName) |
309
|
|
|
{ |
310
|
|
|
$this->authorName = $authorName; |
311
|
|
|
|
312
|
|
|
return $this; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Set the auhtor link to use for the attachment. |
317
|
|
|
* |
318
|
|
|
* @param string $authorLink |
319
|
|
|
* |
320
|
|
|
* @return $this |
321
|
|
|
*/ |
322
|
|
|
public function setAuthorLink(string $authorLink) |
323
|
|
|
{ |
324
|
|
|
$this->authorLink = $authorLink; |
325
|
|
|
|
326
|
|
|
return $this; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Set the author icon to use for the attachment. |
331
|
|
|
* |
332
|
|
|
* @param string $authorIcon |
333
|
|
|
* |
334
|
|
|
* @return $this |
335
|
|
|
*/ |
336
|
|
|
public function setAuthorIcon(string $authorIcon) |
337
|
|
|
{ |
338
|
|
|
$this->authorIcon = $authorIcon; |
339
|
|
|
|
340
|
|
|
return $this; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Set the callback id to use for the attachment. |
345
|
|
|
* |
346
|
|
|
* @param string $callbackId |
347
|
|
|
* |
348
|
|
|
* @return $this |
349
|
|
|
*/ |
350
|
|
|
public function setCallbackId(string $callbackId) |
351
|
|
|
{ |
352
|
|
|
$this->callbackId = $callbackId; |
353
|
|
|
|
354
|
|
|
return $this; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Add a field to the attachment. |
359
|
|
|
* |
360
|
|
|
* @param \Spatie\SlashCommand\AttachmentField|array $field |
361
|
|
|
* |
362
|
|
|
* @return $this |
363
|
|
|
* |
364
|
|
|
* @throws \Spatie\SlashCommand\Exceptions\FieldCannotBeAdded |
365
|
|
|
*/ |
366
|
|
|
public function addField($field) |
367
|
|
|
{ |
368
|
|
|
if (! is_array($field) && ! $field instanceof AttachmentField) { |
369
|
|
|
throw FieldCannotBeAdded::invalidType(); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
if (is_array($field)) { |
373
|
|
|
$title = array_keys($field)[0]; |
374
|
|
|
$value = $field[$title]; |
375
|
|
|
|
376
|
|
|
$field = AttachmentField::create($title, $value); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
$this->fields->push($field); |
380
|
|
|
|
381
|
|
|
return $this; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Add the fields for the attachment. |
386
|
|
|
* |
387
|
|
|
* @param array $fields |
388
|
|
|
* |
389
|
|
|
* @return $this |
390
|
|
|
*/ |
391
|
|
|
public function addFields(array $fields) |
392
|
|
|
{ |
393
|
|
|
collect($fields)->each(function ($field, $key) { |
394
|
|
|
if (! $field instanceof AttachmentField) { |
395
|
|
|
$field = [$key => $field]; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
$this->addField($field); |
399
|
|
|
}); |
400
|
|
|
|
401
|
|
|
return $this; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Set the fields for the attachment. |
406
|
|
|
* |
407
|
|
|
* @param array $fields |
408
|
|
|
* |
409
|
|
|
* @return $this |
410
|
|
|
*/ |
411
|
|
|
public function setFields(array $fields) |
412
|
|
|
{ |
413
|
|
|
$this->clearFields(); |
414
|
|
|
|
415
|
|
|
$this->addFields($fields); |
416
|
|
|
|
417
|
|
|
return $this; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Clear all fields for this attachment. |
422
|
|
|
* |
423
|
|
|
* @return $this |
424
|
|
|
*/ |
425
|
|
|
public function clearFields() |
426
|
|
|
{ |
427
|
|
|
$this->fields = new Collection(); |
428
|
|
|
|
429
|
|
|
return $this; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* @param \Spatie\SlashCommand\AttachmentAction|array $action |
434
|
|
|
* |
435
|
|
|
* @return $this |
436
|
|
|
* @throws \Spatie\SlashCommand\Exceptions\ActionCannotBeAdded |
437
|
|
|
*/ |
438
|
|
|
public function addAction($action) |
439
|
|
|
{ |
440
|
|
|
if (! is_array($action) && ! $action instanceof AttachmentAction) { |
441
|
|
|
throw ActionCannotBeAdded::invalidType(); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
if (is_array($action)) { |
445
|
|
|
$name = $action['name']; |
446
|
|
|
$text = $action['text']; |
447
|
|
|
$type = $action['type']; |
448
|
|
|
|
449
|
|
|
$action = AttachmentAction::create($name, $text, $type); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
$this->actions->push($action); |
453
|
|
|
|
454
|
|
|
return $this; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* Add the actions for the attachment. |
459
|
|
|
* |
460
|
|
|
* @param array $actions |
461
|
|
|
* |
462
|
|
|
* @return $this |
463
|
|
|
*/ |
464
|
|
|
public function addActions(array $actions) |
465
|
|
|
{ |
466
|
|
|
collect($actions)->each(function ($action) { |
467
|
|
|
$this->addAction($action); |
468
|
|
|
}); |
469
|
|
|
|
470
|
|
|
return $this; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
/** |
474
|
|
|
* Set the actions for the attachment. |
475
|
|
|
* |
476
|
|
|
* @param array $actions |
477
|
|
|
* |
478
|
|
|
* @return $this |
479
|
|
|
*/ |
480
|
|
|
public function setActions(array $actions) |
481
|
|
|
{ |
482
|
|
|
$this->clearActions(); |
483
|
|
|
|
484
|
|
|
$this->addActions($actions); |
485
|
|
|
|
486
|
|
|
return $this; |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
/** |
490
|
|
|
* Clear all actions for this attachment. |
491
|
|
|
* |
492
|
|
|
* @return $this |
493
|
|
|
*/ |
494
|
|
|
public function clearActions() |
495
|
|
|
{ |
496
|
|
|
$this->actions = new Collection(); |
497
|
|
|
|
498
|
|
|
return $this; |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* Convert this attachment to its array representation. |
503
|
|
|
* |
504
|
|
|
* @return array |
505
|
|
|
*/ |
506
|
|
|
public function toArray() |
507
|
|
|
{ |
508
|
|
|
return [ |
509
|
|
|
'fallback' => $this->fallback, |
510
|
|
|
'text' => $this->text, |
511
|
|
|
'pretext' => $this->preText, |
512
|
|
|
'color' => $this->color, |
513
|
|
|
'footer' => $this->footer, |
514
|
|
|
'footer_icon' => $this->footer, |
515
|
|
|
'ts' => $this->timestamp ? $this->timestamp->getTimestamp() : null, |
516
|
|
|
'image_url' => $this->imageUrl, |
517
|
|
|
'thumb_url' => $this->thumbUrl, |
518
|
|
|
'title' => $this->title, |
519
|
|
|
'title_link' => $this->titleLink, |
520
|
|
|
'author_name' => $this->authorName, |
521
|
|
|
'author_link' => $this->authorLink, |
522
|
|
|
'author_icon' => $this->authorIcon, |
523
|
|
|
'callback_id' => $this->callbackId, |
524
|
|
|
'fields' => $this->fields->map(function (AttachmentField $field) { |
525
|
|
|
return $field->toArray(); |
526
|
|
|
})->toArray(), |
527
|
|
|
'actions' => $this->actions->map(function (AttachmentAction $action) { |
528
|
|
|
return $action->toArray(); |
529
|
|
|
})->toArray(), |
530
|
|
|
]; |
531
|
|
|
} |
532
|
|
|
} |
533
|
|
|
|