Skip to content

Commit 334ae57

Browse files
authored
Merge pull request #332 from hinoguma/feature/add-default-action-for-carousel-column
add defaultAction for CarouselColumnTemplateBuilder
2 parents aaf2d40 + cbda0a3 commit 334ae57

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

src/LINEBot/MessageBuilder/TemplateBuilder/CarouselColumnTemplateBuilder.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class CarouselColumnTemplateBuilder implements TemplateBuilder
4242
/** @var array */
4343
private $template;
4444

45+
/**
46+
* @var TemplateActionBuilder|null
47+
*/
48+
private $defaultAction;
49+
4550
/**
4651
* CarouselColumnTemplateBuilder constructor.
4752
*
@@ -50,14 +55,22 @@ class CarouselColumnTemplateBuilder implements TemplateBuilder
5055
* @param string $thumbnailImageUrl
5156
* @param TemplateActionBuilder[] $actionBuilders
5257
* @param string|null $imageBackgroundColor
58+
* @param TemplateActionBuilder|null $defaultAction
5359
*/
54-
public function __construct($title, $text, $thumbnailImageUrl, array $actionBuilders, $imageBackgroundColor = null)
55-
{
60+
public function __construct(
61+
$title,
62+
$text,
63+
$thumbnailImageUrl,
64+
array $actionBuilders,
65+
$imageBackgroundColor = null,
66+
TemplateActionBuilder $defaultAction = null
67+
) {
5668
$this->title = $title;
5769
$this->text = $text;
5870
$this->thumbnailImageUrl = $thumbnailImageUrl;
5971
$this->actionBuilders = $actionBuilders;
6072
$this->imageBackgroundColor = $imageBackgroundColor;
73+
$this->defaultAction = $defaultAction;
6174
}
6275

6376
/**
@@ -86,6 +99,9 @@ public function buildTemplate()
8699
if ($this->imageBackgroundColor) {
87100
$this->template['imageBackgroundColor'] = $this->imageBackgroundColor;
88101
}
102+
if ($this->defaultAction) {
103+
$this->template['defaultAction'] = $this->defaultAction->buildTemplateAction();
104+
}
89105

90106
return $this->template;
91107
}

tests/LINEBot/MessageBuilder/TemplateBuilder/CarouselColumnTemplateBuilderTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,51 @@ class CarouselColumnTemplateBuilderTest extends TestCase
7171
],
7272
"imageBackgroundColor":"ddd"
7373
}
74+
JSON
75+
],
76+
[
77+
'param' => ['aaa', 'bbb', 'ccc', ['postback'], 'ddd', 'uri'],
78+
'json' => <<<JSON
79+
{
80+
"thumbnailImageUrl":"ccc",
81+
"title":"aaa",
82+
"text":"bbb",
83+
"actions":[
84+
{"type":"postback","label":"AAA","data":"BBB"}
85+
],
86+
"imageBackgroundColor":"ddd",
87+
"defaultAction":{"type":"uri","label":"EEE","uri":"FFF"}
88+
}
89+
JSON
90+
],
91+
[
92+
'param' => ['aaa', 'bbb', 'ccc', ['postback'], 'ddd', 'message'],
93+
'json' => <<<JSON
94+
{
95+
"thumbnailImageUrl":"ccc",
96+
"title":"aaa",
97+
"text":"bbb",
98+
"actions":[
99+
{"type":"postback","label":"AAA","data":"BBB"}
100+
],
101+
"imageBackgroundColor":"ddd",
102+
"defaultAction":{"type":"message","label":"CCC","text":"DDD"}
103+
}
104+
JSON
105+
],
106+
[
107+
'param' => ['aaa', 'bbb', 'ccc', ['postback'], 'ddd', 'postback'],
108+
'json' => <<<JSON
109+
{
110+
"thumbnailImageUrl":"ccc",
111+
"title":"aaa",
112+
"text":"bbb",
113+
"actions":[
114+
{"type":"postback","label":"AAA","data":"BBB"}
115+
],
116+
"imageBackgroundColor":"ddd",
117+
"defaultAction":{"type":"postback","label":"AAA","data":"BBB"}
118+
}
74119
JSON
75120
],
76121
];
@@ -100,6 +145,20 @@ public function test()
100145
$actionBuilders = null;
101146
}
102147
$imageBackgroundColor = isset($t['param'][4]) ? $t['param'][4] : null;
148+
$defaultAction = null;
149+
if (isset($t['param'][5])) {
150+
switch ($t['param'][5]) {
151+
case 'postback':
152+
$defaultAction = $postbackActionBuilder;
153+
break;
154+
case 'message':
155+
$defaultAction = $messageTemplateActionBuilder;
156+
break;
157+
case 'uri':
158+
$defaultAction = $uriTemplateActionBuilder;
159+
break;
160+
}
161+
}
103162

104163
if (count($t['param']) == 5) {
105164
$builder = new CarouselColumnTemplateBuilder(
@@ -109,6 +168,15 @@ public function test()
109168
$actionBuilders,
110169
$imageBackgroundColor
111170
);
171+
} elseif (count($t['param']) == 6) {
172+
$builder = new CarouselColumnTemplateBuilder(
173+
$title,
174+
$text,
175+
$thumbnailImageUrl,
176+
$actionBuilders,
177+
$imageBackgroundColor,
178+
$defaultAction
179+
);
112180
} else {
113181
$builder = new CarouselColumnTemplateBuilder(
114182
$title,

0 commit comments

Comments
 (0)