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
|
|
|
public static function create() |
121
|
|
|
{ |
122
|
|
|
return new static(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function __construct() |
126
|
|
|
{ |
127
|
|
|
$this->fields = new Collection(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set the fallback text. |
132
|
|
|
* |
133
|
|
|
* @param string $fallback |
134
|
|
|
* |
135
|
|
|
* @return $this |
136
|
|
|
*/ |
137
|
|
|
public function setFallback(string $fallback) |
138
|
|
|
{ |
139
|
|
|
$this->fallback = $fallback; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set the optional text to appear within the attachment. |
146
|
|
|
* |
147
|
|
|
* @param string $text |
148
|
|
|
* |
149
|
|
|
* @return $this |
150
|
|
|
*/ |
151
|
|
|
public function setText($text) |
152
|
|
|
{ |
153
|
|
|
$this->text = $text; |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Set the optional image to appear within the attachment. |
160
|
|
|
* |
161
|
|
|
* @param string $imageUrl |
162
|
|
|
* |
163
|
|
|
* @return $this |
164
|
|
|
*/ |
165
|
|
|
public function setImageUrl(string $imageUrl) |
166
|
|
|
{ |
167
|
|
|
$this->imageUrl = $imageUrl; |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Set the optional thumbnail to appear within the attachment. |
174
|
|
|
* |
175
|
|
|
* @param string $thumbUrl |
176
|
|
|
* |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
|
|
public function setThumbUrl(string $thumbUrl) |
180
|
|
|
{ |
181
|
|
|
$this->thumbUrl = $thumbUrl; |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Set the text that should appear above the formatted data. |
188
|
|
|
* |
189
|
|
|
* @param string $preText |
190
|
|
|
* |
191
|
|
|
* @return $this |
192
|
|
|
*/ |
193
|
|
|
public function setPreText(string $preText) |
194
|
|
|
{ |
195
|
|
|
$this->preText = $preText; |
196
|
|
|
|
197
|
|
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Set the color to use for the attachment. |
202
|
|
|
* |
203
|
|
|
* @param string $color |
204
|
|
|
* |
205
|
|
|
* @return $this |
206
|
|
|
*/ |
207
|
|
|
public function setColor($color) |
208
|
|
|
{ |
209
|
|
|
$this->color = $color; |
210
|
|
|
|
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Set the footer text to use for the attachment. |
216
|
|
|
* |
217
|
|
|
* @param string $footer |
218
|
|
|
* |
219
|
|
|
* @return $this |
220
|
|
|
*/ |
221
|
|
|
public function setFooter(string $footer) |
222
|
|
|
{ |
223
|
|
|
$this->footer = $footer; |
224
|
|
|
|
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Set the footer icon to use for the attachment. |
230
|
|
|
* |
231
|
|
|
* @param string $footerIcon |
232
|
|
|
* |
233
|
|
|
* @return $this |
234
|
|
|
*/ |
235
|
|
|
public function setFooterIcon(string $footerIcon) |
236
|
|
|
{ |
237
|
|
|
$this->footerIcon = $footerIcon; |
238
|
|
|
|
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Set the timestamp to use for the attachment. |
244
|
|
|
* |
245
|
|
|
* @param \DateTime $timestamp |
246
|
|
|
* |
247
|
|
|
* @return $this |
248
|
|
|
*/ |
249
|
|
|
public function setTimestamp(DateTime $timestamp) |
250
|
|
|
{ |
251
|
|
|
$this->timestamp = $timestamp; |
252
|
|
|
|
253
|
|
|
return $this; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Set the title to use for the attachment. |
258
|
|
|
* |
259
|
|
|
* @param string $title |
260
|
|
|
* |
261
|
|
|
* @return $this |
262
|
|
|
*/ |
263
|
|
|
public function setTitle(string $title) |
264
|
|
|
{ |
265
|
|
|
$this->title = $title; |
266
|
|
|
|
267
|
|
|
return $this; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Set the title link to use for the attachment. |
272
|
|
|
* |
273
|
|
|
* @param string $titleLink |
274
|
|
|
* |
275
|
|
|
* @return $this |
276
|
|
|
*/ |
277
|
|
|
public function setTitleLink(string $titleLink) |
278
|
|
|
{ |
279
|
|
|
$this->titleLink = $titleLink; |
280
|
|
|
|
281
|
|
|
return $this; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Set the author name to use for the attachment. |
286
|
|
|
* |
287
|
|
|
* @param string $authorName |
288
|
|
|
* |
289
|
|
|
* @return $this |
290
|
|
|
*/ |
291
|
|
|
public function setAuthorName(string $authorName) |
292
|
|
|
{ |
293
|
|
|
$this->authorName = $authorName; |
294
|
|
|
|
295
|
|
|
return $this; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Set the auhtor link to use for the attachment. |
300
|
|
|
* |
301
|
|
|
* @param string $authorLink |
302
|
|
|
* |
303
|
|
|
* @return $this |
304
|
|
|
*/ |
305
|
|
|
public function setAuthorLink(string $authorLink) |
306
|
|
|
{ |
307
|
|
|
$this->authorLink = $authorLink; |
308
|
|
|
|
309
|
|
|
return $this; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Set the author icon to use for the attachment. |
314
|
|
|
* |
315
|
|
|
* @param string $authorIcon |
316
|
|
|
* |
317
|
|
|
* @return $this |
318
|
|
|
*/ |
319
|
|
|
public function setAuthorIcon(string $authorIcon) |
320
|
|
|
{ |
321
|
|
|
$this->authorIcon = $authorIcon; |
322
|
|
|
|
323
|
|
|
return $this; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
|
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Add a field to the attachment. |
330
|
|
|
* |
331
|
|
|
* @param \Spatie\SlashCommand\AttachmentField|array $field |
332
|
|
|
* |
333
|
|
|
* @return $this |
334
|
|
|
* |
335
|
|
|
* @throws \Spatie\SlashCommand\Exceptions\FieldCannotBeAdded |
336
|
|
|
*/ |
337
|
|
|
public function addField($field) |
338
|
|
|
{ |
339
|
|
|
if (! is_array($field) && ! $field instanceof AttachmentField) { |
340
|
|
|
throw FieldCannotBeAdded::invalidType(); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
if (is_array($field)) { |
344
|
|
|
$title = array_keys($field)[0]; |
345
|
|
|
$value = $field[$title]; |
346
|
|
|
|
347
|
|
|
$field = AttachmentField::create($title, $value); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
$this->fields->push($field); |
351
|
|
|
|
352
|
|
|
return $this; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Add the fields for the attachment. |
357
|
|
|
* |
358
|
|
|
* @param array $fields |
359
|
|
|
* |
360
|
|
|
* @return $this |
361
|
|
|
*/ |
362
|
|
|
public function addFields(array $fields) |
363
|
|
|
{ |
364
|
|
|
collect($fields)->each(function ($field) { |
365
|
|
|
$this->addField($field); |
366
|
|
|
}); |
367
|
|
|
|
368
|
|
|
return $this; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Set the fields for the attachment. |
373
|
|
|
* |
374
|
|
|
* @param array $fields |
375
|
|
|
* |
376
|
|
|
* @return $this |
377
|
|
|
*/ |
378
|
|
|
public function setFields(array $fields) |
|
|
|
|
379
|
|
|
{ |
380
|
|
|
$this->clearFields(); |
381
|
|
|
|
382
|
|
|
$this->addFields(); |
|
|
|
|
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Clear all fields for this attachment. |
387
|
|
|
* |
388
|
|
|
* @return $this |
389
|
|
|
*/ |
390
|
|
|
public function clearFields() |
391
|
|
|
{ |
392
|
|
|
$this->fields = new Collection(); |
393
|
|
|
|
394
|
|
|
return $this; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Convert this attachment to its array representation. |
399
|
|
|
* |
400
|
|
|
* @return array |
401
|
|
|
*/ |
402
|
|
|
public function toArray() |
403
|
|
|
{ |
404
|
|
|
return [ |
405
|
|
|
'fallback' => $this->fallback, |
406
|
|
|
'text' => $this->text, |
407
|
|
|
'pretext' => $this->preText, |
408
|
|
|
'color' => $this->color, |
409
|
|
|
'footer' => $this->footer, |
410
|
|
|
'footer_icon' => $this->footer, |
411
|
|
|
'ts' => $this->timestamp ? $this->timestamp->getTimestamp() : null, |
412
|
|
|
'image_url' => $this->imageUrl, |
413
|
|
|
'thumb_url' => $this->thumbUrl, |
414
|
|
|
'title' => $this->title, |
415
|
|
|
'title_link' => $this->titleLink, |
416
|
|
|
'author_name' => $this->authorName, |
417
|
|
|
'author_link' => $this->authorLink, |
418
|
|
|
'author_icon' => $this->authorIcon, |
419
|
|
|
'fields' => $this->fields->map(function (AttachmentField $field) { |
420
|
|
|
return $field->toArray(); |
421
|
|
|
})->toArray(), |
422
|
|
|
]; |
423
|
|
|
} |
424
|
|
|
} |
425
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.