Completed
Push — master ( eb575e...c103e9 )
by Romain
13s
created

ButtonTemplate   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 59
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A create() 0 3 1
A toArray() 0 12 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model\Message\Attachment\Template;
6
7
use Kerox\Messenger\Model\Message\Attachment\Template;
8
9
class ButtonTemplate extends Template
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $text;
15
16
    /**
17
     * @var \Kerox\Messenger\Model\Common\Button\AbstractButton[]
18
     */
19
    protected $buttons;
20
21
    /**
22
     * Buttons constructor.
23
     *
24
     * @param string                                                $text
25
     * @param \Kerox\Messenger\Model\Common\Button\AbstractButton[] $buttons
26
     *
27
     * @throws \InvalidArgumentException
28
     */
29
    public function __construct(string $text, array $buttons)
30
    {
31
        parent::__construct();
32
33
        $this->isValidString($text, 320);
34
        $this->isValidArray($buttons, 3);
35
36
        $this->text = $text;
37
        $this->buttons = $buttons;
38
    }
39
40
    /**
41
     * @param string $text
42
     * @param array  $buttons
43
     *
44
     * @throws \InvalidArgumentException
45
     *
46
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\ButtonTemplate
47
     */
48
    public static function create(string $text, array $buttons): self
49
    {
50
        return new self($text, $buttons);
51
    }
52
53
    /**
54
     * @return array
55
     */
56
    public function toArray(): array
57
    {
58
        $array = parent::toArray();
59
        $array += [
60
            'payload' => [
61
                'template_type' => Template::TYPE_BUTTON,
62
                'text'          => $this->text,
63
                'buttons'       => $this->buttons,
64
            ],
65
        ];
66
67
        return $this->arrayFilter($array);
68
    }
69
}
70