diff --git a/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackContextBlock.php b/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackContextBlock.php new file mode 100644 index 0000000000000..8c6434321a14d --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackContextBlock.php @@ -0,0 +1,66 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Symfony\Component\Notifier\Bridge\Slack\Block; + +final class SlackContextBlock extends AbstractSlackBlock +{ + private const ELEMENT_LIMIT = 10; + + public function __construct() + { + $this->options['type'] = 'context'; + } + + public function text(string $text, bool $markdown = true, bool $emoji = true, bool $verbatim = false): self + { + if (self::ELEMENT_LIMIT === \count($this->options['elements'] ?? [])) { + throw new \LogicException(sprintf('Maximum number of elements should not exceed %d.', self::ELEMENT_LIMIT)); + } + + $element = [ + 'type' => $markdown ? 'mrkdwn' : 'plain_text', + 'text' => $text, + ]; + if ($markdown) { + $element['verbatim'] = $verbatim; + } else { + $element['emoji'] = $emoji; + } + $this->options['elements'][] = $element; + + return $this; + } + + public function image(string $url, string $text): self + { + if (self::ELEMENT_LIMIT === \count($this->options['elements'] ?? [])) { + throw new \LogicException(sprintf('Maximum number of elements should not exceed %d.', self::ELEMENT_LIMIT)); + } + + $this->options['elements'][] = [ + 'type' => 'image', + 'image_url' => $url, + 'alt_text' => $text, + ]; + + return $this; + } + + public function id(string $id): self + { + $this->options['block_id'] = $id; + + return $this; + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/Slack/Tests/Block/SlackContextBlockTest.php b/src/Symfony/Component/Notifier/Bridge/Slack/Tests/Block/SlackContextBlockTest.php new file mode 100644 index 0000000000000..2df45a4907ed1 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/Slack/Tests/Block/SlackContextBlockTest.php @@ -0,0 +1,66 @@ + + * + * 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\Tests\Block; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Notifier\Bridge\Slack\Block\SlackContextBlock; + +final class SlackContextBlockTest extends TestCase +{ + public function testCanBeInstantiated(): void + { + $context = new SlackContextBlock(); + $context->text('context text without emoji', false, false); + $context->text('context text verbatim', true, false, true); + $context->image('https://example.com/image.jpg', 'an image'); + $context->id('context_id'); + + $this->assertSame([ + 'type' => 'context', + 'elements' => [ + [ + 'type' => 'plain_text', + 'text' => 'context text without emoji', + 'emoji' => false, + ], + [ + 'type' => 'mrkdwn', + 'text' => 'context text verbatim', + 'verbatim' => true, + ], + [ + 'type' => 'image', + 'image_url' => 'https://example.com/image.jpg', + 'alt_text' => 'an image', + ], + ], + 'block_id' => 'context_id', + ], $context->toArray()); + } + + public function testThrowsWhenElementsLimitReached(): void + { + $context = new SlackContextBlock(); + for ($i = 0; $i < 10; ++$i) { + if (0 === $i % 2) { + $context->text($i); + } else { + $context->image($i, $i); + } + } + + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Maximum number of elements should not exceed 10.'); + + $context->text('fail'); + } +} 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