-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathElementList.php
More file actions
99 lines (83 loc) · 2.43 KB
/
Copy pathElementList.php
File metadata and controls
99 lines (83 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace Tgallice\FBMessenger\Model\Attachment\Template;
use Tgallice\FBMessenger\Model\Attachment\Template;
use Tgallice\FBMessenger\Model\Attachment\Template\ElementList\Element;
use Tgallice\FBMessenger\Model\Button as ButtonModel;
class ElementList extends Template
{
const TOP_STYLE_COMPACT = 'compact';
const TOP_STYLE_LARGE = 'large';
/**
* @var Element[]
*/
private $elements;
/**
* @var ButtonModel|null
*/
private $button;
/**
* @var string
*/
private $topElementStyle;
/**
* @param Element[] $elements
* @param ButtonModel|null $button
* @param string $topElementStyle
*/
public function __construct(array $elements, ?ButtonModel $button = null, $topElementStyle = self::TOP_STYLE_LARGE)
{
if (empty($elements) || (count($elements) < 2 || count($elements) > 4)) {
throw new \InvalidArgumentException('You must provide between 2 and 4 elements.');
}
$topElementStyle = strtolower($topElementStyle);
if (!in_array($topElementStyle, [self::TOP_STYLE_LARGE, self::TOP_STYLE_COMPACT])) {
throw new \InvalidArgumentException(sprintf('"%s" is not a valid top element style.', $topElementStyle));
}
if ($topElementStyle === self::TOP_STYLE_LARGE && empty($elements[0]->getImageUrl())) {
throw new \InvalidArgumentException(sprintf('If the top element style is "%s" your first element must have an image url', self::TOP_STYLE_LARGE));
}
$this->elements = $elements;
$this->button = $button;
$this->topElementStyle = $topElementStyle;
}
/**
* @return Element[]
*/
public function getElements()
{
return $this->elements;
}
/**
* @return string
*/
public function getTopElementStyle()
{
return $this->topElementStyle;
}
/**
* @return null|ButtonModel
*/
public function getButton()
{
return $this->button;
}
/**
* @return string
*/
public function getType()
{
return Template::TYPE_LIST;
}
/**
* @inheritdoc
*/
public function jsonSerialize()
{
return [
'template_type' => $this->getType(),
'elements' => $this->elements,
'buttons' => $this->button ? [$this->button] : null,
'top_element_style' => $this->topElementStyle,
];
}
}