ListTemplate::setTopElementStyle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model\Message\Attachment\Template;
6
7
use Kerox\Messenger\Exception\InvalidKeyException;
8
use Kerox\Messenger\Model\Message\Attachment\AbstractTemplate;
9
10
/**
11
 * @deprecated Since version 3.2.0 and will be removed in version 4.0.0.
12
 */
13
class ListTemplate extends AbstractTemplate
14
{
15
    public const TOP_ELEMENT_STYLE_LARGE = 'large';
16
    public const TOP_ELEMENT_STYLE_COMPACT = 'compact';
17
18
    /**
19
     * @var string
20
     */
21
    protected $topElementStyle;
22
23
    /**
24
     * @var \Kerox\Messenger\Model\Message\Attachment\Template\Element\ListElement[]
25
     */
26
    protected $elements = [];
27
28
    /**
29
     * @var \Kerox\Messenger\Model\Common\Button\AbstractButton[]
30
     */
31
    protected $buttons = [];
32
33
    /**
34
     * Liste constructor.
35
     *
36
     * @param \Kerox\Messenger\Model\Message\Attachment\Template\Element\ListElement[] $elements
37
     *
38
     * @throws \Kerox\Messenger\Exception\MessengerException
39
     */
40 2
    public function __construct(array $elements)
41
    {
42 2
        parent::__construct();
43
44 2
        $this->isValidArray($elements, 4, 2);
45
46 2
        $this->elements = $elements;
47 2
    }
48
49
    /**
50
     *@throws \Kerox\Messenger\Exception\MessengerException
51
     *
52
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\ListTemplate
53
     */
54 2
    public static function create(array $elements): self
55
    {
56 2
        return new self($elements);
0 ignored issues
show
Deprecated Code introduced by
The class Kerox\Messenger\Model\Me...t\Template\ListTemplate has been deprecated with message: Since version 3.2.0 and will be removed in version 4.0.0.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
57
    }
58
59
    /**
60
     *@throws \Kerox\Messenger\Exception\MessengerException
61
     *
62
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\ListTemplate
63
     */
64 2
    public function setTopElementStyle(string $topElementStyle): self
65
    {
66 2
        $this->isValidTopElementStyle($topElementStyle);
67
68 1
        $this->topElementStyle = $topElementStyle;
69
70 1
        return $this;
71
    }
72
73
    /**
74
     * @param \Kerox\Messenger\Model\Common\Button\AbstractButton[] $buttons
75
     *
76
     *@throws \Kerox\Messenger\Exception\MessengerException
77
     *
78
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\ListTemplate
79
     */
80 1
    public function setButtons(array $buttons): self
81
    {
82 1
        $this->isValidArray($buttons, 1);
83
84 1
        $this->buttons = $buttons;
85
86 1
        return $this;
87
    }
88
89
    /**
90
     * @throws \Kerox\Messenger\Exception\MessengerException
91
     */
92 2
    private function isValidTopElementStyle(string $topElementStyle): void
93
    {
94 2
        $allowedTopElementStyle = $this->getAllowedTopElementStyle();
95 2
        if (!\in_array($topElementStyle, $allowedTopElementStyle, true)) {
96 1
            throw new InvalidKeyException(sprintf('topElementStyle must be either "%s".', implode(', ', $allowedTopElementStyle)));
97
        }
98 1
    }
99
100 2
    private function getAllowedTopElementStyle(): array
101
    {
102
        return [
103 2
            self::TOP_ELEMENT_STYLE_LARGE,
104 2
            self::TOP_ELEMENT_STYLE_COMPACT,
105
        ];
106
    }
107
108 1
    public function toArray(): array
109
    {
110 1
        $array = parent::toArray();
111
        $array += [
112
            'payload' => [
113 1
                'template_type' => AbstractTemplate::TYPE_LIST,
114 1
                'top_element_style' => $this->topElementStyle,
115 1
                'elements' => $this->elements,
116 1
                'buttons' => $this->buttons,
117
            ],
118
        ];
119
120 1
        return $this->arrayFilter($array);
121
    }
122
}
123