diff --git a/README.md b/README.md index a755f8d..adf8b71 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,8 @@ From Slack's [Block Kit documentation](https://api.slack.com/block-kit): > Customize the order and appearance of information and guide users through your app's capabilities by composing, > updating, sequencing, and stacking _blocks_ — reusable components that work almost everywhere in Slack. -This library provides an OOP interface in PHP for composing messages using Slack Block Kit. +This library provides an OOP interface in PHP for composing messages/modals using Slack Block Kit. It also does the +reverse, meaning you can "hydrate" message/modal JSON into an object hierarchy. ## Block Kit Concepts @@ -41,7 +42,7 @@ but it does not validate or guard against every single rule. You may want to review the following concepts in the Slack documentation: -- [Surfaces](https://api.slack.com/surfaces) – There are 3 types: Message, Modal, and App Home +- [Surfaces](https://api.slack.com/surfaces) – There are 3 main types: Message, Modal, and App Home - [Blocks](https://api.slack.com/reference/block-kit/blocks) – Includes _section_, _context_, _actions_, and more - [Interactive Components](https://api.slack.com/reference/block-kit/interactive-components) – We call these "Inputs" in this library - [Composition Objects](https://api.slack.com/reference/block-kit/composition-objects) – We call these "Partials" ion the library @@ -58,21 +59,27 @@ composer require jeremeamia/slack-block-kit Then include the Composer-generated autoloader in your project's initialization code. -_Note: This library is built for PHP 7.2+._ +_Note: This library is built for PHP 7.3+._ ## Basic Usage -This library supports an intuitive syntax for composing Slack messages. The `Slack` class acts as a façade to the -entire library, and let's you start new messages. +This library supports an intuitive and fluid syntax for composing Slack surfaces (e.g., messages, modals). The `Kit` +class acts as a façade to the library, and let's you start new messages/modals. ```php text('Don\'t you just love XKCD?'); $msg->divider(); $msg->newImage() @@ -80,105 +87,132 @@ $msg->newImage() ->url('https://imgs.xkcd.com/comics/team_chat.png') ->altText('Comic about the stubbornness of some people switching chat clients'); -// Messages can be converted to JSON using PHP's regular `json_encode` function. -$json = json_encode($msg); +// To convert to JSON (to send to Slack API, webhook, or response_url), use PHP's `json_encode` function. +echo json_encode($msg); +// OR you can use the surfaces's `toJson` method, which also includes a convenience parameter for pretty printing. +echo $msg->toJson(true); ``` -### Renderers - -This library comes with 3 message/surface renderers out of the box. - -All the renderers can be accessed from the `Slack` façade class, as well. +### Fluid Interface -All the examples below will show the message above being rendered. +When using the fluid interface, every method that sets a property or adds a sub-element returns the original element's +object, so you can chain additional method calls. -#### JSON +```php +$msg = Message::new() + ->text('Don\'t you just love XKCD?'); + ->divider(); +``` -This renderer outputs JSON and is similar to just `json_encode`-ing the message. However, it will use the -pretty-print option. +Methods with a `new` prefix will return the new element's object, so be careful with how you are using the fluid +interface in those cases. ```php -echo Slack::newRenderer()->forJson()->render($msg); +// Correctly renders the whole message. +$msg = Message::new() + ->text('Don\'t you just love XKCD?') + ->divider(); +$msg->newImage() + ->title('Team Chat') + ->url('https://imgs.xkcd.com/comics/team_chat.png') + ->altText('Comic about the stubbornness of some people switching chat clients'); +echo json_encode($msg); +// YAY! + +// INCORRECT: Renders just the image, because only that element gets stored in the variable. +$msg = Message::new() + ->text('Don\'t you just love XKCD?') + ->divider() + ->newImage() + ->title('Team Chat') + ->url('https://imgs.xkcd.com/comics/team_chat.png') + ->altText('Comic about the stubbornness of some people switching chat clients'); +echo json_encode($msg); +// WHOOPS! ``` -##### Output +#### Tapping -``` -{ - "response_type": "ephemeral", - "blocks": [ - { - "type": "section", - "text": { - "type": "mrkdwn", - "text": "Don't you just love XKCD?" - } - }, - { - "type": "divider" - }, - { - "type": "image", - "title": { - "type": "plain_text", - "text": "Team Chat", - "emoji": true - }, - "image_url": "https:\/\/imgs.xkcd.com\/comics\/team_chat.png", - "alt_text": "Comic about the stubbornness of some people switching chat clients" - } - ] -} +Tapping is a way to keep the fluid interface going, but makes sure the whole message is preserved. + +```php +// Correctly renders the whole message, by using tap() +$msg = Message::new() + ->text('Don\'t you just love XKCD?') + ->divider() + ->tap(function (Message $msg) { + $msg->newImage() + ->title('Team Chat') + ->url('https://imgs.xkcd.com/comics/team_chat.png') + ->altText('Comic about the stubbornness of some people switching chat clients'); + }); +echo json_encode($msg); +// YAY! ``` -### Block Kit Builder +### Preview in Block Kit Builder -Slack provides an [interactive Block Kit Builder](https://api.slack.com/tools/block-kit-builder) for composing/testing -messages. This is a great way to play around with and learn the Block Kit format. +Slack provides an [interactive Block Kit Builder](https://app.slack.com/block-kit-builder) for composing/testing +messages and other surfaces. This is a great way to play around with and learn the Block Kit format. -The `KitBuilder` renderer allows you to render your message/surface as a Block Kit Builder link, so you can preview your -message in the browser as Slack would render it via their interactive tool. +The `Kit::preview` method allows you to render your message/surface as a Block Kit Builder URL, so you can link to a +preview or your message/surface in the browser via their interactive tool. This will help you see how it would be +rendered in a Slack client. ```php -echo Slack::newRenderer()->forKitBuilder()->render($msg); +$msg = Kit::newMessage() + ->text('Don\'t you just love XKCD?') + ->divider() + ->tap(function (Message $msg) { + $msg->newImage() + ->title('Team Chat') + ->url('https://imgs.xkcd.com/comics/team_chat.png') + ->altText('Comic about the stubbornness of some people switching chat clients'); + }); + +echo Kit::preview($msg); ``` -##### Output +#### Output ``` -https://api.slack.com/tools/block-kit-builder?mode=message&blocks=%5B%7B%22type%22%3A%22section%22%2C%22text%22%3A%7B%22type%22%3A%22mrkdwn%22%2C%22text%22%3A%22Don%27t%20you%20just%20love%20XKCD%3F%22%7D%7D%2C%7B%22type%22%3A%22divider%22%7D%2C%7B%22type%22%3A%22image%22%2C%22title%22%3A%7B%22type%22%3A%22plain_text%22%2C%22text%22%3A%22Team%20Chat%22%2C%22emoji%22%3Atrue%7D%2C%22image_url%22%3A%22https%3A%2F%2Fimgs.xkcd.com%2Fcomics%2Fteam_chat.png%22%2C%22alt_text%22%3A%22Comic%20about%20the%20stubbornness%20of%20some%20people%20switching%20chat%20clients%22%7D%5D +https://app.slack.com/block-kit-builder#%7B"blocks":%5B%7B"type":"section"%2C"text":%7B"type":"mrkdwn"%2C"text":"Don%27t%20you%20just%20love%20XKCD%3F"%7D%7D%2C%7B"type":"divider"%7D%2C%7B"type":"image"%2C"title":%7B"type":"plain_text"%2C"text":"Team%20Chat"%7D%2C"image_url":"https:%5C%2F%5C%2Fimgs.xkcd.com%5C%2Fcomics%5C%2Fteam_chat.png"%2C"alt_text":"Comic%20about%20the%20stubbornness%20of%20some%20people%20switching%20chat%20clients"%7D%5D%7D ``` -And here's the [actual Block Kit Builder link](https://api.slack.com/tools/block-kit-builder?mode=message&blocks=%5B%7B%22type%22%3A%22section%22%2C%22text%22%3A%7B%22type%22%3A%22mrkdwn%22%2C%22text%22%3A%22Don%27t%20you%20just%20love%20XKCD%3F%22%7D%7D%2C%7B%22type%22%3A%22divider%22%7D%2C%7B%22type%22%3A%22image%22%2C%22title%22%3A%7B%22type%22%3A%22plain_text%22%2C%22text%22%3A%22Team%20Chat%22%2C%22emoji%22%3Atrue%7D%2C%22image_url%22%3A%22https%3A%2F%2Fimgs.xkcd.com%2Fcomics%2Fteam_chat.png%22%2C%22alt_text%22%3A%22Comic%20about%20the%20stubbornness%20of%20some%20people%20switching%20chat%20clients%22%7D%5D). +And here's the [actual Block Kit Builder link](https://app.slack.com/block-kit-builder#%7B"blocks":%5B%7B"type":"section"%2C"text":%7B"type":"mrkdwn"%2C"text":"Don%27t%20you%20just%20love%20XKCD%3F"%7D%7D%2C%7B"type":"divider"%7D%2C%7B"type":"image"%2C"title":%7B"type":"plain_text"%2C"text":"Team%20Chat"%7D%2C"image_url":"https:%5C%2F%5C%2Fimgs.xkcd.com%5C%2Fcomics%5C%2Fteam_chat.png"%2C"alt_text":"Comic%20about%20the%20stubbornness%20of%20some%20people%20switching%20chat%20clients"%7D%5D%7D). It will show up in the Block Kit Builder looking something like this: ![Screenshot of rendered message in Block Kit Builder](block-kit-screenshot.png) -#### CLI +### Surface Hydration -Sometimes previewing the content of a message in the Terminal/CLI is useful, but the JSON representation can be -difficult to read. The CLI renderer will render to a more CLI-friendly format. +Some Slack application integrations (such as with Modals) require receiving the JSON of an existing surface and then +modifying or replacing that surface with another. You can "hydrate" the JSON of a surface (or element) into its object +representation using its `fromArray` method (or `fromJson`). ```php -echo Slack::newRenderer()->forCli()->render($msg); -``` +$messageJson = << See the YUML
-[Slack]-creates>[Renderer]
-[Slack]-creates>[Surface]
+[Kit]-creates>[Surface]
 [Surface]^[Message]
 [Surface]^[Modal]
 [Surface]^[AppHome]
 [Element]^[Surface]
 [Element]^[Block]
