|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the LineMob package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Ishmael Doss <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
namespace LineMob\Core\Template\Carousel; |
|
14
|
|
|
|
|
15
|
|
|
use LINE\LINEBot\MessageBuilder\TemplateBuilder\CarouselColumnTemplateBuilder; |
|
16
|
|
|
use LINE\LINEBot\MessageBuilder\TemplateBuilder\CarouselTemplateBuilder; |
|
17
|
|
|
use LINE\LINEBot\MessageBuilder\TemplateMessageBuilder; |
|
18
|
|
|
use LINE\LINEBot\TemplateActionBuilder\MessageTemplateActionBuilder; |
|
19
|
|
|
use LINE\LINEBot\TemplateActionBuilder\PostbackTemplateActionBuilder; |
|
20
|
|
|
use LINE\LINEBot\TemplateActionBuilder\UriTemplateActionBuilder; |
|
21
|
|
|
use LineMob\Core\Template\AbstractTemplate; |
|
22
|
|
|
use LineMob\Core\Template\Action; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @author Ishmael Doss <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class CarouselTemplate extends AbstractTemplate |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var Item[] |
|
31
|
|
|
*/ |
|
32
|
|
|
public $items; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
public $altText = 'This is carousel template.'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param Item $item |
|
41
|
|
|
* |
|
42
|
|
|
* @return Action[] |
|
43
|
|
|
*/ |
|
44
|
|
|
private function createItemActions(Item $item) |
|
45
|
|
|
{ |
|
46
|
|
|
$actions = []; |
|
47
|
|
|
|
|
48
|
|
|
foreach ($item->actions as $action) { |
|
49
|
|
|
switch (strtolower($action->type)) { |
|
50
|
|
|
case Action::TYPE_POSTBACK: |
|
51
|
|
|
$actions[] = new PostbackTemplateActionBuilder($action->label, $action->value); |
|
52
|
|
|
break; |
|
53
|
|
|
case Action::TYPE_URI: |
|
54
|
|
|
$actions[] = new UriTemplateActionBuilder($action->label, $action->value); |
|
55
|
|
|
break; |
|
56
|
|
|
default: |
|
57
|
|
|
$actions[] = new MessageTemplateActionBuilder($action->label, $action->value);; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $actions; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getTemplate() |
|
68
|
|
|
{ |
|
69
|
|
|
$items = []; |
|
70
|
|
|
|
|
71
|
|
|
foreach ($this->items as $item) { |
|
72
|
|
|
$items[] = new CarouselColumnTemplateBuilder( |
|
73
|
|
|
mb_substr($item->title, 0, 40), mb_substr($item->text, 0, 60), |
|
74
|
|
|
$item->thumbnail, |
|
75
|
|
|
$this->createItemActions($item) |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return new TemplateMessageBuilder($this->altText, new CarouselTemplateBuilder($items)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $title |
|
84
|
|
|
* @param string $text |
|
85
|
|
|
* @param string $thumbnail |
|
86
|
|
|
* @param array|Action[] $actions |
|
87
|
|
|
*/ |
|
88
|
|
|
public function addItem($title, $text, $thumbnail, array $actions = []) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->items[] = new Item($title, $text, $thumbnail, $actions); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|