Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Commit

Permalink
Merge pull request #17 from mikepsinn/master
Browse files Browse the repository at this point in the history
Allow multiple buttons in cards
  • Loading branch information
eristemena authored Jan 26, 2019
2 parents 78d0a47 + e21de30 commit a407615
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ reports
composer.lock
.php_cs.cache
coverage.xml
.idea/
32 changes: 16 additions & 16 deletions src/Action/Responses/BasicCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ class BasicCard implements ResponseInterface
/** @var string */
protected $accessibilityText;

/** @var string */
protected $buttonText;

/** @var string */
protected $buttonUrl;
/** @var array */
protected $buttons = [];

/**
* Create a new Basic Card instance.
Expand Down Expand Up @@ -88,8 +85,10 @@ public function image($imageUrl, $accessibilityText = null)
*/
public function button($buttonText, $buttonUrl)
{
$this->buttonText = $buttonText;
$this->buttonUrl = $buttonUrl;
$this->buttons[] = [
'buttonText' => $buttonText,
'buttonUrl' => $buttonUrl,
];

return $this;
}
Expand Down Expand Up @@ -119,15 +118,16 @@ public function renderRichResponseItem()
];
}

if ($this->buttonText && $this->buttonUrl) {
$basicCard['buttons'] = [
[
'title' => $this->buttonText,
'openUrlAction' => [
'url' => $this->buttonUrl,
],
],
];
if ($this->buttons) {
foreach ($this->buttons as $button) {
$basicCard['buttons'][] =
[
'title' => $button['buttonText'],
'openUrlAction' => [
'url' => $button['buttonUrl'],
],
];
}
}

$out['basicCard'] = $basicCard;
Expand Down
85 changes: 46 additions & 39 deletions src/RichMessage/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ class Card extends RichMessage
/** @var string */
protected $imageUrl;

/** @var string */
protected $buttonText;

/** @var string */
protected $buttonUrl;
/** @var array */
protected $buttons = [];

/**
* Create a new Card instance.
Expand Down Expand Up @@ -79,8 +76,10 @@ public function image($imageUrl)
*/
public function button($buttonText, $buttonUrl)
{
$this->buttonText = $buttonText;
$this->buttonUrl = $buttonUrl;
$this->buttons[] = [
'buttonText' => $buttonText,
'buttonUrl' => $buttonUrl,
];

return $this;
}
Expand Down Expand Up @@ -114,15 +113,17 @@ protected function renderV1()
];
}

if ($this->buttonText && $this->buttonUrl) {
$out['buttons'] = [
[
'title' => $this->buttonText,
'openUrlAction' => [
'url' => $this->buttonUrl,
],
],
];
if ($this->buttons) {
$out['buttons'] = [];
foreach ($this->buttons as $button) {
$out['buttons'][] =
[
'title' => $button['buttonText'],
'openUrlAction' => [
'url' => $button['buttonUrl'],
],
];
}
}

return $out;
Expand All @@ -144,13 +145,15 @@ protected function renderV1()
$out['buttons'] = [];
}

if ($this->buttonText && $this->buttonUrl) {
$out['buttons'] = [
[
'text' => $this->buttonText,
'postback' => $this->buttonUrl,
],
];
if ($this->buttons) {
$out['buttons'] = [];
foreach ($this->buttons as $button) {
$out['buttons'][] =
[
'text' => $button['buttonText'],
'postback' => $button['buttonUrl'],
];
}
}

$out['platform'] = $this->requestSource;
Expand Down Expand Up @@ -185,15 +188,17 @@ protected function renderV2()
];
}

if ($this->buttonText && $this->buttonUrl) {
$out['basicCard']['buttons'] = [
[
'title' => $this->buttonText,
'openUriAction' => [
'uri' => $this->buttonUrl,
],
],
];
if ($this->buttons) {
$out['basicCard']['buttons'] = [];
foreach ($this->buttons as $button) {
$out['basicCard']['buttons'][] =
[
'title' => $button['buttonText'],
'openUriAction' => [
'uri' => $button['buttonUrl'],
],
];
}
}

return $out;
Expand All @@ -213,13 +218,15 @@ protected function renderV2()
$out['card']['imageUri'] = $this->imageUrl;
}

if ($this->buttonText && $this->buttonUrl) {
$out['card']['buttons'] = [
[
'text' => $this->buttonText,
'postback' => $this->buttonUrl,
],
];
if ($this->buttons) {
$out['card']['buttons'] = [];
foreach ($this->buttons as $button) {
$out['card']['buttons'][] =
[
'text' => $button['buttonText'],
'postback' => $button['buttonUrl'],
];
}
}

return $out;
Expand Down

0 comments on commit a407615

Please sign in to comment.