Attachment::toArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 19
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 24
rs 9.6333
1
<?php
2
3
namespace Eloquentcoder\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 text to use for the attachment footer.
88
     *
89
     * @var string
90
     */
91
    protected $footer;
92
93
    /**
94
     * The icon to use for the attachment footer.
95
     *
96
     * @var string
97
     */
98
    protected $footer_icon;
99
100
    /**
101
     * The timestamp to use for the attachment.
102
     *
103
     * @var \DateTime
104
     */
105
    protected $timestamp;
106
107
    /**
108
     * The fields of the attachment.
109
     *
110
     * @var array
111
     */
112
    protected $fields = [];
113
114
    /**
115
     * The fields of the attachment that Slack should interpret
116
     * with its Markdown-like language.
117
     *
118
     * @var array
119
     */
120
    protected $markdown_fields = [];
121
122
    /**
123
     * A collection of actions (buttons) to include in the attachment.
124
     * A maximum of 5 actions may be provided.
125
     *
126
     * @var array
127
     */
128
    protected $actions = [];
129
130
    /**
131
     * Instantiate a new Attachment.
132
     *
133
     * @param array $attributes
134
     *
135
     * @return void
136
     */
137
    public function __construct(array $attributes)
138
    {
139
        if (isset($attributes['fallback'])) {
140
            $this->setFallback($attributes['fallback']);
141
        }
142
143
        if (isset($attributes['text'])) {
144
            $this->setText($attributes['text']);
145
        }
146
147
        if (isset($attributes['image_url'])) {
148
            $this->setImageUrl($attributes['image_url']);
149
        }
150
151
        if (isset($attributes['thumb_url'])) {
152
            $this->setThumbUrl($attributes['thumb_url']);
153
        }
154
155
        if (isset($attributes['pretext'])) {
156
            $this->setPretext($attributes['pretext']);
157
        }
158
159
        if (isset($attributes['color'])) {
160
            $this->setColor($attributes['color']);
161
        }
162
163
        if (isset($attributes['footer'])) {
164
            $this->setFooter($attributes['footer']);
165
        }
166
167
        if (isset($attributes['footer_icon'])) {
168
            $this->setFooterIcon($attributes['footer_icon']);
169
        }
170
171
        if (isset($attributes['timestamp'])) {
172
            $this->setTimestamp($attributes['timestamp']);
173
        }
174
175
        if (isset($attributes['fields'])) {
176
            $this->setFields($attributes['fields']);
177
        }
178
179
        if (isset($attributes['mrkdwn_in'])) {
180
            $this->setMarkdownFields($attributes['mrkdwn_in']);
181
        }
182
183
        if (isset($attributes['title'])) {
184
            $this->setTitle($attributes['title']);
185
        }
186
187
        if (isset($attributes['title_link'])) {
188
            $this->setTitleLink($attributes['title_link']);
189
        }
190
191
        if (isset($attributes['author_name'])) {
192
            $this->setAuthorName($attributes['author_name']);
193
        }
194
195
        if (isset($attributes['author_link'])) {
196
            $this->setAuthorLink($attributes['author_link']);
197
        }
198
199
        if (isset($attributes['author_icon'])) {
200
            $this->setAuthorIcon($attributes['author_icon']);
201
        }
202
203
        if (isset($attributes['actions'])) {
204
            $this->setActions($attributes['actions']);
205
        }
206
    }
207
208
    /**
209
     * Get the fallback text.
210
     *
211
     * @return string
212
     */
213
    public function getFallback()
214
    {
215
        return $this->fallback;
216
    }
217
218
    /**
219
     * Set the fallback text.
220
     *
221
     * @param string $fallback
222
     *
223
     * @return $this
224
     */
225
    public function setFallback($fallback)
226
    {
227
        $this->fallback = $fallback;
228
229
        return $this;
230
    }
231
232
    /**
233
     * Get the optional text to appear within the attachment.
234
     *
235
     * @return string
236
     */
