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