Skip to content

Commit 6687e23

Browse files
committed
Add ContextBlock for slack notifier
1 parent ff97b5f commit 6687e23

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Symfony\Component\Notifier\Bridge\Slack\Block;
15+
16+
final class SlackContextBlock extends AbstractSlackBlock
17+
{
18+
private const ELEMENT_LIMIT = 10;
19+
20+
public function __construct()
21+
{
22+
$this->options['type'] = 'context';
23+
}
24+
25+
public function text(string $text, bool $markdown = true, bool $emoji = true, bool $verbatim = false): self
26+
{
27+
if (self::ELEMENT_LIMIT === \count($this->options['elements'] ?? [])) {
28+
throw new \LogicException(sprintf('Maximum number of elements should not exceed %d.', self::ELEMENT_LIMIT));
29+
}
30+
31+
$element = [
32+
'type' => $markdown ? 'mrkdwn' : 'plain_text',
33+
'text' => $text,
34+
];
35+
if ($markdown) {
36+
$element['verbatim'] = $verbatim;
37+
} else {
38+
$element['emoji'] = $emoji;
39+
}
40+
$this->options['elements'][] = $element;
41+
42+
return $this;
43+
}
44+
45+
public function image(string $url, string $text): self
46+
{
47+
if (self::ELEMENT_LIMIT === \count($this->options['elements'] ?? [])) {
48+
throw new \LogicException(sprintf('Maximum number of elements should not exceed %d.', self::ELEMENT_LIMIT));
49+
}
50+
51+
$this->options['elements'][] = [
52+
'type' => 'image',
53+
'image_url' => $url,
54+
'alt_text' => $text,
55+
];
56+
57+
return $this;
58+
}
59+
60+
public function id(string $id): self
61+
{
62+
$this->options['block_id'] = $id;
63+
64+
return $this;
65+
}
66+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Slack\Tests\Block;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackContextBlock;
16+
17+
final class SlackContextBlockTest extends TestCase
18+
{
19+
public function testCanBeInstantiated(): void
20+
{
21+
$context = new SlackContextBlock();
22+
$context->text('context text without emoji', false, false);
23+
$context->text('context text verbatim', true, false, true);
24+
$context->image('https://example.com/image.jpg', 'an image');
25+
$context->id('context_id');
26+
27+
$this->assertSame([
28+
'type' => 'context',
29+
'elements' => [
30+
[
31+
'type' => 'plain_text',
32+
'text' => 'context text without emoji',
33+
'emoji' => false,
34+
],
35+
[
36+
'type' => 'mrkdwn',
37+
'text' => 'context text verbatim',
38+
'verbatim' => true,
39+
],
40+
[
41+
'type' => 'image',
42+
'image_url' => 'https://example.com/image.jpg',
43+
'alt_text' => 'an image',
44+
],
45+
],
46+
'block_id' => 'context_id',
47+
], $context->toArray());
48+
}
49+
50+
public function testThrowsWhenElementsLimitReached(): void
51+
{
52+
$context = new SlackContextBlock();
53+
for ($i = 0; $i < 10; ++$i) {
54+
if (0 === $i % 2) {
55+
$context->text($i);
56+
} else {
57+
$context->image($i, $i);
58+
}
59+
}
60+
61+
$this->expectException(\LogicException::class);
62+
$this->expectExceptionMessage('Maximum number of elements should not exceed 10.');
63+
64+
$context->text('fail');
65+
}
66+
}

0 commit comments

Comments
 (0)
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