237
    public function getText()
238
    {
239
        return $this->text;
240
    }
241
242
    /**
243
     * Set the optional text to appear within the attachment.
244
     *
245
     * @param string $text
246
     *
247
     * @return $this
248
     */
249
    public function setText($text)
250
    {
251
        $this->text = $text;
252
253
        return $this;
254
    }
255
256
    /**
257
     * Get the optional image to appear within the attachment.
258
     *
259
     * @return string
260
     */
261
    public function getImageUrl()
262
    {
263
        return $this->image_url;
264
    }
265
266
    /**
267
     * Set the optional image to appear within the attachment.
268
     *
269
     * @param string $image_url
270
     *
271
     * @return $this
272
     */
273
    public function setImageUrl($image_url)
274
    {
275
        $this->image_url = $image_url;
276
277
        return $this;
278
    }
279
280
    /**
281
     * Get the optional thumbnail to appear within the attachment.
282
     *
283
     * @return string
284
     */
285
    public function getThumbUrl()
286
    {
287
        return $this->thumb_url;
288
    }
289
290
    /**
291
     * Set the optional thumbnail to appear within the attachment.
292
     *
293
     * @param string $thumb_url
294
     *
295
     * @return $this
296
     */
297
    public function setThumbUrl($thumb_url)
298
    {
299
        $this->thumb_url = $thumb_url;
300
301
        return $this;
302
    }
303
304
    /**
305
     * Get the text that should appear above the formatted data.
306
     *
307
     * @return string
308
     */
309
    public function getPretext()
310
    {
311
        return $this->pretext;
312
    }
313
314
    /**
315
     * Set the text that should appear above the formatted data.
316
     *
317
     * @param string $pretext
318
     *
319
     * @return $this
320
     */
321
    public function setPretext($pretext)
322
    {
323
        $this->pretext = $pretext;
324
325
        return $this;
326
    }
327
328
    /**
329
     * Get the color to use for the attachment.
330
     *
331
     * @return string
332
     */
333
    public function getColor()
334
    {
335
        return $this->color;
336
    }
337
338
    /**
339
     * Set the color to use for the attachment.
340
     *
341
     * @param string $color
342
     *
343
     * @return $this
344
     */
345
    public function setColor($color)
346
    {
347
        $this->color = $color;
348
349
        return $this;
350
    }
351
352
    /**
353
     * Get the footer to use for the attachment.
354
     *
355
     * @return string
356
     */
357
    public function getFooter()
358
    {
359
        return $this->footer;
360
    }
361
362
    /**
363
     * Set the footer text to use for the attachment.
364
     *
365
     * @param string $footer
366
     *
367
     * @return $this
368
     */
369
    public function setFooter($footer)
370
    {
371
        $this->footer = $footer;
372
373
        return $this;
374
    }
375
376
    /**
377
     * Get the footer icon to use for the attachment.
378
     *
379
     * @return string
380
     */
381
    public function getFooterIcon()
382
    {
383
        return $this->footer_icon;
384
    }
385
386
    /**
387
     * Set the footer icon to use for the attachment.
388
     *
389
     * @param string $footerIcon
390
     *
391
     * @return $this
392
     */
393
    public function setFooterIcon($footerIcon)
394
    {
395
        $this->footer_icon = $footerIcon;
396
397
        return $this;
398
    }
399
400
    /**
401
     * Get the timestamp to use for the attachment.
402
     *
403
     * @return \DateTime
404
     */
405
    public function getTimestamp()
406
    {
407
        return $this->timestamp;
408
    }
409
410
    /**
411
     * Set the timestamp to use for the attachment.
412
     *
413
     * @param \DateTime $timestamp
414
     *
415
     * @return $this
416
     */
417
    public function setTimestamp($timestamp)
418
    {
419
        $this->timestamp = $timestamp;
420
421
        return $this;
422
    }
