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
|
|
|
namespace LineMob\Core\Template; |
13
|
|
|
|
14
|
|
|
use LINE\LINEBot\MessageBuilder\TemplateBuilder\ButtonTemplateBuilder; |
15
|
|
|
use LINE\LINEBot\MessageBuilder\TemplateMessageBuilder; |
16
|
|
|
use LINE\LINEBot\TemplateActionBuilder\MessageTemplateActionBuilder; |
17
|
|
|
use LINE\LINEBot\TemplateActionBuilder\PostbackTemplateActionBuilder; |
18
|
|
|
use LINE\LINEBot\TemplateActionBuilder\UriTemplateActionBuilder; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author YokYukYik <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ButtonsTemplate extends AbstractTemplate |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $altText = 'This is buttons template.'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $title = 'Menu'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
public $text = 'Please select'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
public $thumbnail; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Action[] |
47
|
|
|
*/ |
48
|
|
|
public $actions = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function getTemplate() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$actions = []; |
56
|
|
|
|
57
|
|
|
foreach ($this->actions as $action) { |
58
|
|
|
switch (strtolower($action->type)) { |
59
|
|
|
case Action::TYPE_POSTBACK: |
60
|
|
|
$actions[] = new PostbackTemplateActionBuilder($action->label, $action->value); |
61
|
|
|
break; |
62
|
|
|
case Action::TYPE_URI: |
63
|
|
|
$actions[] = new UriTemplateActionBuilder($action->label, $action->value); |
64
|
|
|
break; |
65
|
|
|
default: |
66
|
|
|
$actions[] = new MessageTemplateActionBuilder($action->label, $action->value);; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return new TemplateMessageBuilder( |
71
|
|
|
$this->altText, |
72
|
|
|
new ButtonTemplateBuilder($this->title, $this->text, $this->thumbnail, $actions) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $text |
78
|
|
|
* @param $value |
79
|
|
|
* @param $type |
80
|
|
|
*/ |
81
|
|
|
public function addAction($text, $value, $type = Action::TYPE_MESSAGE) |
82
|
|
|
{ |
83
|
|
|
$this->actions[] = new Action($text, $value, $type); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.