Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,26 @@ public function __construct()
/**
* @return $this
*/
public function button(string $text, string $url, ?string $style = null): static
public function button(string $text, ?string $url = null, ?string $style = null, ?string $value = null): static
{
if (25 === \count($this->options['elements'] ?? [])) {
throw new \LogicException('Maximum number of buttons should not exceed 25.');
}

$element = new SlackButtonBlockElement($text, $url, $style);
$element = new SlackButtonBlockElement($text, $url, $style, $value);

$this->options['elements'][] = $element->toArray();

return $this;
}

/**
* @return $this
*/
public function id(string $id): static
{
$this->options['block_id'] = $id;

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,27 @@
*/
final class SlackButtonBlockElement extends AbstractSlackBlockElement
{
public function __construct(string $text, string $url, ?string $style = null)
public function __construct(string $text, ?string $url = null, ?string $style = null, ?string $value = null)
{
$this->options = [
'type' => 'button',
'text' => [
'type' => 'plain_text',
'text' => $text,
],
'url' => $url,
];

if ($url) {
$this->options['url'] = $url;
}

if ($style) {
// primary or danger
$this->options['style'] = $style;
}

if ($value) {
$this->options['value'] = $value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ final class SlackActionsBlockTest extends TestCase
public function testCanBeInstantiated()
{
$actions = new SlackActionsBlock();
$actions->button('first button text', 'https://example.org')
$actions->button('first button text', 'https://example.org', null, 'test-value')
->button('second button text', 'https://example.org/slack', 'danger')
->button('third button text', null, null, 'test-value-3')
;

$this->assertSame([
Expand All @@ -33,6 +34,7 @@ public function testCanBeInstantiated()
'text' => 'first button text',
],
'url' => 'https://example.org',
'value' => 'test-value'
],
[
'type' => 'button',
Expand All @@ -43,6 +45,14 @@ public function testCanBeInstantiated()
'url' => 'https://example.org/slack',
'style' => 'danger',
],
[
'type' => 'button',
'text' => [
'type' => 'plain_text',
'text' => 'third button text',
],
'value' => 'test-value-3',
]
],
], $actions->toArray());
}
Expand Down
Loading