Skip to content

Commit 12213bd

Browse files
committed
feat: add rich text block, elements and parts
1 parent ff33581 commit 12213bd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+2903
-25
lines changed

src/Blocks/RichText.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SlackPhp\BlockKit\Blocks;
6+
7+
use SlackPhp\BlockKit\Collections\RichTextSubCollection;
8+
use SlackPhp\BlockKit\Elements\RichTexts\RichTextSubElement;
9+
use SlackPhp\BlockKit\Property;
10+
use SlackPhp\BlockKit\Validation\RequiresAllOf;
11+
use SlackPhp\BlockKit\Validation\ValidCollection;
12+
13+
#[RequiresAllOf('elements')]
14+
class RichText extends Block
15+
{
16+
#[Property, ValidCollection]
17+
public RichTextSubCollection $elements;
18+
19+
/**
20+
* @param RichTextSubCollection|array<RichTextSubCollection|RichTextSubElement|null> $elements
21+
*/
22+
public function __construct(RichTextSubCollection|array $elements = [], ?string $blockId = null)
23+
{
24+
parent::__construct($blockId);
25+
$this->elements = new RichTextSubCollection();
26+
$this->elements(...$elements);
27+
}
28+
29+
public function elements(RichTextSubCollection|RichTextSubElement|null ...$elements): self
30+
{
31+
$this->elements->append(...$elements);
32+
33+
return $this;
34+
}
35+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SlackPhp\BlockKit\Collections;
6+
7+
use SlackPhp\BlockKit\Component;
8+
use SlackPhp\BlockKit\Elements\RichTexts\RichTextElement;
9+
use SlackPhp\BlockKit\Elements\RichTexts\Text;
10+
11+
/**
12+
* @extends ComponentCollection<RichTextElement>
13+
*/
14+
class RichTextCollection extends ComponentCollection
15+
{
16+
protected static function createComponent(array $data): Component
17+
{
18+
return RichTextElement::fromArray($data);
19+
}
20+
21+
/**
22+
* @param array<RichTextElement|self|string|null> $elements
23+
*/
24+
public function __construct(array $elements = [])
25+
{
26+
$this->append(...$elements);
27+
}
28+
29+
public function append(RichTextElement|self|string|null ...$elements): void
30+
{
31+
$this->add($elements);
32+
}
33+
34+
public function prepend(RichTextElement|self|string|null ...$elements): void
35+
{
36+
$this->add($elements, true);
37+
}
38+
39+
protected function prepareItems(array $items): iterable
40+
{
41+
foreach ($items as $element) {
42+
if ($element instanceof RichTextElement) {
43+
yield $element;
44+
} elseif ($element instanceof self) {
45+
yield from $element;
46+
} elseif (is_string($element)) {
47+
yield Text::wrap($element);
48+
}
49+
}
50+
}
51+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SlackPhp\BlockKit\Collections;
6+
7+
use SlackPhp\BlockKit\Component;
8+
use SlackPhp\BlockKit\Elements\RichTexts\RichTextSection;
9+
10+
/**
11+
* @extends ComponentCollection<RichTextSection>
12+
*/
13+
class RichTextSectionCollection extends ComponentCollection
14+
{
15+
protected static function createComponent(array $data): Component
16+
{
17+
return RichTextSection::fromArray($data);
18+
}
19+
20+
/**
21+
* @param array<RichTextSection|self|string|null> $sections
22+
*/
23+
public function __construct(array $sections = [])
24+
{
25+
$this->append(...$sections);
26+
}
27+
28+
public function append(RichTextSection|self|string|null ...$elements): void
29+
{
30+
$this->add($elements);
31+
}
32+
33+
public function prepend(RichTextSection|self|string|null ...$elements): void
34+
{
35+
$this->add($elements, true);
36+
}
37+
38+
39+
protected function prepareItems(array $items): iterable
40+
{
41+
foreach ($items as $section) {
42+
if ($section instanceof RichTextSection) {
43+
yield $section;
44+
} elseif ($section instanceof self) {
45+
yield from $section;
46+
} elseif (is_string($section)) {
47+
yield RichTextSection::wrap($section);
48+
}
49+
}
50+
}
51+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SlackPhp\BlockKit\Collections;
6+
7+
use SlackPhp\BlockKit\Component;
8+
use SlackPhp\BlockKit\Elements\RichTexts\RichTextSubElement;
9+
10+
/**
11+
* @extends ComponentCollection<RichTextSubElement>
12+
*/
13+
class RichTextSubCollection extends ComponentCollection
14+
{
15+
protected static function createComponent(array $data): Component
16+
{
17+
return RichTextSubElement::fromArray($data);
18+
}
19+
20+
/**
21+
* @param array<RichTextSubElement|self|null> $elements
22+
*/
23+
public function __construct(array $elements = [])
24+
{
25+
$this->append(...$elements);
26+
}
27+
28+
public function append(RichTextSubElement|self|null ...$elements): void
29+
{
30+
$this->add($elements);
31+
}
32+
33+
public function prepend(RichTextSubElement|self|null ...$elements): void
34+
{
35+
$this->add($elements, true);
36+
}
37+
38+
protected function prepareItems(array $items): iterable
39+
{
40+
foreach ($items as $element) {
41+
if ($element instanceof RichTextSubElement) {
42+
yield $element;
43+
} elseif ($element instanceof self) {
44+
yield from $element;
45+
}
46+
}
47+
}
48+
}

src/Collections/TextCollection.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SlackPhp\BlockKit\Collections;
6+
7+
use SlackPhp\BlockKit\Component;
8+
use SlackPhp\BlockKit\Elements\RichTexts\Text;
9+
10+
/**
11+
* @extends ComponentCollection<Text>
12+
*/
13+
class TextCollection extends ComponentCollection
14+
{
15+
protected static function createComponent(array $data): Component
16+
{
17+
return Text::fromArray($data);
18+
}
19+
20+
/**
21+
* @param array<Text|self|string|null> $elements
22+
*/
23+
public function __construct(array $elements = [])
24+
{
25+
$this->append(...$elements);
26+
}
27+
28+
public function append(Text|self|string|null ...$elements): void
29+
{
30+
$this->add($elements);
31+
}
32+
33+
public function prepend(Text|self|string|null ...$elements): void
34+
{
35+
$this->add($elements, true);
36+
}
37+
38+
protected function prepareItems(array $items): iterable
39+
{
40+
foreach ($items as $text) {
41+
if ($text instanceof Text) {
42+
yield $text;
43+
} elseif ($text instanceof self) {
44+
yield from $text;
45+
} elseif (is_string($text)) {
46+
yield Text::wrap($text);
47+
}
48+
}
49+
}
50+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SlackPhp\BlockKit\Elements\RichTexts;
6+
7+
use SlackPhp\BlockKit\Parts\Range;
8+
use SlackPhp\BlockKit\Property;
9+
use SlackPhp\BlockKit\Validation\RequiresAllOf;
10+
11+
#[RequiresAllOf('range')]
12+
class Broadcast extends RichTextElement
13+
{
14+
#[Property]
15+
public ?Range $range;
16+
17+
public function __construct(Range|string|null $range = null)
18+
{
19+
parent::__construct();
20+
$this->range($range);
21+
}
22+
23+
public function range(Range|string|null $range): self
24+
{
25+
$this->range = Range::fromValue($range);
26+
27+
return $this;
28+
}
29+
}

src/Elements/RichTexts/Channel.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SlackPhp\BlockKit\Elements\RichTexts;
6+
7+
use SlackPhp\BlockKit\Elements\RichTexts\Traits\HasMentionStyle;
8+
use SlackPhp\BlockKit\Parts\MentionStyle;
9+
use SlackPhp\BlockKit\Property;
10+
use SlackPhp\BlockKit\Validation\RequiresAllOf;
11+
use SlackPhp\BlockKit\Validation\ValidString;
12+
13+
#[RequiresAllOf('channel_id')]
14+
class Channel extends RichTextElement
15+
{
16+
use HasMentionStyle;
17+
18+
#[Property('channel_id'), ValidString]
19+
public ?string $channelId;
20+
21+
public function __construct(?string $channelId = null, ?MentionStyle $style = null)
22+
{
23+
parent::__construct();
24+
25+
$this->channelId($channelId);
26+
$this->style($style);
27+
}
28+
29+
public function channelId(?string $channelId): self
30+
{
31+
$this->channelId = $channelId;
32+
33+
return $this;
34+
}
35+
}

src/Elements/RichTexts/Color.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SlackPhp\BlockKit\Elements\RichTexts;
6+
7+
use SlackPhp\BlockKit\Property;
8+
use SlackPhp\BlockKit\Validation\RequiresAllOf;
9+
use SlackPhp\BlockKit\Validation\ValidString;
10+
11+
#[RequiresAllOf('value')]
12+
class Color extends RichTextElement
13+
{
14+
#[Property, ValidString]
15+
public ?string $value;
16+
17+
public function __construct(?string $value = null)
18+
{
19+
parent::__construct();
20+
$this->value($value);
21+
}
22+
23+
public function value(?string $value): self
24+
{
25+
$this->value = $value;
26+
27+
return $this;
28+
}
29+
}

0 commit comments

Comments
 (0)