423
424
    /**
425
     * Get the title to use for the attachment.
426
     *
427
     * @return string
428
     */
429
    public function getTitle()
430
    {
431
        return $this->title;
432
    }
433
434
    /**
435
     * Set the title to use for the attachment.
436
     *
437
     * @param string $title
438
     *
439
     * @return $this
440
     */
441
    public function setTitle($title)
442
    {
443
        $this->title = $title;
444
445
        return $this;
446
    }
447
448
    /**
449
     * Get the title link to use for the attachment.
450
     *
451
     * @return string
452
     */
453
    public function getTitleLink()
454
    {
455
        return $this->title_link;
456
    }
457
458
    /**
459
     * Set the title link to use for the attachment.
460
     *
461
     * @param string $title_link
462
     *
463
     * @return $this
464
     */
465
    public function setTitleLink($title_link)
466
    {
467
        $this->title_link = $title_link;
468
469
        return $this;
470
    }
471
472
    /**
473
     * Get the author name to use for the attachment.
474
     *
475
     * @return string
476
     */
477
    public function getAuthorName()
478
    {
479
        return $this->author_name;
480
    }
481
482
    /**
483
     * Set the author name to use for the attachment.
484
     *
485
     * @param string $author_name
486
     *
487
     * @return $this
488
     */
489
    public function setAuthorName($author_name)
490
    {
491
        $this->author_name = $author_name;
492
493
        return $this;
494
    }
495
496
    /**
497
     * Get the author link to use for the attachment.
498
     *
499
     * @return string
500
     */
501
    public function getAuthorLink()
502
    {
503
        return $this->author_link;
504
    }
505
506
    /**
507
     * Set the auhtor link to use for the attachment.
508
     *
509
     * @param string $author_link
510
     *
511
     * @return $this
512
     */
513
    public function setAuthorLink($author_link)
514
    {
515
        $this->author_link = $author_link;
516
517
        return $this;
518
    }
519
520
    /**
521
     * Get the author icon to use for the attachment.
522
     *
523
     * @return string
524
     */
525
    public function getAuthorIcon()
526
    {
527
        return $this->author_icon;
528
    }
529
530
    /**
531
     * Set the author icon to use for the attachment.
532
     *
533
     * @param string $author_icon
534
     *
535
     * @return $this
536
     */
537
    public function setAuthorIcon($author_icon)
538
    {
539
        $this->author_icon = $author_icon;
540
541
        return $this;
542
    }
543
544
    /**
545
     * Get the fields for the attachment.
546
     *
547
     * @return array
548
     */
549
    public function getFields()
550
    {
551
        return $this->fields;
552
    }
553
554
    /**
555
     * Set the fields for the attachment.
556
     *
557
     * @param array $fields
558
     *
559
     * @return $this
560
     */
561
    public function setFields(array $fields)
562
    {
563
        $this->clearFields();
564
565
        foreach ($fields as $field) {
566
            $this->addField($field);
567
        }
568
569
        return $this;
570
    }
571
572
    /**
573
     * Add a field to the attachment.
574
     *
575
     * @param mixed $field
576
     *
577
     * @return $this
578
     */
579
    public function addField($field)
580
    {
581
        if ($field instanceof AttachmentField) {
582
            $this->fields[] = $field;
583
584
            return $this;
585
        } elseif (is_array($field)) {
586
            $this->fields[] = new AttachmentField($field);
587
588
            return $this;
589
        }
590
591
        throw new InvalidArgumentException('The attachment field must be an instance of Maknz\Slack\AttachmentField or a keyed array');
592
    }
593
594
    /**
595
     * Clear the fields for the attachment.
596
     *
597
     * @return $this
598
     */
599
    public function clearFields()
600
    {
601
        $this->fields = [];
602
603
        return $this;
604
    }
605
606
    /**
607
     * Clear the actions for the attachment.
608
     *
609
     * @return $this
610
     */
