Skip to content

[Notifier] Allow to set block_id/value for SlackActionsBlock and SlackButtonBlockElement #60209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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