From 181365f456b4ba27eb0d596f39933266024afe2b Mon Sep 17 00:00:00 2001 From: Deon George Date: Tue, 22 Feb 2022 17:47:32 +1100 Subject: [PATCH] Added Input/PlainTextInput, some minor fixes --- src/API.php | 5 +- src/Blockkit/Blocks/Elements/Button.php | 1 - src/Blockkit/Blocks/Elements/Confirm.php | 1 - .../Blocks/Elements/MultiStaticSelect.php | 3 +- src/Blockkit/Blocks/Elements/Options.php | 3 +- .../Blocks/Elements/PlaintTextInput.php | 78 ++++++++++++++++ src/Blockkit/Blocks/Elements/Text.php | 5 +- src/Blockkit/Blocks/Input.php | 92 +++++++++++++++++++ src/Blockkit/Input/Element.php | 22 ----- src/Blockkit/Modal.php | 10 +- src/Message.php | 30 +++--- src/Message/Attachment.php | 2 +- 12 files changed, 199 insertions(+), 53 deletions(-) create mode 100644 src/Blockkit/Blocks/Elements/PlaintTextInput.php create mode 100644 src/Blockkit/Blocks/Input.php delete mode 100644 src/Blockkit/Input/Element.php diff --git a/src/API.php b/src/API.php index bd0a7a7..41bd5b3 100644 --- a/src/API.php +++ b/src/API.php @@ -5,6 +5,7 @@ namespace Slack; use Illuminate\Support\Arr; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Log; +use Slack\Blockkit\Modal; use Slack\Response\Chat; use Slack\Exceptions\{SlackAlreadyPinnedException, SlackChannelNotFoundException, @@ -311,11 +312,11 @@ final class API return new ChannelList($this->execute('users.conversations',$args->toArray())); } - public function viewOpen(string $trigger,string $view): Generic + public function viewOpen(string $trigger,Modal $view): Generic { Log::debug(sprintf('%s:Open a view',static::LOGKEY),['m'=>__METHOD__,'t'=>$trigger]); - return new Generic($this->execute('views.open',json_encode(['trigger_id'=>$trigger,'view'=>$view]))); + return new Generic($this->execute('views.open',json_encode(['trigger_id'=>$trigger,'view'=>json_encode($view)]))); } /** diff --git a/src/Blockkit/Blocks/Elements/Button.php b/src/Blockkit/Blocks/Elements/Button.php index be9be9e..65cc630 100644 --- a/src/Blockkit/Blocks/Elements/Button.php +++ b/src/Blockkit/Blocks/Elements/Button.php @@ -3,7 +3,6 @@ namespace Slack\Blockkit\Blocks\Elements; use \Exception; -use Slack\BlockKit; use Slack\Blockkit\Element; final class Button extends Element diff --git a/src/Blockkit/Blocks/Elements/Confirm.php b/src/Blockkit/Blocks/Elements/Confirm.php index 1a6a1c3..841e8f0 100644 --- a/src/Blockkit/Blocks/Elements/Confirm.php +++ b/src/Blockkit/Blocks/Elements/Confirm.php @@ -3,7 +3,6 @@ namespace Slack\Blockkit\Blocks\Elements; use \Exception; -use Slack\BlockKit; use Slack\Blockkit\Element; final class Confirm extends Element diff --git a/src/Blockkit/Blocks/Elements/MultiStaticSelect.php b/src/Blockkit/Blocks/Elements/MultiStaticSelect.php index c36622b..5763d5c 100644 --- a/src/Blockkit/Blocks/Elements/MultiStaticSelect.php +++ b/src/Blockkit/Blocks/Elements/MultiStaticSelect.php @@ -4,7 +4,6 @@ namespace Slack\Blockkit\Blocks\Elements; use \Exception; use Illuminate\Support\Collection; -use Slack\BlockKit\Blocks; use Slack\Blockkit\Element; final class MultiStaticSelect extends Element @@ -64,7 +63,7 @@ final class MultiStaticSelect extends Element public function focus_on_load(bool $bool): self { - $this->focus_on_load = $bool ? 'true' : 'false'; + $this->focus_on_load = $bool; return $this; } diff --git a/src/Blockkit/Blocks/Elements/Options.php b/src/Blockkit/Blocks/Elements/Options.php index 7cc8c2c..f48bb55 100644 --- a/src/Blockkit/Blocks/Elements/Options.php +++ b/src/Blockkit/Blocks/Elements/Options.php @@ -3,7 +3,6 @@ namespace Slack\Blockkit\Blocks\Elements; use \Exception; -use Slack\BlockKit; use Slack\Blockkit\Element; /** @@ -21,6 +20,8 @@ final class Options extends Element public function __construct(Text $text,string $value) { + parent::__construct(); + if (strlen($text->text) > self::LIMITS['text']) throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['text'])); diff --git a/src/Blockkit/Blocks/Elements/PlaintTextInput.php b/src/Blockkit/Blocks/Elements/PlaintTextInput.php new file mode 100644 index 0000000..bc47dee --- /dev/null +++ b/src/Blockkit/Blocks/Elements/PlaintTextInput.php @@ -0,0 +1,78 @@ + 255, // @todo Should be unique for each message + 'placeholder' => 150, + ]; + + private const MAX_MIN_LENGTH = 3000; + + // @todo dispatch_action_config + // @todo focus_on_load + + public function __construct(string $action_id) + { + parent::__construct(); + + $this->type = 'plain_text_input'; + + $this->action_id = $this->validate('action_id',$action_id); + } + + public static function item(string $action_id): self + { + return new self($action_id); + } + + /* OPTIONAL ITEMS */ + + public function initial_value(string $text): self + { + $this->initial_value = $text; + + return $this; + } + + public function min_length(int $int): self + { + if ($int > self::MAX_MIN_LENGTH) + throw new Exception(sprintf('min_length must be less than %d',self::MAX_MIN_LENGTH)); + + $this->min_length = $int; + + return $this; + } + + public function max_length(int $int): self + { + if ($this->min_length && ($int < $this->min_length)) + throw new Exception('max_length must be greater than min_length'); + + $this->max_length = $int; + + return $this; + } + + public function multiline(bool $bool): self + { + $this->multiline = $bool; + + return $this; + } + + public function placeholder(string $text): self + { + $this->placeholder = $this->validate('placeholder',$text); + + return $this; + } +} \ No newline at end of file diff --git a/src/Blockkit/Blocks/Elements/Text.php b/src/Blockkit/Blocks/Elements/Text.php index 73adb4f..f098a9d 100644 --- a/src/Blockkit/Blocks/Elements/Text.php +++ b/src/Blockkit/Blocks/Elements/Text.php @@ -3,7 +3,6 @@ namespace Slack\Blockkit\Blocks\Elements; use \Exception; -use Slack\BlockKit; use Slack\Blockkit\Element; final class Text extends Element @@ -33,14 +32,14 @@ final class Text extends Element if ($x=$this->type != 'plain_text') throw new Exception(sprintf('Cannnot use emoji when type is [%s]',$x)); - $this->emoji = $bool ? 'true' : 'false'; + $this->emoji = $bool; return $this; } public function verbatim(bool $bool): self { - $this->verbatim = $bool ? 'true' : 'false'; + $this->verbatim = $bool; return $this; } diff --git a/src/Blockkit/Blocks/Input.php b/src/Blockkit/Blocks/Input.php new file mode 100644 index 0000000..71bd414 --- /dev/null +++ b/src/Blockkit/Blocks/Input.php @@ -0,0 +1,92 @@ + 255, // @todo Should be unique for each message + 'hint' => 2000, + 'label' => 2000, + ]; + + private const VALID_ELEMENTS = [ + Elements\PlaintTextInput::class, + Elements\MultiStaticSelect::class + ]; + + // @todo dispatch_action + + /** + * @param Text|NULL $text not required if fields is provided + * @throws Exception + */ + public function __construct(Elements\Text $label=NULL) + { + parent::__construct(); + + if ($label->type != 'plain_text') + throw new Exception(sprintf('Text must be plain_text not %s',$label->type)); + + // Defaults + $this->type = 'input'; + + if (strlen($label->text) > self::LIMITS['label']) + throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['label'])); + + $this->label = $label; + } + + public static function item(Elements\Text $label=NULL): self + { + return new self($label); + } + + public function jsonSerialize() + { + if (! $this->element) + throw new Exception('Must define an element'); + + return parent::jsonSerialize(); + } + + /* OPTIONAL ITEMS */ + + public function block_id(string $string): self + { + $this->block_id = $this->validate('block_id',$string); + + return $this; + } + + public function element(Element $object): self + { + if (! in_array(get_class($object),self::VALID_ELEMENTS)) + throw new Exception(sprintf('Invalid element [%s] added to input',get_class($object))); + + $this->element = $object; + + return $this; + } + + public function hint(Elements\Text $text): self + { + if (strlen($text->text) > self::LIMITS['hint']) + throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['hint'])); + + $this->hint = $text; + + return $this; + } + + public function optional(bool $bool): self + { + $this->optional = $bool; + + return $this; + } +} \ No newline at end of file diff --git a/src/Blockkit/Input/Element.php b/src/Blockkit/Input/Element.php deleted file mode 100644 index f02c7a6..0000000 --- a/src/Blockkit/Input/Element.php +++ /dev/null @@ -1,22 +0,0 @@ -type = $type; - $this->action_id = $action_id; - $this->multiline = $multiline; - } - - public static function item(string $type,string $action_id,bool $multiline=FALSE): self - { - return new self($type,$action_id,$multiline); - } -} \ No newline at end of file diff --git a/src/Blockkit/Modal.php b/src/Blockkit/Modal.php index ab67d6a..8761304 100644 --- a/src/Blockkit/Modal.php +++ b/src/Blockkit/Modal.php @@ -17,6 +17,7 @@ final class Modal extends BlockKit 'callback_id' => 255, 'close' => 24, 'private_metadata' => 3000, + 'submit' => 24, 'text' => 24, ]; @@ -52,8 +53,9 @@ final class Modal extends BlockKit /** * Add a block to this modal * - * @param BlockKit $block + * @param Blocks $block * @return $this + * @throws Exception */ public function addBlock(Blocks $block): self { @@ -97,7 +99,7 @@ final class Modal extends BlockKit if ($this->type != 'modal') throw new Exception(sprintf('clear_on_close is not required for %s',$type)); - $this->clear_on_close = $bool ? 'true' : 'false'; + $this->clear_on_close = $bool; return $this; } @@ -150,7 +152,7 @@ final class Modal extends BlockKit if ($this->type != 'modal') throw new Exception(sprintf('notify_on_close is not required for %s',$type)); - $this->notify_on_close = $bool ? 'true' : 'false'; + $this->notify_on_close = $bool; return $this; } @@ -191,7 +193,7 @@ final class Modal extends BlockKit if ($this->type != 'modal') throw new Exception(sprintf('submit_disabled is not required for %s',$type)); - $this->submit_disabled = $bool ? 'true' : 'false'; + $this->submit_disabled = $bool; return $this; } diff --git a/src/Message.php b/src/Message.php index aab4399..7b6aa05 100644 --- a/src/Message.php +++ b/src/Message.php @@ -56,6 +56,16 @@ final class Message extends BlockKit } } + /** + * Empty the message + * + * @return Message + */ + public static function blank(): self + { + return new self; + } + /* HELPER METHODS */ /** @@ -88,18 +98,6 @@ final class Message extends BlockKit return $this; } - /** - * Empty the message - * - * @return Message - */ - public function blank(): self - { - $this->_data = collect(); - - return $this; - } - public function forgetTS(): self { $this->_data->forget('ts'); @@ -269,7 +267,7 @@ final class Message extends BlockKit */ public function ephemeral(): self { - $this->ephemeral = 'true'; + $this->ephemeral = TRUE; return $this; } @@ -306,7 +304,7 @@ final class Message extends BlockKit */ public function replace_original(bool $bool=TRUE): self { - $this->replace_original = $bool ? 'true' : 'false'; + $this->replace_original = $bool; return $this; } @@ -358,7 +356,7 @@ final class Message extends BlockKit */ public function unfurl_links(bool $bool): self { - $this->unfurl_links = $bool ? 'true' : 'false'; + $this->unfurl_links = $bool; return $this; } @@ -443,4 +441,4 @@ final class Message extends BlockKit return $this; } -} +} \ No newline at end of file diff --git a/src/Message/Attachment.php b/src/Message/Attachment.php index e4cb762..3b81ae7 100644 --- a/src/Message/Attachment.php +++ b/src/Message/Attachment.php @@ -68,7 +68,7 @@ final class Attachment extends BlockKit if (! Arr::get($this->_data,'fields')) $this->fields = collect(); - $this->fields->push(['title'=>$title,'value'=>$value,'short'=>$short ? 'true' : 'false']); + $this->fields->push(['title'=>$title,'value'=>$value,'short'=>$short]); return $this; }