Completed
Branch develop (a0a623)
by Romain
02:33
created

Button::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
namespace Kerox\Messenger\Message\Attachment\Template;
3
4
use Kerox\Messenger\Message\Attachment\Template;
5
6
class Button extends Template
7
{
8
9
    /**
10
     * @var string
11
     */
12
    protected $text;
13
14
    /**
15
     * @var \Kerox\Messenger\Message\Attachment\Template\Buttons\AbstractButtons[]
16
     */
17
    protected $buttons;
18
19
    /**
20
     * Buttons constructor.
21
     *
22
     * @param string $text
23
     * @param \Kerox\Messenger\Message\Attachment\Template\Buttons\AbstractButtons[] $buttons
24
     */
25
    public function __construct(string $text, array $buttons)
26
    {
27
        parent::__construct();
28
29
        $this->isValidString($text, 320);
30
        $this->isValidArray($buttons, 3);
31
32
        $this->text = $text;
33
        $this->buttons = $buttons;
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function jsonSerialize(): array
40
    {
41
        $payload = [
42
            'template_type' => Template::TYPE_BUTTON,
43
            'text' => $this->text,
44
            'buttons' => $this->buttons,
45
        ];
46
47
        $json = parent::jsonSerialize();
48
        $json += [
49
            'payload' => array_filter($payload)
50
        ];
51
52
        return $json;
53
    }
54
}
55