Completed
Pull Request — master (#53)
by Romain
03:15
created

Nested::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
1
<?php
2
3
namespace Kerox\Messenger\Model\Common\Button;
4
5
class Nested extends AbstractButton
6
{
7
8
    /**
9
     * @var string
10
     */
11
    protected $title;
12
13
    /**
14
     * @var \Kerox\Messenger\Model\Common\Button\AbstractButton[]
15
     */
16
    protected $buttons;
17
18
    /**
19
     * Nested constructor.
20
     *
21
     * @param string $title
22
     * @param \Kerox\Messenger\Model\Common\Button\AbstractButton[] $buttons
23
     */
24
    public function __construct(string $title, array $buttons)
25
    {
26
        parent::__construct(self::TYPE_NESTED);
27
28
        $this->isValidString($title, 20);
29
        $this->isValidArray($buttons, 5);
30
        $this->isValidButtons($buttons, $this->getAllowedButtonsType());
31
32
        $this->title = $title;
33
        $this->buttons = $buttons;
34
    }
35
36
    /**
37
     * @param \Kerox\Messenger\Model\Common\Button\AbstractButton $button
38
     * @return \Kerox\Messenger\Model\Common\Button\Nested
39
     */
40
    public function addButton(AbstractButton $button): Nested
41
    {
42
        $this->isValidButtons([$button], $this->getAllowedButtonsType());
43
44
        $this->buttons[] = $button;
45
46
        return $this;
47
    }
48
49
    /**
50
     * @return array
51
     */
52
    protected function getAllowedButtonsType(): array
53
    {
54
        return [
55
            AbstractButton::TYPE_WEB_URL,
56
            AbstractButton::TYPE_POSTBACK,
57
            AbstractButton::TYPE_NESTED,
58
        ];
59
    }
60
61
    /**
62
     * @return array
63
     */
64
    public function jsonSerialize(): array
65
    {
66
        $json = parent::jsonSerialize();
67
        $json += [
68
            'title' => $this->title,
69
            'call_to_actions' => $this->buttons,
70
        ];
71
72
        return $json;
73
    }
74
}
75