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