Skip to content

Commit

Permalink
Added possibility to create link buttons with URL (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
balazscsaba2006 authored and soullivaneuh committed Jul 4, 2019
1 parent ba41738 commit 1e37862
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Add support for link buttons

## [2.2.0]
### Added
Expand Down
46 changes: 41 additions & 5 deletions src/AttachmentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ final class AttachmentAction
*/
private $value;

/**
* Optional value. It can be used to create link buttons.
*
* @var string|null
*/
private $url;

/**
* Confirmation field.
*
Expand Down Expand Up @@ -141,7 +148,7 @@ public function getValue(): ?string
}

/**
* @param string $value
* @param string|null $value
*
* @return AttachmentAction
*/
Expand All @@ -152,6 +159,26 @@ public function setValue(?string $value): self
return $this;
}

/**
* @return string|null
*/
public function getUrl(): ?string
{
return $this->url;
}

/**
* @param string|null $url
*
* @return AttachmentAction
*/
public function setUrl(?string $url): self
{
$this->url = $url;

return $this;
}

/**
* @return ActionConfirmation|null
*/
Expand Down Expand Up @@ -179,13 +206,22 @@ public function setConfirm(?ActionConfirmation $confirm): self
*/
public function toArray(): array
{
return [
'name' => $this->name,
$array = [
'text' => $this->text,
'style' => $this->style,
'type' => $this->type,
'value' => $this->value,
'confirm' => $this->confirm ? $this->confirm->toArray() : null,
];

// Link buttons do not require "name", "value" and "confirm" attributes.
// @see https://api.slack.com/docs/message-attachments#link_buttons
if (null !== $this->url) {
$array['url'] = $this->url;
} else {
$array['name'] = $this->name;
$array['value'] = $this->value;
$array['confirm'] = $this->confirm ? $this->confirm->toArray() : null;
}

return $array;
}
}
14 changes: 12 additions & 2 deletions tests/AttachmentUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public function testAttachmentToArray(): void
->setOkText('OK Text 2')
->setDismissText('Dismiss Text 2')
),
(new AttachmentAction('Button Name 1', 'Button Label 1'))
->setStyle('default')
->setType('button')
->setUrl('https://www.google.com'),
]
)
;
Expand Down Expand Up @@ -149,10 +153,10 @@ public function testAttachmentToArray(): void
],
'actions' => [
[
'name' => 'Name 1',
'text' => 'Text 1',
'style' => 'default',
'type' => 'button',
'name' => 'Name 1',
'value' => 'Value 1',
'confirm' => [
'title' => 'Title 1',
Expand All @@ -162,10 +166,10 @@ public function testAttachmentToArray(): void
],
],
[
'name' => 'Name 2',
'text' => 'Text 2',
'style' => 'default',
'type' => 'button',
'name' => 'Name 2',
'value' => 'Value 2',
'confirm' => [
'title' => 'Title 2',
Expand All @@ -174,6 +178,12 @@ public function testAttachmentToArray(): void
'dismiss_text' => 'Dismiss Text 2',
],
],
[
'text' => 'Button Label 1',
'style' => 'default',
'type' => 'button',
'url' => 'https://www.google.com',
],
],
];

Expand Down
14 changes: 12 additions & 2 deletions tests/ClientFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ public function testMessageWithAttachmentsAndActions(): void
->setOkText('OK Text 2')
->setDismissText('Dismiss Text 2')
),
(new AttachmentAction('Button Name 1', 'Button Label 1'))
->setStyle('default')
->setType('button')
->setUrl('https://www.google.com'),
]
)
;
Expand All @@ -291,10 +295,10 @@ public function testMessageWithAttachmentsAndActions(): void
'fields' => [],
'actions' => [
[
'name' => 'Name 1',
'text' => 'Text 1',
'style' => 'default',
'type' => 'button',
'name' => 'Name 1',
'value' => 'Value 1',
'confirm' => [
'title' => 'Title 1',
Expand All @@ -304,10 +308,10 @@ public function testMessageWithAttachmentsAndActions(): void
],
],
[
'name' => 'Name 2',
'text' => 'Text 2',
'style' => 'default',
'type' => 'button',
'name' => 'Name 2',
'value' => 'Value 2',
'confirm' => [
'title' => 'Title 2',
Expand All @@ -316,6 +320,12 @@ public function testMessageWithAttachmentsAndActions(): void
'dismiss_text' => 'Dismiss Text 2',
],
],
[
'text' => 'Button Label 1',
'style' => 'default',
'type' => 'button',
'url' => 'https://www.google.com',
],
],
];

Expand Down

0 comments on commit 1e37862

Please sign in to comment.