Passed
Push — master ( 38d516...3306f1 )
by Alexander
02:07
created

Attachment::getFieldClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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