From e9b79542a3426f1987cbe0abacbeadcaa5212f0f Mon Sep 17 00:00:00 2001 From: Christophe VERGNE Date: Thu, 25 Apr 2024 19:49:43 +0200 Subject: [PATCH] [Notifier] [Slack] Add button block element and `emoji`/`verbatim` options to section block --- .../Bridge/Slack/Block/SlackActionsBlock.php | 16 ++------- .../Slack/Block/SlackButtonBlockElement.php | 35 +++++++++++++++++++ .../Bridge/Slack/Block/SlackSectionBlock.php | 22 ++++++++++-- .../Notifier/Bridge/Slack/CHANGELOG.md | 6 ++++ .../Tests/Block/SlackSectionBlockTest.php | 14 ++++++++ .../Bridge/Slack/Tests/SlackOptionsTest.php | 3 ++ 6 files changed, 79 insertions(+), 17 deletions(-) create mode 100644 src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackButtonBlockElement.php diff --git a/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackActionsBlock.php b/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackActionsBlock.php index ea74e5a1bb227..25da853677415 100644 --- a/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackActionsBlock.php +++ b/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackActionsBlock.php @@ -30,21 +30,9 @@ public function button(string $text, string $url, ?string $style = null): static throw new \LogicException('Maximum number of buttons should not exceed 25.'); } - $element = [ - 'type' => 'button', - 'text' => [ - 'type' => 'plain_text', - 'text' => $text, - ], - 'url' => $url, - ]; + $element = new SlackButtonBlockElement($text, $url, $style); - if ($style) { - // primary or danger - $element['style'] = $style; - } - - $this->options['elements'][] = $element; + $this->options['elements'][] = $element->toArray(); return $this; } diff --git a/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackButtonBlockElement.php b/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackButtonBlockElement.php new file mode 100644 index 0000000000000..ff83bb9d870a5 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackButtonBlockElement.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Bridge\Slack\Block; + +/** + * @author Christophe Vergne + */ +final class SlackButtonBlockElement extends AbstractSlackBlockElement +{ + public function __construct(string $text, string $url, ?string $style = null) + { + $this->options = [ + 'type' => 'button', + 'text' => [ + 'type' => 'plain_text', + 'text' => $text, + ], + 'url' => $url, + ]; + + if ($style) { + // primary or danger + $this->options['style'] = $style; + } + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackSectionBlock.php b/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackSectionBlock.php index 01e80e26802f3..6842b69cf4250 100644 --- a/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackSectionBlock.php +++ b/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackSectionBlock.php @@ -24,30 +24,46 @@ public function __construct() /** * @return $this */ - public function text(string $text, bool $markdown = true): static + public function text(string $text, bool $markdown = true, bool $emoji = true, bool $verbatim = false): static { $this->options['text'] = [ 'type' => $markdown ? 'mrkdwn' : 'plain_text', 'text' => $text, ]; + // verbatim is only available for markdown + if ($markdown) { + $this->options['text']['verbatim'] = $verbatim; + } else { + $this->options['text']['emoji'] = $emoji; + } + return $this; } /** * @return $this */ - public function field(string $text, bool $markdown = true): static + public function field(string $text, bool $markdown = true, bool $emoji = true, bool $verbatim = false): static { if (10 === \count($this->options['fields'] ?? [])) { throw new \LogicException('Maximum number of fields should not exceed 10.'); } - $this->options['fields'][] = [ + $field = [ 'type' => $markdown ? 'mrkdwn' : 'plain_text', 'text' => $text, ]; + // verbatim is only available for markdown + if ($markdown) { + $field['verbatim'] = $verbatim; + } else { + $field['emoji'] = $emoji; + } + + $this->options['fields'][] = $field; + return $this; } diff --git a/src/Symfony/Component/Notifier/Bridge/Slack/CHANGELOG.md b/src/Symfony/Component/Notifier/Bridge/Slack/CHANGELOG.md index 05fecc49d9c2c..01a62c1e86b3b 100644 --- a/src/Symfony/Component/Notifier/Bridge/Slack/CHANGELOG.md +++ b/src/Symfony/Component/Notifier/Bridge/Slack/CHANGELOG.md @@ -1,6 +1,12 @@ CHANGELOG ========= +7.2 +--- + + * Add `SlackButtonBlockElement` to add button as accessory to a section block + * Add `emoji` and `verbatim` options to `text` and `field` methods in `SlackSectionBlock` + 6.3 --- diff --git a/src/Symfony/Component/Notifier/Bridge/Slack/Tests/Block/SlackSectionBlockTest.php b/src/Symfony/Component/Notifier/Bridge/Slack/Tests/Block/SlackSectionBlockTest.php index 0a1c097bcf54d..fcfab588368a2 100644 --- a/src/Symfony/Component/Notifier/Bridge/Slack/Tests/Block/SlackSectionBlockTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Slack/Tests/Block/SlackSectionBlockTest.php @@ -22,6 +22,8 @@ public function testCanBeInstantiated() $section = new SlackSectionBlock(); $section->text('section text'); $section->field('section field'); + $section->field('section field with verbatim', true, true, false); + $section->field('section field in plain text with verbatim', false, true, true); $section->accessory(new SlackImageBlockElement('https://example.com/image.jpg', 'an image')); $this->assertSame([ @@ -29,11 +31,23 @@ public function testCanBeInstantiated() 'text' => [ 'type' => 'mrkdwn', 'text' => 'section text', + 'verbatim' => false, ], 'fields' => [ [ 'type' => 'mrkdwn', 'text' => 'section field', + 'verbatim' => false, + ], + [ + 'type' => 'mrkdwn', + 'text' => 'section field with verbatim', + 'verbatim' => false, + ], + [ + 'type' => 'plain_text', + 'text' => 'section field in plain text with verbatim', + 'emoji' => true, ], ], 'accessory' => [ diff --git a/src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackOptionsTest.php index 748b7f2ebf7bc..c80d263b9cf38 100644 --- a/src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackOptionsTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackOptionsTest.php @@ -146,6 +146,7 @@ public static function fromNotificationProvider(): iterable 'text' => [ 'type' => 'mrkdwn', 'text' => $subject, + 'verbatim' => false, ], ], ], @@ -162,6 +163,7 @@ public static function fromNotificationProvider(): iterable 'text' => [ 'type' => 'mrkdwn', 'text' => $subject, + 'verbatim' => false, ], ], [ @@ -169,6 +171,7 @@ public static function fromNotificationProvider(): iterable 'text' => [ 'type' => 'mrkdwn', 'text' => $content, + 'verbatim' => false, ], ], ], pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy