Skip to content

Commit

Permalink
Add support for default_action on element #36 (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Gasser authored and mpociot committed Mar 10, 2018
1 parent 76fca3c commit c81a96a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Extensions/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class Element implements JsonSerializable
/** @var object */
protected $buttons;

/** @var object */
protected $default_action;

/**
* @param $title
* @return static
Expand Down Expand Up @@ -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
*/
Expand All @@ -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,
];
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Extensions/ElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}

0 comments on commit c81a96a

Please sign in to comment.