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