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

ListTemplate::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 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 ListTemplate extends Template
10
{
11
    public const TOP_ELEMENT_STYLE_LARGE = 'large';
12
13
    public const TOP_ELEMENT_STYLE_COMPACT = 'compact';
14
15
    /**
16
     * @var string
17
     */
18
    protected $topElementStyle;
19
20
    /**
21
     * @var \Kerox\Messenger\Model\Message\Attachment\Template\Element\ListeElement[]
22
     */
23
    protected $elements = [];
24
25
    /**
26
     * @var \Kerox\Messenger\Model\Common\Button\AbstractButton[]
27
     */
28
    protected $buttons = [];
29
30
    /**
31
     * Liste constructor.
32
     *
33
     * @param \Kerox\Messenger\Model\Message\Attachment\Template\Element\ListeElement[] $elements
34
     *
35
     * @throws \InvalidArgumentException
36
     */
37
    public function __construct(array $elements)
38
    {
39
        parent::__construct();
40
41
        $this->isValidArray($elements, 4, 2);
42
43
        $this->elements = $elements;
44
    }
45
46
    /**
47
     * @param array $elements
48
     *
49
     * @throws \InvalidArgumentException
50
     *
51
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\ListTemplate
52
     */
53
    public static function create(array $elements): self
54
    {
55
        return new self($elements);
56
    }
57
58
    /**
59
     * @param string $topElementStyle
60
     *
61
     * @throws \InvalidArgumentException
62
     *
63
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\ListTemplate
64
     */
65
    public function setTopElementStyle(string $topElementStyle): self
66
    {
67
        $this->isValidTopElementStyle($topElementStyle);
68
69
        $this->topElementStyle = $topElementStyle;
70
71
        return $this;
72
    }
73
74
    /**
75
     * @param \Kerox\Messenger\Model\Common\Button\AbstractButton[] $buttons
76
     *
77
     * @throws \InvalidArgumentException
78
     *
79
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\ListTemplate
80
     */
81
    public function setButtons(array $buttons): self
82
    {
83
        $this->isValidArray($buttons, 1);
84
85
        $this->buttons = $buttons;
86
87
        return $this;
88
    }
89
90
    /**
91
     * @param string $topElementStyle
92
     *
93
     * @throws \InvalidArgumentException
94
     */
95
    private function isValidTopElementStyle(string $topElementStyle): void
96
    {
97
        $allowedTopElementStyle = $this->getAllowedTopElementStyle();
98
        if (!\in_array($topElementStyle, $allowedTopElementStyle, true)) {
99
            throw new \InvalidArgumentException(
100
                '$topElementStyle must be either ' . implode(', ', $allowedTopElementStyle)
101
            );
102
        }
103
    }
104
105
    /**
106
     * @return array
107
     */
108
    private function getAllowedTopElementStyle(): array
109
    {
110
        return [
111
            self::TOP_ELEMENT_STYLE_LARGE,
112
            self::TOP_ELEMENT_STYLE_COMPACT,
113
        ];
114
    }
115
116
    /**
117
     * @return array
118
     */
119
    public function toArray(): array
120
    {
121
        $array = parent::toArray();
122
        $array += [
123
            'payload' => [
124
                'template_type'     => Template::TYPE_LIST,
125
                'top_element_style' => $this->topElementStyle,
126
                'elements'          => $this->elements,
127
                'buttons'           => $this->buttons,
128
            ],
129
        ];
130
131
        return $this->arrayFilter($array);
132
    }
133
}
134