-[Renderer]^[Json]
-[Renderer]^[KitBuilder]
-[Renderer]^[Cli]
-[Surface]<>->[Block]
-
- - -### Blocks and Other Elements - -_Blocks_ are the primary element of the Block Kit. Blocks contain other elements that are grouped into _inputs_ -(interactive elements) and _partials_ (repeatable element parts that are not uniquely identifiable). - -![UML diagram for blocks](https://yuml.me/6bf6925a.png) - -
-See the YUML -
-[Element]^[Surface]
-[Element]^[Block]
 [Element]^[Input]
 [Element]^[Partial]
 [Surface]<>->[Block]
+[Block]<>->[Block]
 [Block]<>->[Input]
 [Block]<>->[Partial]
-[Input]-[note: examples:;Button;DatePicker{bg:cornsilk}]
-[Partial]-[note: examples:;Text;Fields{bg:cornsilk}]
-[Block]-[note: examples:;Section;Actions{bg:cornsilk}]
+[Input]-[note:Examples: Button
+DatePicker {bg:cornsilk}]
+[Partial]-[note: Examples: Text
+Fields {bg:cornsilk}]
+[Block]-[note: Examples: Section
+Actions {bg:cornsilk}]
 
diff --git a/src/Element.php b/src/Element.php index 08c57bd..b699a0b 100644 --- a/src/Element.php +++ b/src/Element.php @@ -96,10 +96,20 @@ public function toArray(): array return $data; } + public function toJson(bool $prettyPrint = false): string + { + $opts = JSON_THROW_ON_ERROR; + if ($prettyPrint) { + $opts |= JSON_PRETTY_PRINT; + } + + return (string) json_encode($this, $opts); + } + /** * @return array */ - final public function jsonSerialize() + public function jsonSerialize() { return $this->toArray(); } diff --git a/src/Kit.php b/src/Kit.php new file mode 100644 index 0000000..d1388a7 --- /dev/null +++ b/src/Kit.php @@ -0,0 +1,44 @@ +directives([]); + } elseif ($surface instanceof Surfaces\Attachment) { + // Block Kit Builder can only show an attachment within a message. + $surface = self::newMessage()->addAttachment($surface); + } elseif ($surface instanceof Surfaces\WorkflowStep) { + throw new Exception('The "workflow_step" surface is not compatible with Block Kit Builder'); + } + + $encoded = str_replace(['%22', '%3A'], ['"', ':'], rawurlencode($surface->toJson())); + + return "https://app.slack.com/block-kit-builder#{$encoded}"; + } +} diff --git a/src/Renderers/Cli.php b/src/Renderers/Cli.php deleted file mode 100644 index 0646761..0000000 --- a/src/Renderers/Cli.php +++ /dev/null @@ -1,114 +0,0 @@ -renderSurface($surface->toArray()); - } - - public function renderJson(string $json): string - { - return $this->renderSurface(json_decode($json, true)); - } - - private function renderSurface(array $data): string - { - $buffer = ''; - - // Special handling to messages to add type and visibility information. - if (!isset($data['type'])) { - $responseType = $data['response_type'] ?? 'ephemeral'; - $buffer .= '(•) '; - $buffer .= $responseType === 'ephemeral' - ? "Only visible to you\n" - : "Visible to channel\n"; - unset($data['response_type']); - $data['type'] = 'message'; - } - - $buffer .= $this->renderElements($data); - - return $buffer; - } - - /** - * Recursively renders the data from a surface and its blocks. - * - * @param array $array Surface data - * @param int $level Indentation level - * @return string - */ - private function renderElements(array $array, int $level = 0): string - { - // Start with an empty buffer. - $buffer = ''; - - // Setup indentation for current item. - $tab = str_repeat(self::TAB_CHAR, self::TAB_LENGTH); - $indent = str_repeat($tab, $level); - - // If the array is a list, render each element in the list. - if (isset($array[0])) { - foreach ($array as $subArray) { - $buffer .= $this->renderElements($subArray, $level); - } - return $buffer; - } - - // For elements with a type, indent and extract the type as a title for the section. - // Also, apply any custom renderings that are type-specific. - if (isset($array['type'])) { - $buffer .= $indent; - - $type = $array['type']; - unset($array['type']); - - // Render dividers as a line. - if ($type === Type::DIVIDER) { - $buffer .= str_repeat(self::DIVIDER_CHAR, self::DIVIDER_LENGTH) . "\n"; - return $buffer; - } - - // Extract IDs and place them in line with the title. - $buffer .= $type; - if (isset($array['block_id'])) { - $buffer .= " (id#{$array['block_id']})"; - unset($array['block_id']); - } - if (isset($array['action_id'])) { - $buffer .= " (id#{$array['action_id']})"; - unset($array['action_id']); - } - $buffer .= ":"; - - // If a text type, render the text inline and surrounded by quotes. - if ($type === Type::PLAINTEXT || $type === Type::MRKDWNTEXT) { - $buffer .= " \"{$array['text']}\"\n"; - return $buffer; - } else { - $buffer .= "\n"; - } - } - - // Render all properties of an element under it's title/type. - foreach ($array as $property => $value) { - $buffer .= "{$indent}{$tab}{$property}:"; - $buffer .= is_array($value) ? "\n{$this->renderElements($value, $level + 2)}" : " {$value}\n"; - } - - return $buffer; - } -} diff --git a/src/Renderers/Json.php b/src/Renderers/Json.php deleted file mode 100644 index 5609d90..0000000 --- a/src/Renderers/Json.php +++ /dev/null @@ -1,40 +0,0 @@ -encode($surface); - } - - public function renderJson(string $json): string - { - return $this->encode(json_decode($json)); - } - - /** - * @param object $object - * @return string - */ - private function encode(object $object): string - { - $json = json_encode($object, JSON_PRETTY_PRINT); - if (!is_string($json)) { - throw new Exception('Could not encode surface as JSON'); - } - - return $json; - } -} diff --git a/src/Renderers/KitBuilder.php b/src/Renderers/KitBuilder.php deleted file mode 100644 index 92d0b17..0000000 --- a/src/Renderers/KitBuilder.php +++ /dev/null @@ -1,62 +0,0 @@ -addAttachment($surface); - } elseif ($surface instanceof Message) { - $directives = new \ReflectionProperty($surface, 'directives'); - $directives->setAccessible(true); - $directives->setValue($surface, []); - } - - $content = $this->encode($surface); - - return $this->createLink($content); - } - - public function renderJson(string $json): string - { - $json = json_decode($json, true); - unset($json['response_type'], $json['replace_original'], $json['delete_original']); - $content = $this->encode($json); - - return $this->createLink($content); - } - - private function createLink(string $content): string - { - return "https://app.slack.com/block-kit-builder#" . rawurlencode($content); - } - - /** - * @param object|array $object - * @return string - */ - private function encode($object): string - { - $json = json_encode($object); - if (!is_string($json)) { - throw new Exception('Could not encode blocks as JSON for Kit Builder link'); - } - - return $json; - } -} diff --git a/src/Renderers/Renderer.php b/src/Renderers/Renderer.php deleted file mode 100644 index 6e4c023..0000000 --- a/src/Renderers/Renderer.php +++ /dev/null @@ -1,14 +0,0 @@ -toJson(); $this->assertJsonStringEqualsJsonString($beforeJson, $afterJson); } } diff --git a/tests/Surfaces/WorkflowStepTest.php b/tests/Surfaces/WorkflowStepTest.php index 92cd08c..b04a594 100644 --- a/tests/Surfaces/WorkflowStepTest.php +++ b/tests/Surfaces/WorkflowStepTest.php @@ -4,7 +4,7 @@ namespace Jeremeamia\Slack\BlockKit\Tests\Surfaces; -use Jeremeamia\Slack\BlockKit\Slack; +use Jeremeamia\Slack\BlockKit\Surfaces\WorkflowStep; use Jeremeamia\Slack\BlockKit\Tests\TestCase; /** @@ -14,7 +14,7 @@ class WorkflowStepTest extends TestCase { public function testCanConfigureTextInput() { - $workflowStep = Slack::newWorkflowStep() + $workflowStep = WorkflowStep::new() ->callbackId('my_step') ->privateMetadata('foo=bar'); $workflowStep->newInput() diff --git a/tests/TypeTest.php b/tests/TypeTest.php index 2042944..05971bd 100644 --- a/tests/TypeTest.php +++ b/tests/TypeTest.php @@ -3,7 +3,7 @@ namespace Jeremeamia\Slack\BlockKit\Tests; use Jeremeamia\Slack\BlockKit\Blocks\Section; -use Jeremeamia\Slack\BlockKit\{Exception, Slack, Type}; +use Jeremeamia\Slack\BlockKit\{Exception, Kit, Type}; /** * @covers \Jeremeamia\Slack\BlockKit\Type @@ -18,6 +18,6 @@ public function testCanMapDefinedElementClassToADefinedType() public function testThrowsErrorIfMappingClassesNotRegisteredInTypeMaps() { $this->expectException(Exception::class); - Type::mapClass(Slack::class); + Type::mapClass(Kit::class); } } diff --git a/tests/manual/attachments.php b/tests/manual/attachments.php index fbd4874..3babc7e 100644 --- a/tests/manual/attachments.php +++ b/tests/manual/attachments.php @@ -2,10 +2,9 @@ declare(strict_types=1); -use Jeremeamia\Slack\BlockKit\Slack; use Jeremeamia\Slack\BlockKit\Surfaces\Message; -require __DIR__ . '/../../vendor/autoload.php'; +require __DIR__ . '/bootstrap.php'; $msg = Message::new()->tap(function (Message $msg) { $msg->text('Primary Content'); @@ -14,6 +13,4 @@ $msg->newAttachment()->color('#0000ff')->text('Attachment 3'); }); -//echo Slack::newRenderer()->forJson()->render($msg) . "\n"; -echo Slack::newRenderer()->forKitBuilder()->render($msg) . "\n"; -// echo Slack::newRenderer()->forCli()->render($msg) . "\n"; +view($msg); diff --git a/tests/manual/bootstrap.php b/tests/manual/bootstrap.php new file mode 100644 index 0000000..6a9639b --- /dev/null +++ b/tests/manual/bootstrap.php @@ -0,0 +1,4 @@ +toJson(true) . "\n"; +} diff --git a/tests/manual/hydrate-message.php b/tests/manual/hydrate-message.php index 0643b06..b280b4d 100644 --- a/tests/manual/hydrate-message.php +++ b/tests/manual/hydrate-message.php @@ -2,10 +2,9 @@ declare(strict_types=1); -use Jeremeamia\Slack\BlockKit\Slack; use Jeremeamia\Slack\BlockKit\Surfaces\Message; -require __DIR__ . '/../../vendor/autoload.php'; +require __DIR__ . '/bootstrap.php'; $json = <<forJson()->render($message) . "\n"; -echo Slack::newRenderer()->forKitBuilder()->render($message) . "\n"; +view($message); diff --git a/tests/manual/hydrate-modal.php b/tests/manual/hydrate-modal.php index a7810df..9414816 100644 --- a/tests/manual/hydrate-modal.php +++ b/tests/manual/hydrate-modal.php @@ -2,10 +2,9 @@ declare(strict_types=1); -use Jeremeamia\Slack\BlockKit\Slack; use Jeremeamia\Slack\BlockKit\Surfaces\Modal; -require __DIR__ . '/../../vendor/autoload.php'; +require __DIR__ . '/bootstrap.php'; $modal = Modal::new() ->title('Foo Bar') @@ -21,8 +20,7 @@ $m->newInput('id2')->label('Stuff')->newTextInput('a1')->placeholder('type something'); }); -$json = json_encode($modal); +$json = $modal->toJson(); $hydratedModal = Modal::fromJson($json); -assert($json === json_encode($hydratedModal)); -echo Slack::newRenderer()->forJson()->render($hydratedModal) . "\n"; -echo Slack::newRenderer()->forKitBuilder()->render($hydratedModal) . "\n"; +assert($json === $hydratedModal->toJson()); +view($hydratedModal); diff --git a/tests/manual/menus.php b/tests/manual/menus.php index 2cb5010..934d0d3 100644 --- a/tests/manual/menus.php +++ b/tests/manual/menus.php @@ -2,12 +2,12 @@ declare(strict_types=1); +use Jeremeamia\Slack\BlockKit\Kit; use Jeremeamia\Slack\BlockKit\Partials\Confirm; -use Jeremeamia\Slack\BlockKit\Slack; -require __DIR__ . '/../../vendor/autoload.php'; +require __DIR__ . '/bootstrap.php'; -$msg = Slack::newMessage(); +$msg = Kit::newMessage(); $actions = $msg->newActions('b1'); $actions->newSelectMenu('m1') ->forStaticOptions() @@ -70,6 +70,5 @@ ->urlOption('bar', 'bar', 'https://example.org') ->option('foobar', 'foobar') ->setConfirm(new Confirm('Choose', 'Do you really want to choose this?', 'Yes choose')); -//echo Slack::newRenderer()->forJson()->render($msg) . "\n"; -echo Slack::newRenderer()->forKitBuilder()->render($msg) . "\n"; -// echo Slack::newRenderer()->forCli()->render($msg) . "\n"; + +view($msg); diff --git a/tests/manual/message-chained.php b/tests/manual/message-chained.php index c80b1cd..01539ef 100644 --- a/tests/manual/message-chained.php +++ b/tests/manual/message-chained.php @@ -5,10 +5,9 @@ use Jeremeamia\Slack\BlockKit\Blocks\{Actions, Context, Header, Image, Section}; use Jeremeamia\Slack\BlockKit\Inputs\Button; use Jeremeamia\Slack\BlockKit\Inputs\DatePicker; -use Jeremeamia\Slack\BlockKit\Renderers; use Jeremeamia\Slack\BlockKit\Surfaces\Message; -require __DIR__ . '/../../vendor/autoload.php'; +require __DIR__ . '/bootstrap.php'; $msg = Message::new() ->add(Header::new()->text('Header')) @@ -42,6 +41,4 @@ ->initialDate('2020-01-01') ->confirm('Proceed?', 'If this is correct, click "OK".'))); -// echo (new Renderers\Json())->render($msg) . "\n"; -echo (new Renderers\KitBuilder())->render($msg) . "\n"; -// echo (new Renderers\Cli())->render($msg) . "\n"; +view($msg); diff --git a/tests/manual/message-standard.php b/tests/manual/message-standard.php index 588dfd3..75fa993 100644 --- a/tests/manual/message-standard.php +++ b/tests/manual/message-standard.php @@ -2,11 +2,11 @@ declare(strict_types=1); -use Jeremeamia\Slack\BlockKit\Slack; +use Jeremeamia\Slack\BlockKit\Kit; -require __DIR__ . '/../../vendor/autoload.php'; +require __DIR__ . '/bootstrap.php'; -$msg = Slack::newMessage(); +$msg = Kit::newMessage(); $msg->newSection('b1') ->mrkdwnText('*foo* _bar_') ->fieldMap(['foo' => 'bar', 'fizz' => 'buzz']) @@ -41,6 +41,4 @@ ->initialDate('2020-01-01') ->confirm('Proceed?', 'If this is correct, click "OK".'); -// echo Slack::newRenderer()->forJson()->render($msg) . "\n"; -echo Slack::newRenderer()->forKitBuilder()->render($msg) . "\n"; -// echo Slack::newRenderer()->forCli()->render($msg) . "\n"; +view($msg); diff --git a/tests/manual/message-tapped.php b/tests/manual/message-tapped.php index 05e2c0d..201da71 100644 --- a/tests/manual/message-tapped.php +++ b/tests/manual/message-tapped.php @@ -4,12 +4,12 @@ use Jeremeamia\Slack\BlockKit\Blocks\Actions; use Jeremeamia\Slack\BlockKit\Blocks\Section; -use Jeremeamia\Slack\BlockKit\Slack; +use Jeremeamia\Slack\BlockKit\Kit; use Jeremeamia\Slack\BlockKit\Surfaces\Message; -require __DIR__ . '/../../vendor/autoload.php'; +require __DIR__ . '/bootstrap.php'; -$msg = Slack::newMessage()->tap(function (Message $msg) { +$msg = Kit::newMessage()->tap(function (Message $msg) { $msg->newSection('b1') ->mrkdwnText('*foo* _bar_') ->fieldMap(['foo' => 'bar', 'fizz' => 'buzz']) @@ -49,6 +49,4 @@ }); }); -// echo Slack::newRenderer()->forJson()->render($msg) . "\n"; -echo Slack::newRenderer()->forKitBuilder()->render($msg) . "\n"; -// echo Slack::newRenderer()->forCli()->render($msg) . "\n"; +view($msg); diff --git a/tests/manual/modal.php b/tests/manual/modal.php index e83ce08..6c4189e 100644 --- a/tests/manual/modal.php +++ b/tests/manual/modal.php @@ -3,12 +3,11 @@ declare(strict_types=1); use Jeremeamia\Slack\BlockKit\Partials\Confirm; -use Jeremeamia\Slack\BlockKit\Slack; -use Jeremeamia\Slack\BlockKit\Partials\Option; +use Jeremeamia\Slack\BlockKit\Kit; -require __DIR__ . '/../../vendor/autoload.php'; +require __DIR__ . '/bootstrap.php'; -$msg = Slack::newModal() +$msg = Kit::newModal() ->title('My Modal') ->submit('Submit') ->close('Cancel') @@ -41,6 +40,4 @@ ->option('foobar', 'foobar', true) ->setConfirm(new Confirm('Switch', 'Do you really want to switch?', 'Yes switch')); -// echo Slack::newRenderer()->forJson()->render($msg) . "\n"; -echo Slack::newRenderer()->forKitBuilder()->render($msg) . "\n"; -// echo Slack::newRenderer()->forCli()->render($msg) . "\n"; +view($msg); diff --git a/tests/manual/two-col-table.php b/tests/manual/two-col-table.php index 0c388a2..ef6c2f0 100644 --- a/tests/manual/two-col-table.php +++ b/tests/manual/two-col-table.php @@ -2,11 +2,11 @@ declare(strict_types=1); -use Jeremeamia\Slack\BlockKit\Slack; +use Jeremeamia\Slack\BlockKit\Kit; -require __DIR__ . '/../../vendor/autoload.php'; +require __DIR__ . '/bootstrap.php'; -$msg = Slack::newMessage(); +$msg = Kit::newMessage(); $msg->newTwoColumnTable('table1') ->caption('My Kids') ->cols('Name', 'Age') @@ -35,6 +35,4 @@ 'Emmy' => '0', ]); -// echo Slack::newRenderer()->forJson()->render($msg) . "\n"; -echo Slack::newRenderer()->forKitBuilder()->render($msg) . "\n"; -// echo Slack::newRenderer()->forCli()->render($msg) . "\n"; +view($msg); diff --git a/tests/manual/workflow-step.php b/tests/manual/workflow-step.php index 36ce1b5..ed1a529 100644 --- a/tests/manual/workflow-step.php +++ b/tests/manual/workflow-step.php @@ -3,34 +3,34 @@ declare(strict_types=1); use Jeremeamia\Slack\BlockKit\Partials\Confirm; -use Jeremeamia\Slack\BlockKit\Slack; +use Jeremeamia\Slack\BlockKit\Surfaces\WorkflowStep; -require __DIR__ . '/../../vendor/autoload.php'; +require __DIR__ . '/bootstrap.php'; -$msg = Slack::newWorkflowStep() +$step = WorkflowStep::new() ->privateMetadata('foo=bar') ->callbackId('my_foo') ->text('Hello!', 'b1'); -$msg->newInput('b2') +$step->newInput('b2') ->label('Date') ->newDatePicker('a1') ->placeholder('Choose a date') ->initialDate('2020-01-01'); -$msg->newInput('c1') +$step->newInput('c1') ->label('Multiline') ->newTextInput('text_input') ->placeholder('Text Input') ->multiline(true) ->minLength(10) ->maxLength(100); -$msg->newInput('c2') +$step->newInput('c2') ->label('Radio Buttons') ->newRadioButtons('radio_buttons') ->option('foo', 'foo') ->option('bar', 'bar', true) ->option('foobar', 'foobar') ->setConfirm(new Confirm('Switch', 'Do you really want to switch?', 'Yes switch')); -$msg->newInput('c3') +$step->newInput('c3') ->label('Checkboxes') ->newCheckboxes('checkboxes') ->option('foo', 'foo') @@ -38,6 +38,4 @@ ->option('foobar', 'foobar', true) ->setConfirm(new Confirm('Switch', 'Do you really want to switch?', 'Yes switch')); -echo Slack::newRenderer()->forJson()->render($msg) . "\n"; -// echo Slack::newRenderer()->forKitBuilder()->render($msg) . "\n"; -// echo Slack::newRenderer()->forCli()->render($msg) . "\n"; +echo "JSON: {$step->toJson(true)}\n";