diff --git a/src/Blockkit/Blocks/Elements/PlaintTextInput.php b/src/Blockkit/Blocks/Elements/PlaintTextInput.php index bc47dee..4478673 100644 --- a/src/Blockkit/Blocks/Elements/PlaintTextInput.php +++ b/src/Blockkit/Blocks/Elements/PlaintTextInput.php @@ -69,9 +69,12 @@ final class PlaintTextInput extends Element return $this; } - public function placeholder(string $text): self + public function placeholder(Text $text): self { - $this->placeholder = $this->validate('placeholder',$text); + if (strlen($text->text) > self::LIMITS['placeholder']) + throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['placeholder'])); + + $this->placeholder = $text; return $this; } diff --git a/src/Message.php b/src/Message.php index 7b6aa05..ac278e2 100644 --- a/src/Message.php +++ b/src/Message.php @@ -98,6 +98,13 @@ final class Message extends BlockKit return $this; } + public function clearAttachments(): self + { + $this->attachments = ''; + + return $this; + } + public function forgetTS(): self { $this->_data->forget('ts'); diff --git a/src/Message/Attachment.php b/src/Message/Attachment.php index 3b81ae7..6a49535 100644 --- a/src/Message/Attachment.php +++ b/src/Message/Attachment.php @@ -33,22 +33,12 @@ final class Attachment extends BlockKit * @note These are now legacy - use blocks instead * @note more information on this available https://api.slack.com/legacy/interactive-message-field-guide */ - public function addAction(string $name,string $text,string $type,string $value,string $style=NULL,array $confirm=NULL): self + public function addAction(AttachmentAction $action): self { - if (! Arr::get($this->_data,'actions')) { + if (! Arr::get($this->_data,'actions')) $this->actions = collect(); - $this->attachment_type = 'default'; - } - $action = ['name'=>$name,'text'=>$text,'type'=>$type,'value'=>$value]; - if ($style) - $action['style'] = $style; - - // Confirm dialog is an array of 'title','text','ok_text','dismiss_text' - if ($confirm) - $action['confirm'] = $confirm; - - $this->actions->push(); + $this->actions->push($action); return $this; } diff --git a/src/Message/AttachmentAction.php b/src/Message/AttachmentAction.php new file mode 100644 index 0000000..a7bf55b --- /dev/null +++ b/src/Message/AttachmentAction.php @@ -0,0 +1,125 @@ + 30, + 'value' => 2000, + ]; + + private const DATA_SOURCES = ['static','users','channels','conversastions','external']; + private const STYLES = ['default','danger','primary']; + private const TYPES = ['button','select']; + + // @todo options + // @todo option_groups + // @todo selected_options + + public function __construct(string $text,string $name,string $type) + { + parent::__construct(); + + if (! in_array($type,self::TYPES)) + throw new Exception(sprintf('Type [%s] not valid',$type)); + + $this->type = $type; + switch ($type) { + case 'button': + $this->action_id = $name; + $this->text = Text::item($this->validate('text',$text),'plain_text'); + break; + + case 'select': + $this->name = $name; + $this->text = $this->validate('text',$text); + break; + } + } + + public static function item(string $text,string $name,string $type): self + { + return new self($text,$name,$type); + } + + /* OPTIONAL ITEMS */ + + /** + * Set a confirmation diaglog when this action is selected + * + * @param string $title + * @param string $text + * @param string $ok_text + * @param string $dismiss_text + * @return $this + */ + public function confirm(string $title,string $text,string $ok_text,string $dismiss_text): self + { + $this->confirm = [ + 'title' => $title, + 'text' => $text, + 'ok_text' => $ok_text, + 'dismiss_text' => $dismiss_text + ]; + + return $this; + } + + public function data_source(string $string): self + { + if (! in_array($string,self::DATA_SOURCES)) + throw new Exception(sprintf('Type [%s] not valid',$string)); + + $this->data_source = $string; + + return $this; + } + + public function min_query_length(int $int): self + { + $this->min_query_length = $int; + + return $this; + } + + /** + * Set the text displayed in the action + * + * @param string $type + * @return $this + */ + public function style(string $string): self + { + if (! in_array($string,self::STYLES)) + throw new Exception(sprintf('Type [%s] not valid',$string)); + + $this->style = $string; + + return $this; + } + + /** + * Set the value for the action + * + * @param string $string + * @return $this + */ + public function value(string $string): self + { + $this->value = $this->validate('value',$string); + + return $this; + } +} \ No newline at end of file