From e399fcd44e81a427da329f979f2e3f69cb564a39 Mon Sep 17 00:00:00 2001 From: "Mike P. Sinn" Date: Fri, 25 Jan 2019 09:28:22 -0600 Subject: [PATCH 1/3] Allow multiple buttons in cards --- .gitignore | 1 + src/Action/Responses/BasicCard.php | 32 +++++------ src/RichMessage/Card.php | 87 ++++++++++++++++-------------- 3 files changed, 65 insertions(+), 55 deletions(-) diff --git a/.gitignore b/.gitignore index f97d724..60707b6 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ reports composer.lock .php_cs.cache coverage.xml +.idea/ diff --git a/src/Action/Responses/BasicCard.php b/src/Action/Responses/BasicCard.php index a8102c3..f281e59 100644 --- a/src/Action/Responses/BasicCard.php +++ b/src/Action/Responses/BasicCard.php @@ -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. @@ -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; } @@ -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; diff --git a/src/RichMessage/Card.php b/src/RichMessage/Card.php index 1a52bff..1758e1b 100644 --- a/src/RichMessage/Card.php +++ b/src/RichMessage/Card.php @@ -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. @@ -76,11 +73,15 @@ public function image($imageUrl) * * @param string $buttonText button text * @param string $buttonUrl button link URL + * + * @return Dialogflow\Action\Responses\BasicCard */ public function button($buttonText, $buttonUrl) { - $this->buttonText = $buttonText; - $this->buttonUrl = $buttonUrl; + $this->buttons[] = [ + 'buttonText' => $buttonText, + 'buttonUrl' => $buttonUrl + ]; return $this; } @@ -114,15 +115,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; @@ -144,13 +147,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; @@ -185,15 +190,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; @@ -213,13 +220,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; From b69249f3e765da5ed7450eefc3978575aa8c6bef Mon Sep 17 00:00:00 2001 From: "Mike P. Sinn" Date: Fri, 25 Jan 2019 09:30:42 -0600 Subject: [PATCH 2/3] PHPDoc fix --- src/RichMessage/Card.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/RichMessage/Card.php b/src/RichMessage/Card.php index 1758e1b..f1fcc5c 100644 --- a/src/RichMessage/Card.php +++ b/src/RichMessage/Card.php @@ -73,8 +73,6 @@ public function image($imageUrl) * * @param string $buttonText button text * @param string $buttonUrl button link URL - * - * @return Dialogflow\Action\Responses\BasicCard */ public function button($buttonText, $buttonUrl) { From e21de300423d3ec85ab6b92eedb55e7a1cf7139a Mon Sep 17 00:00:00 2001 From: "Mike P. Sinn" Date: Fri, 25 Jan 2019 14:15:40 -0600 Subject: [PATCH 3/3] StyleCI issues --- src/Action/Responses/BasicCard.php | 4 ++-- src/RichMessage/Card.php | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Action/Responses/BasicCard.php b/src/Action/Responses/BasicCard.php index f281e59..af3e51e 100644 --- a/src/Action/Responses/BasicCard.php +++ b/src/Action/Responses/BasicCard.php @@ -87,7 +87,7 @@ public function button($buttonText, $buttonUrl) { $this->buttons[] = [ 'buttonText' => $buttonText, - 'buttonUrl' => $buttonUrl + 'buttonUrl' => $buttonUrl, ]; return $this; @@ -119,7 +119,7 @@ public function renderRichResponseItem() } if ($this->buttons) { - foreach ($this->buttons as $button){ + foreach ($this->buttons as $button) { $basicCard['buttons'][] = [ 'title' => $button['buttonText'], diff --git a/src/RichMessage/Card.php b/src/RichMessage/Card.php index f1fcc5c..17e70f3 100644 --- a/src/RichMessage/Card.php +++ b/src/RichMessage/Card.php @@ -78,7 +78,7 @@ public function button($buttonText, $buttonUrl) { $this->buttons[] = [ 'buttonText' => $buttonText, - 'buttonUrl' => $buttonUrl + 'buttonUrl' => $buttonUrl, ]; return $this; @@ -115,7 +115,7 @@ protected function renderV1() if ($this->buttons) { $out['buttons'] = []; - foreach ($this->buttons as $button){ + foreach ($this->buttons as $button) { $out['buttons'][] = [ 'title' => $button['buttonText'], @@ -147,7 +147,7 @@ protected function renderV1() if ($this->buttons) { $out['buttons'] = []; - foreach ($this->buttons as $button){ + foreach ($this->buttons as $button) { $out['buttons'][] = [ 'text' => $button['buttonText'], @@ -190,7 +190,7 @@ protected function renderV2() if ($this->buttons) { $out['basicCard']['buttons'] = []; - foreach ($this->buttons as $button){ + foreach ($this->buttons as $button) { $out['basicCard']['buttons'][] = [ 'title' => $button['buttonText'], @@ -220,7 +220,7 @@ protected function renderV2() if ($this->buttons) { $out['card']['buttons'] = []; - foreach ($this->buttons as $button){ + foreach ($this->buttons as $button) { $out['card']['buttons'][] = [ 'text' => $button['buttonText'],