611
    public function clearActions()
612
    {
613
        $this->actions = [];
614
615
        return $this;
616
    }
617
618
    /**
619
     * Get the fields Slack should interpret in its
620
     * Markdown-like language.
621
     *
622
     * @return array
623
     */
624
    public function getMarkdownFields()
625
    {
626
        return $this->markdown_fields;
627
    }
628
629
    /**
630
     * Set the fields Slack should interpret in its
631
     * Markdown-like language.
632
     *
633
     * @param array $fields
634
     *
635
     * @return $this
636
     */
637
    public function setMarkdownFields(array $fields)
638
    {
639
        $this->markdown_fields = $fields;
640
641
        return $this;
642
    }
643
644
    /**
645
     * Get the collection of actions (buttons) to include in the attachment.
646
     *
647
     * @return AttachmentAction[]
648
     */
649
    public function getActions()
650
    {
651
        return $this->actions;
652
    }
653
654
    /**
655
     * Set the collection of actions (buttons) to include in the attachment.
656
     *
657
     * @param array $actions
658
     *
659
     * @return Attachment
660
     */
661
    public function setActions($actions)
662
    {
663
        $this->clearActions();
664
665
        foreach ($actions as $action) {
666
            $this->addAction($action);
667
        }
668
669
        return $this;
670
    }
671
672
    /**
673
     * Add an action to the attachment.
674
     *
675
     * @param mixed $action
676
     *
677
     * @return $this
678
     */
679
    public function addAction($action)
680
    {
681
        if ($action instanceof AttachmentAction) {
682
            $this->actions[] = $action;
683
684
            return $this;
685
        } elseif (is_array($action)) {
686
            $this->actions[] = new AttachmentAction($action);
687
688
            return $this;
689
        }
690
691
        throw new InvalidArgumentException('The attachment action must be an instance of Maknz\Slack\AttachmentAction or a keyed array');
692
    }
693
694
    /**
695
     * Convert this attachment to its array representation.
696
     *
697
     * @return array
698
     */
699
    public function toArray()
700
    {
701
        $data = [
702
            'fallback'    => $this->getFallback(),
703
            'text'        => $this->getText(),
704
            'pretext'     => $this->getPretext(),
705
            'color'       => $this->getColor(),
706
            'footer'      => $this->getFooter(),
707
            'footer_icon' => $this->getFooterIcon(),
708
            'ts'          => $this->getTimestamp() ? $this->getTimestamp()->getTimestamp() : null,
709
            'mrkdwn_in'   => $this->getMarkdownFields(),
710
            'image_url'   => $this->getImageUrl(),
711
            'thumb_url'   => $this->getThumbUrl(),
712
            'title'       => $this->getTitle(),
713
            'title_link'  => $this->getTitleLink(),
714
            'author_name' => $this->getAuthorName(),
715
            'author_link' => $this->getAuthorLink(),
716
            'author_icon' => $this->getAuthorIcon(),
717
        ];
718
719
        $data['fields'] = $this->getFieldsAsArrays();
720
        $data['actions'] = $this->getActionsAsArrays();
721
722
        return $data;
723
    }
724
725
    /**
726
     * Iterates over all fields in this attachment and returns
727
     * them in their array form.
728
     *
729
     * @return array
730
     */
731
    protected function getFieldsAsArrays()
732
    {
733
        $fields = [];
734
735
        foreach ($this->getFields() as $field) {
736
            $fields[] = $field->toArray();
737
        }
738
739
        return $fields;
740
    }
741
742
    /**
743
     * Iterates over all actions in this attachment and returns
744
     * them in their array form.
745
     *
746
     * @return array
747
     */
748
    protected function getActionsAsArrays()
749
    {
750
        $actions = [];
751
752
        foreach ($this->getActions() as $action) {
753
            $actions[] = $action->toArray();
754
        }
755
756
        return $actions;
757
    }
758
}
759