diff --git a/CHANGELOG.md b/CHANGELOG.md index 33e86cf..e322a94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/AttachmentAction.php b/src/AttachmentAction.php index 089fd75..f2fefd7 100644 --- a/src/AttachmentAction.php +++ b/src/AttachmentAction.php @@ -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. * @@ -141,7 +148,7 @@ public function getValue(): ?string } /** - * @param string $value + * @param string|null $value * * @return AttachmentAction */ @@ -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 */ @@ -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; } } diff --git a/tests/AttachmentUnitTest.php b/tests/AttachmentUnitTest.php index 4056205..bb97af4 100644 --- a/tests/AttachmentUnitTest.php +++ b/tests/AttachmentUnitTest.php @@ -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'), ] ) ; @@ -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', @@ -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', @@ -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', + ], ], ]; diff --git a/tests/ClientFunctionalTest.php b/tests/ClientFunctionalTest.php index beef726..7f40247 100644 --- a/tests/ClientFunctionalTest.php +++ b/tests/ClientFunctionalTest.php @@ -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'), ] ) ; @@ -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', @@ -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', @@ -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', + ], ], ];