From c81a96a11fd92e519563503ade294ee9c5ef7f6b Mon Sep 17 00:00:00 2001 From: Stefan Gasser Date: Sat, 10 Mar 2018 20:54:26 +0100 Subject: [PATCH] Add support for default_action on element #36 (#60) --- src/Extensions/Element.php | 17 +++++++++++++++++ tests/Extensions/ElementTest.php | 12 ++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/Extensions/Element.php b/src/Extensions/Element.php index 12da26b..92bffb7 100644 --- a/src/Extensions/Element.php +++ b/src/Extensions/Element.php @@ -21,6 +21,9 @@ class Element implements JsonSerializable /** @var object */ protected $buttons; + /** @var object */ + protected $default_action; + /** * @param $title * @return static @@ -110,6 +113,19 @@ public function addButtons(array $buttons) return $this; } + /** + * @param ElementButton $defaultAction + * + * @return $this + */ + public function defaultAction(ElementButton $defaultAction) + { + $defaultAction->type(ElementButton::TYPE_WEB_URL); + $this->default_action = $defaultAction->toArray(); + + return $this; + } + /** * @return array */ @@ -120,6 +136,7 @@ public function toArray() 'image_url' => $this->image_url, 'item_url' => $this->item_url, 'subtitle' => $this->subtitle, + 'default_action' => $this->default_action, 'buttons' => $this->buttons, ]; } diff --git a/tests/Extensions/ElementTest.php b/tests/Extensions/ElementTest.php index 7a8631e..61ea59b 100644 --- a/tests/Extensions/ElementTest.php +++ b/tests/Extensions/ElementTest.php @@ -81,4 +81,16 @@ public function it_can_add_multiple_buttons() $this->assertSame('button1', Arr::get($template->toArray(), 'buttons.0.title')); $this->assertSame('button2', Arr::get($template->toArray(), 'buttons.1.title')); } + + /** + * @test + **/ + public function it_can_set_default_action() + { + $template = new Element('Element with default_action'); + $template->defaultAction(ElementButton::create(null)->url('https://botman.io')); + + $this->assertSame('web_url', Arr::get($template->toArray(), 'default_action.type')); + $this->assertSame('https://botman.io', Arr::get($template->toArray(), 'default_action.url')); + } }