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