Skip to content

Commit c105f8a

Browse files
committed
Split replace/delete original from response type
1 parent c577977 commit c105f8a

7 files changed

Lines changed: 205 additions & 35 deletions

File tree

src/Kit.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ public static function attachment(
4343
*/
4444
public static function message(
4545
Collections\BlockCollection|array|null $blocks = null,
46-
?Surfaces\MessageDirective $directive = null,
46+
?Surfaces\MessageDirective\ResponseType $responseType = null,
4747
?string $text = null,
4848
Collections\AttachmentCollection|array|null $attachments = null,
4949
?bool $mrkdwn = null,
5050
?string $threadTs = null,
5151
): Surfaces\Message {
52-
return new Surfaces\Message($blocks, $directive, $text, $attachments, $mrkdwn, $threadTs);
52+
return new Surfaces\Message($blocks, $responseType, $text, $attachments, $mrkdwn, $threadTs);
5353
}
5454

5555
/**

src/Previewer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function preview(Surfaces\Surface $surface): string
2727
if ($surface instanceof Surfaces\Message) {
2828
// Block Kit Builder doesn't support message directives or fallback text.
2929
$surface = (clone $surface)
30-
->directive(null)
30+
->responseType(null)
3131
->text(null)
3232
->mrkdwn(null)
3333
->threadTs(null);

src/Surfaces/Message.php

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
namespace SlackPhp\BlockKit\Surfaces;
66

7+
use SlackPhp\BlockKit\{FauxProperty,
8+
Property,
9+
Surfaces\MessageDirective\DeleteOriginal,
10+
Surfaces\MessageDirective\ReplaceOriginal,
11+
Surfaces\MessageDirective\ResponseType};
712
use SlackPhp\BlockKit\Blocks\Block;
813
use SlackPhp\BlockKit\Collections\{AttachmentCollection, BlockCollection};
9-
use SlackPhp\BlockKit\{FauxProperty, Property};
1014
use SlackPhp\BlockKit\Hydration\OmitType;
1115
use SlackPhp\BlockKit\Validation\{RequiresAnyOf, UniqueIds, ValidCollection, ValidString};
1216

@@ -19,8 +23,14 @@ class Message extends Surface
1923
#[Property, ValidCollection(50, 0), UniqueIds]
2024
public BlockCollection $blocks;
2125

22-
#[FauxProperty('response_type', 'replace_original', 'delete_original')]
23-
public ?MessageDirective $directive;
26+
#[FauxProperty('response_type')]
27+
public ?ResponseType $responseType;
28+
29+
#[FauxProperty('replace_original')]
30+
public ?ReplaceOriginal $replaceOriginal;
31+
32+
#[FauxProperty('delete_original')]
33+
public ?DeleteOriginal $deleteOriginal;
2434

2535
#[Property, ValidString]
2636
public ?string $text;
@@ -40,19 +50,23 @@ class Message extends Surface
4050
*/
4151
public function __construct(
4252
BlockCollection|array|null $blocks = null,
43-
?MessageDirective $directive = null,
53+
?ResponseType $directive = null,
4454
?string $text = null,
4555
AttachmentCollection|array|null $attachments = null,
4656
?bool $mrkdwn = null,
4757
?string $threadTs = null,
4858
?bool $ephemeral = null,
59+
?bool $replaceOriginal = null,
60+
?bool $deleteOriginal = null,
4961
) {
5062
parent::__construct($blocks);
5163
$this->attachments = AttachmentCollection::wrap($attachments);
52-
$this->directive($directive ?? ($ephemeral ? MessageDirective::EPHEMERAL : null));
64+
$this->responseType($directive ?? ($ephemeral ? ResponseType::EPHEMERAL : null));
5365
$this->text($text);
5466
$this->mrkdwn($mrkdwn);
5567
$this->threadTs($threadTs);
68+
$this->replaceOriginal($replaceOriginal);
69+
$this->deleteOriginal($deleteOriginal);
5670
}
5771

5872
/**
@@ -62,36 +76,40 @@ public function __construct(
6276
*/
6377
public function ephemeral(): static
6478
{
65-
return $this->directive(MessageDirective::EPHEMERAL);
79+
return $this->responseType(ResponseType::EPHEMERAL);
6680
}
6781

6882
/**
6983
* Configures message to send to the entire channel.
7084
*/
7185
public function inChannel(): static
7286
{
73-
return $this->directive(MessageDirective::IN_CHANNEL);
87+
return $this->responseType(ResponseType::IN_CHANNEL);
7488
}
7589

7690
/**
7791
* Configures message to "replace_original" mode.
7892
*/
79-
public function replaceOriginal(): static
93+
public function replaceOriginal(ReplaceOriginal|array|bool|null $replaceOriginal = true): static
8094
{
81-
return $this->directive(MessageDirective::REPLACE_ORIGINAL);
95+
$this->replaceOriginal = ReplaceOriginal::fromValue($replaceOriginal);
96+
97+
return $this;
8298
}
8399

84100
/**
85101
* Configures message to "delete_original" mode.
86102
*/
87-
public function deleteOriginal(): static
103+
public function deleteOriginal(DeleteOriginal|array|bool|null $deleteOriginal = true): static
88104
{
89-
return $this->directive(MessageDirective::DELETE_ORIGINAL);
105+
$this->deleteOriginal = DeleteOriginal::fromValue($deleteOriginal);
106+
107+
return $this;
90108
}
91109

92-
public function directive(MessageDirective|array|null $directive): static
110+
public function responseType(ResponseType|array|null $responseType): static
93111
{
94-
$this->directive = MessageDirective::fromValue($directive);
112+
$this->responseType = ResponseType::fromValue($responseType);
95113

96114
return $this;
97115
}
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\Surfaces\MessageDirective;
6+
7+
use SlackPhp\BlockKit\Exception;
8+
9+
enum DeleteOriginal
10+
{
11+
case DELETE_ORIGINAL;
12+
case DONT_DELETE_ORIGINAL;
13+
14+
/**
15+
* @return array<string, string>
16+
*/
17+
public function toArray(): array
18+
{
19+
return match ($this) {
20+
self::DELETE_ORIGINAL => ['delete_original' => 'true'],
21+
self::DONT_DELETE_ORIGINAL => ['delete_original' => 'false'],
22+
};
23+
}
24+
25+
/**
26+
* @param self|array<string, string>|bool|null $data
27+
* @return static|null
28+
*/
29+
public static function fromValue(self|array|bool|null $data): ?self
30+
{
31+
if ($data instanceof self) {
32+
return $data;
33+
}
34+
35+
if (is_bool($data)) {
36+
return $data ? self::DELETE_ORIGINAL : self::DONT_DELETE_ORIGINAL;
37+
}
38+
39+
if (is_array($data)) {
40+
$data = array_filter($data);
41+
return match ($data) {
42+
['delete_original' => 'true'] => self::DELETE_ORIGINAL,
43+
['delete_original' => 'false'] => self::DONT_DELETE_ORIGINAL,
44+
[] => null,
45+
default => throw new Exception('Invalid Delete Original enum encountered: %s', [json_encode($data)]),
46+
};
47+
}
48+
49+
return null;
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\Surfaces\MessageDirective;
6+
7+
use SlackPhp\BlockKit\Exception;
8+
9+
enum ReplaceOriginal
10+
{
11+
case REPLACE_ORIGINAL;
12+
case DONT_REPLACE_ORIGINAL;
13+
14+
/**
15+
* @return array<string, string>
16+
*/
17+
public function toArray(): array
18+
{
19+
return match ($this) {
20+
self::REPLACE_ORIGINAL => ['replace_original' => 'true'],
21+
self::DONT_REPLACE_ORIGINAL => ['replace_original' => 'false'],
22+
};
23+
}
24+
25+
/**
26+
* @param self|array<string, string>|bool|null $data
27+
* @return static|null
28+
*/
29+
public static function fromValue(self|array|bool|null $data): ?self
30+
{
31+
if ($data instanceof self) {
32+
return $data;
33+
}
34+
35+
if (is_bool($data)) {
36+
return $data ? self::REPLACE_ORIGINAL : self::DONT_REPLACE_ORIGINAL;
37+
}
38+
39+
if (is_array($data)) {
40+
$data = array_filter($data);
41+
return match ($data) {
42+
['replace_original' => 'true'] => self::REPLACE_ORIGINAL,
43+
['replace_original' => 'false'] => self::DONT_REPLACE_ORIGINAL,
44+
[] => null,
45+
default => throw new Exception('Invalid Replace Original enum encountered: %s', [json_encode($data)]),
46+
};
47+
}
48+
49+
return null;
50+
}
51+
}

src/Surfaces/MessageDirective.php renamed to src/Surfaces/MessageDirective/ResponseType.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22

33
declare(strict_types=1);
44

5-
namespace SlackPhp\BlockKit\Surfaces;
5+
namespace SlackPhp\BlockKit\Surfaces\MessageDirective;
66

77
use SlackPhp\BlockKit\Exception;
88

9-
enum MessageDirective
9+
enum ResponseType
1010
{
1111
case EPHEMERAL;
1212
case IN_CHANNEL;
13-
case REPLACE_ORIGINAL;
14-
case DELETE_ORIGINAL;
1513

1614
/**
1715
* @return array<string, string>
@@ -21,8 +19,6 @@ public function toArray(): array
2119
return match ($this) {
2220
self::EPHEMERAL => ['response_type' => 'ephemeral'],
2321
self::IN_CHANNEL => ['response_type' => 'in_channel'],
24-
self::REPLACE_ORIGINAL => ['replace_original' => 'true'],
25-
self::DELETE_ORIGINAL => ['delete_original' => 'true'],
2622
};
2723
}
2824

@@ -41,10 +37,8 @@ public static function fromValue(self|array|null $data): ?self
4137
return match ($data) {
4238
['response_type' => 'ephemeral'] => self::EPHEMERAL,
4339
['response_type' => 'in_channel'] => self::IN_CHANNEL,
44-
['replace_original' => 'true'] => self::REPLACE_ORIGINAL,
45-
['delete_original' => 'true'] => self::DELETE_ORIGINAL,
4640
[] => null,
47-
default => throw new Exception('Invalid MessageDirective enum encountered: %s', [json_encode($data)]),
41+
default => throw new Exception('Invalid Response Type enum encountered: %s', [json_encode($data)]),
4842
};
4943
}
5044

0 commit comments

Comments
 (0)