diff --git a/src/Blockkit/Blocks/Accessories/Overflow.php b/src/Blockkit/Blocks/Accessories/Overflow.php index 3eedb74..fa2429e 100644 --- a/src/Blockkit/Blocks/Accessories/Overflow.php +++ b/src/Blockkit/Blocks/Accessories/Overflow.php @@ -11,12 +11,12 @@ use Slack\Exceptions\SlackSyntaxException; final class Overflow extends Element { - protected const LIMITS = [ + public const LIMITS = [ 'action_id' => 255, ]; - private const MIN_OPTIONS = 1; - private const MAX_OPTIONS = 5; + public const MIN_OPTIONS = 1; + public const MAX_OPTIONS = 5; /** * @param string $action_id diff --git a/src/Blockkit/Blocks/Actions.php b/src/Blockkit/Blocks/Actions.php index 96f17a9..34bf3ff 100644 --- a/src/Blockkit/Blocks/Actions.php +++ b/src/Blockkit/Blocks/Actions.php @@ -10,13 +10,13 @@ use Slack\Exceptions\SlackSyntaxException; final class Actions extends Blocks { - protected const LIMITS = [ + public const LIMITS = [ 'block_id' => 255, // @todo Should be unique for each message ]; - private const MAX_ELEMENTS = 5; + public const MAX_ELEMENTS = 5; - private const VALID_ELEMENTS = [ + public const VALID_ELEMENTS = [ Button::class, MultiStaticSelect::class, Blocks\Accessories\Overflow::class, diff --git a/src/Blockkit/Blocks/Context.php b/src/Blockkit/Blocks/Context.php index 9f1a7a2..9649490 100644 --- a/src/Blockkit/Blocks/Context.php +++ b/src/Blockkit/Blocks/Context.php @@ -9,11 +9,11 @@ use Slack\Exceptions\SlackSyntaxException; final class Context extends Blocks { - protected const LIMITS = [ + public const LIMITS = [ 'block_id' => 255, // @todo Should be unique for each message ]; - private const MAX_ELEMENTS = 10; + public const MAX_ELEMENTS = 10; /** * @param Collection $collection diff --git a/src/Blockkit/Blocks/Elements/Button.php b/src/Blockkit/Blocks/Elements/Button.php index 5ee61f2..a384b6e 100644 --- a/src/Blockkit/Blocks/Elements/Button.php +++ b/src/Blockkit/Blocks/Elements/Button.php @@ -15,26 +15,19 @@ final class Button extends Element 'value' => 2000, ]; - public function __construct(Text $text,string $value,string $action_id) + public function __construct(string $text,string $value,string $action_id) { parent::__construct(); // Defaults $this->type = 'button'; - if ($text->type != 'plain_text') - throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$text->type)); - - if (strlen($text->text) > self::LIMITS['text']) - throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['text'])); - - $this->text = $text; - + $this->text = Text::item($this->validate('text',$text),'plain_text'); $this->value = $this->validate('value',$value); $this->action_id = $this->validate('action_id',$action_id); } - public static function item(Text $text,string $value,string $action_id): self + public static function item(string $text,string $value,string $action_id): self { return new self($text,$value,$action_id); } diff --git a/src/Blockkit/Blocks/Elements/Confirm.php b/src/Blockkit/Blocks/Elements/Confirm.php index 2972a1a..f4a1ed3 100644 --- a/src/Blockkit/Blocks/Elements/Confirm.php +++ b/src/Blockkit/Blocks/Elements/Confirm.php @@ -14,29 +14,17 @@ final class Confirm extends Element 'deny' => 30, ]; - public function __construct(Text $title,Text $text,Text $confirm,Text $deny) + public function __construct(string $title,Text $text,string $confirm,string $deny) { parent::__construct(); - if ($title->type != 'plain_text') - throw new SlackSyntaxException(sprintf('Title must be plain_text not %s',$title->type)); - - if ($confirm->type != 'plain_text') - throw new SlackSyntaxException(sprintf('Title must be plain_text not %s',$confirm->type)); - - if ($deny->type != 'plain_text') - throw new SlackSyntaxException(sprintf('Title must be plain_text not %s',$deny->type)); - - $this->title = $this->validate('title',$title->text) ? $title : NULL; - + $this->title = Text::item($this->validate('title',$title),'plain_text'); $this->text = $this->validate('text',$text->text) ? $text : NULL; - - $this->confirm = $this->validate('confirm',$confirm->text) ? $confirm : NULL; - - $this->deny = $this->validate('deny',$deny->text) ? $deny : NULL; + $this->confirm = Text::item($this->validate('confirm',$confirm),'plain_text'); + $this->deny = Text::item($this->validate('deny',$deny),'plain_text'); } - public static function item(Text $title,Text $text,Text $confirm,Text $deny): self + public static function item(string $title,Text $text,string $confirm,string $deny): self { return new self($title,$text,$confirm,$deny); } diff --git a/src/Blockkit/Blocks/Elements/ExternalSelect.php b/src/Blockkit/Blocks/Elements/ExternalSelect.php index 0dd4582..b95a95f 100644 --- a/src/Blockkit/Blocks/Elements/ExternalSelect.php +++ b/src/Blockkit/Blocks/Elements/ExternalSelect.php @@ -13,29 +13,22 @@ final class ExternalSelect extends Element ]; /** - * @param Text $placeholder + * @param string $placeholder * @param string $action_id * @throws SlackSyntaxException */ - public function __construct(Text $placeholder,string $action_id) + public function __construct(string $placeholder,string $action_id) { parent::__construct(); // Defaults $this->type = 'external_select'; - if ($placeholder->type != 'plain_text') - throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$placeholder->type)); - - if (strlen($placeholder->text) > self::LIMITS['placeholder']) - throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['placeholder'])); - - $this->placeholder = $placeholder; - + $this->placeholder = Text::item($this->validate('placeholder',$placeholder),'plain_text'); $this->action_id = $this->validate('action_id',$action_id); } - public static function item(Text $placeholder,string $action_id): self + public static function item(string $placeholder,string $action_id): self { return new self($placeholder,$action_id); } diff --git a/src/Blockkit/Blocks/Elements/MultiExternalSelect.php b/src/Blockkit/Blocks/Elements/MultiExternalSelect.php index 695db5b..ed54898 100644 --- a/src/Blockkit/Blocks/Elements/MultiExternalSelect.php +++ b/src/Blockkit/Blocks/Elements/MultiExternalSelect.php @@ -9,40 +9,32 @@ use Slack\Exceptions\SlackSyntaxException; final class MultiExternalSelect extends Element { - protected const LIMITS = [ + public const LIMITS = [ 'action_id' => 255, 'placeholder' => 150, ]; - private const MAX_OPTIONS = 100; + public const MAX_OPTIONS = 100; // @todo option_group? (double check it is applicable to this item) /** - * @param Text $placeholder + * @param string $placeholder * @param string $action_id * @throws SlackSyntaxException - * @todo We dont handle option_groups yet. */ - public function __construct(Text $placeholder,string $action_id) + public function __construct(string $placeholder,string $action_id) { parent::__construct(); // Defaults $this->type = 'multi_external_select'; - if ($placeholder->type != 'plain_text') - throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$placeholder->type)); - - if (strlen($placeholder->text) > self::LIMITS['placeholder']) - throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['placeholder'])); - - $this->placeholder = $placeholder; - + $this->placeholder = Text::item($this->validate('placeholder',$placeholder),'plain_text'); $this->action_id = $this->validate('action_id',$action_id); } - public static function item(Text $placeholder,string $action_id): self + public static function item(string $placeholder,string $action_id): self { return new self($placeholder,$action_id); } diff --git a/src/Blockkit/Blocks/Elements/MultiStaticSelect.php b/src/Blockkit/Blocks/Elements/MultiStaticSelect.php index ff00c5b..a59705a 100644 --- a/src/Blockkit/Blocks/Elements/MultiStaticSelect.php +++ b/src/Blockkit/Blocks/Elements/MultiStaticSelect.php @@ -9,37 +9,30 @@ use Slack\Exceptions\SlackSyntaxException; final class MultiStaticSelect extends Element { - protected const LIMITS = [ + public const LIMITS = [ 'action_id' => 255, 'placeholder' => 150, ]; - private const MAX_OPTIONS = 100; + public const MAX_OPTIONS = 100; // @todo option_group? (double check it is applicable to this item) /** - * @param Text $placeholder + * @param string $placeholder * @param string $action_id * @param Collection $options * @throws SlackSyntaxException * @todo We dont handle option_groups yet. */ - public function __construct(Text $placeholder,string $action_id,Collection $options) + public function __construct(string $placeholder,string $action_id,Collection $options) { parent::__construct(); // Defaults $this->type = 'multi_static_select'; - if ($placeholder->type != 'plain_text') - throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$placeholder->type)); - - if (strlen($placeholder->text) > self::LIMITS['placeholder']) - throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['placeholder'])); - - $this->placeholder = $placeholder; - + $this->placeholder = Text::item($this->validate('placeholder',$placeholder),'plain_text'); $this->action_id = $this->validate('action_id',$action_id); if (! $options->count()) @@ -53,7 +46,7 @@ final class MultiStaticSelect extends Element }); } - public static function item(Text $placeholder,string $action_id,Collection $options): self + public static function item(string $placeholder,string $action_id,Collection $options): self { return new self($placeholder,$action_id,$options); } diff --git a/src/Blockkit/Blocks/Elements/Options.php b/src/Blockkit/Blocks/Elements/Options.php index 4bbf988..02d3b47 100644 --- a/src/Blockkit/Blocks/Elements/Options.php +++ b/src/Blockkit/Blocks/Elements/Options.php @@ -36,15 +36,9 @@ final class Options extends Element /* OPTIONAL ITEMS */ - public function description(Text $text): self + public function description(string $text): self { - if ($text->type != 'plain_text') - throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$text->type)); - - if (strlen($text->text) > self::LIMITS['description']) - throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['description'])); - - $this->description = $text; + $this->description = Text::item($this->validate('description',$text),'plain_text'); return $this; } diff --git a/src/Blockkit/Blocks/Elements/PlainTextInput.php b/src/Blockkit/Blocks/Elements/PlainTextInput.php index 0ce3fba..361b39d 100644 --- a/src/Blockkit/Blocks/Elements/PlainTextInput.php +++ b/src/Blockkit/Blocks/Elements/PlainTextInput.php @@ -10,12 +10,12 @@ use Slack\Exceptions\SlackSyntaxException; */ final class PlainTextInput extends Element { - protected const LIMITS = [ + public const LIMITS = [ 'action_id' => 255, // @todo Should be unique for each message 'placeholder' => 150, ]; - private const MAX_MIN_LENGTH = 3000; + public const MAX_MIN_LENGTH = 3000; // @todo dispatch_action_config // @todo focus_on_load diff --git a/src/Blockkit/Blocks/Elements/StaticSelect.php b/src/Blockkit/Blocks/Elements/StaticSelect.php index 1bf35c6..e7391a5 100644 --- a/src/Blockkit/Blocks/Elements/StaticSelect.php +++ b/src/Blockkit/Blocks/Elements/StaticSelect.php @@ -9,37 +9,30 @@ use Slack\Exceptions\SlackSyntaxException; final class StaticSelect extends Element { - protected const LIMITS = [ + public const LIMITS = [ 'action_id' => 255, 'placeholder' => 150, ]; - private const MAX_OPTIONS = 100; + public const MAX_OPTIONS = 100; // @todo option_group /** - * @param Text $placeholder + * @param string $placeholder * @param string $action_id * @param Collection $options * @throws SlackSyntaxException * @todo We dont handle option_groups yet. */ - public function __construct(Text $placeholder,string $action_id,Collection $options) + public function __construct(string $placeholder,string $action_id,Collection $options) { parent::__construct(); // Defaults $this->type = 'static_select'; - if ($placeholder->type != 'plain_text') - throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$placeholder->type)); - - if (strlen($placeholder->text) > self::LIMITS['placeholder']) - throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['placeholder'])); - - $this->placeholder = $placeholder; - + $this->placeholder = Text::item($this->validate('placeholder',$placeholder),'plain_text'); $this->action_id = $this->validate('action_id',$action_id); if (! $options->count()) @@ -53,7 +46,7 @@ final class StaticSelect extends Element }); } - public static function item(Text $placeholder,string $action_id,Collection $options): self + public static function item(string $placeholder,string $action_id,Collection $options): self { return new self($placeholder,$action_id,$options); } diff --git a/src/Blockkit/Blocks/Elements/Text.php b/src/Blockkit/Blocks/Elements/Text.php index 88919b7..081ca79 100644 --- a/src/Blockkit/Blocks/Elements/Text.php +++ b/src/Blockkit/Blocks/Elements/Text.php @@ -7,7 +7,7 @@ use Slack\Exceptions\SlackSyntaxException; final class Text extends Element { - private const TYPES = ['mrkdwn','plain_text']; + public const TYPES = ['mrkdwn','plain_text']; public function __construct(string $text,string $type) { @@ -16,8 +16,8 @@ final class Text extends Element if (! in_array($type,self::TYPES)) throw new SlackSyntaxException(sprintf('Type [%s] not valid',$type)); - $this->type = $type; $this->text = $text; + $this->type = $type; } public static function item(string $text,string $type='mrkdwn'): self @@ -29,8 +29,8 @@ final class Text extends Element public function emoji(bool $bool): self { - if ($x=$this->type != 'plain_text') - throw new SlackSyntaxException(sprintf('Cannnot use emoji when type is [%s]',$x)); + if ($this->type != 'plain_text') + throw new SlackSyntaxException(sprintf('Cannnot use emoji when type is [%s]',$this->type)); $this->emoji = $bool; diff --git a/src/Blockkit/Blocks/Header.php b/src/Blockkit/Blocks/Header.php index e6f2321..c7bc228 100644 --- a/src/Blockkit/Blocks/Header.php +++ b/src/Blockkit/Blocks/Header.php @@ -14,26 +14,20 @@ final class Header extends Blocks ]; /** - * @param Text $text + * @param string $text * @throws SlackSyntaxException */ - public function __construct(Text $text) + public function __construct(string $text) { parent::__construct(); // Defaults $this->type = 'header'; - if ($text->type != 'plain_text') - throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$text->type)); - - if (strlen($text->text) > self::LIMITS['text']) - throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['text'])); - - $this->text = $text; + $this->text = Text::item($this->validate('text',$text),'plain_text'); } - public static function item(Text $text): self + public static function item(string $text): self { return new self($text); } diff --git a/src/Blockkit/Blocks/Input.php b/src/Blockkit/Blocks/Input.php index b636396..f7d782f 100644 --- a/src/Blockkit/Blocks/Input.php +++ b/src/Blockkit/Blocks/Input.php @@ -2,18 +2,18 @@ namespace Slack\Blockkit\Blocks; -use Slack\Blockkit\{Blocks,Element}; +use Slack\Blockkit\{Blocks,Blocks\Elements\Text,Element}; use Slack\Exceptions\SlackSyntaxException; final class Input extends Blocks { - protected const LIMITS = [ + public const LIMITS = [ 'block_id' => 255, // @todo Should be unique for each message 'hint' => 2000, 'label' => 2000, ]; - private const VALID_ELEMENTS = [ + public const VALID_ELEMENTS = [ Elements\PlainTextInput::class, Elements\MultiStaticSelect::class ]; @@ -21,26 +21,20 @@ final class Input extends Blocks // @todo dispatch_action /** - * @param Elements\Text|null $label + * @param string|null $label * @throws SlackSyntaxException */ - public function __construct(Elements\Text $label=NULL) + public function __construct(string $label=NULL) { parent::__construct(); - if ($label->type != 'plain_text') - throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$label->type)); - // Defaults $this->type = 'input'; - if (strlen($label->text) > self::LIMITS['label']) - throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['label'])); - - $this->label = $label; + $this->label = Text::item($this->validate('label',$label),'plain_text'); } - public static function item(Elements\Text $label=NULL): self + public static function item(string $label=NULL): self { return new self($label); } diff --git a/src/Blockkit/Blocks/Section.php b/src/Blockkit/Blocks/Section.php index 0385725..6577a34 100644 --- a/src/Blockkit/Blocks/Section.php +++ b/src/Blockkit/Blocks/Section.php @@ -10,13 +10,13 @@ use Slack\Exceptions\SlackSyntaxException; final class Section extends Blocks { - protected const LIMITS = [ + public const LIMITS = [ 'block_id' => 255, // @todo Should be unique for each message 'text' => 3000, ]; - private const MAX_FIELDS = 10; - private const MAX_FIELDS_TEXT = 2000; + public const MAX_FIELDS = 10; + public const MAX_FIELDS_TEXT = 2000; /** * @param Text|NULL $text not required if fields is provided diff --git a/src/Blockkit/Modal.php b/src/Blockkit/Modal.php index f38fd1a..50f0fc7 100644 --- a/src/Blockkit/Modal.php +++ b/src/Blockkit/Modal.php @@ -13,40 +13,33 @@ use Slack\Exceptions\SlackSyntaxException; */ final class Modal extends BlockKit { - protected const LIMITS = [ + public const LIMITS = [ 'callback_id' => 255, 'close' => 24, 'private_metadata' => 3000, 'submit' => 24, - 'text' => 24, + 'title' => 24, ]; - private const MAX_BLOCKS = 100; + public const MAX_BLOCKS = 100; private $action = NULL; - public function __construct(Text $title=NULL,string $type='modal') + public function __construct(string $title=NULL,string $type='modal') { parent::__construct(); if (! in_array($type,['modal','home'])) throw new SlackSyntaxException(sprintf('Unknown type %s',$type)); - if ($type != 'modal' && $title) - throw new SlackSyntaxException(sprintf('Titles are not required for %s',$type)); - if ($title) { - if ($title->type != 'plain_text') - throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$title->type)); + if ($type != 'modal') + throw new SlackSyntaxException(sprintf('Titles are not required for %s',$type)); - if (strlen($title->text) > self::LIMITS['text']) - throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['text'])); - - $this->title = $title; + $this->title = Text::item($this->validate('title',$title),'plain_text'); } $this->type = $type; - $this->blocks = collect(); } @@ -61,7 +54,7 @@ final class Modal extends BlockKit { $this->blocks->push($block); - if ($x=$this->blocks->count() > self::MAX_BLOCKS) + if ($this->blocks->count() > self::MAX_BLOCKS) throw new SlackSyntaxException(sprintf('Modal can only have %d blocks',self::MAX_BLOCKS)); return $this; @@ -113,18 +106,12 @@ final class Modal extends BlockKit return $this; } - public function close(Text $text): self + public function close(string $text): self { if ($this->type != 'modal') throw new SlackSyntaxException(sprintf('Close is not required for %s',$this->type)); - if ($text->type != 'plain_text') - throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$text->type)); - - if (strlen($text->text) > self::LIMITS['close']) - throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['close'])); - - $this->close = $text; + $this->close = Text::item($this->validate('close',$text),'plain_text'); return $this; } @@ -174,18 +161,12 @@ final class Modal extends BlockKit return $this; } - public function submit(Text $text): self + public function submit(string $text): self { if ($this->type != 'modal') throw new SlackSyntaxException(sprintf('Submit is not required for %s',$this->type)); - if ($text->type != 'plain_text') - throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$text->type)); - - if (strlen($text->text) > self::LIMITS['submit']) - throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['submit'])); - - $this->submit = $text; + $this->submit = Text::item($this->validate('submit',$text),'plain_text'); return $this; } diff --git a/src/Listeners/ShortcutListener.php b/src/Listeners/ShortcutListener.php index 0fc0926..4688bc2 100644 --- a/src/Listeners/ShortcutListener.php +++ b/src/Listeners/ShortcutListener.php @@ -9,7 +9,6 @@ use Slack\Blockkit\Blocks\Elements\Text; use Slack\Blockkit\Blocks\{Header,Section}; use Slack\Blockkit\Modal; use Slack\Interactive\Shortcut; -use Slack\Message; class ShortcutListener //implements ShouldQueue { @@ -28,9 +27,9 @@ class ShortcutListener //implements ShouldQueue public function handle(Shortcut $event): void { if (! $event->channel() || ! $event->channel()->active) { - $modal = new Modal(Text::item(config('app.name'),'plain_text')); + $modal = new Modal(config('app.name')); - $modal->addBlock(Header::item(Text::item(':robot_face: Bot not in this channel','plain_text'))) + $modal->addBlock(Header::item(':robot_face: Bot not in this channel')) ->addBlock(Section::item(Text::item('Please add the BOT to this channel and try this again.'))); try { diff --git a/src/Message.php b/src/Message.php index ad1a819..36cc662 100644 --- a/src/Message.php +++ b/src/Message.php @@ -26,8 +26,8 @@ final class Message extends BlockKit { private const LOGKEY = 'SM-'; - private const MAX_ATTACHMENTS = 20; - private const MAX_BLOCKS = 50; + public const MAX_ATTACHMENTS = 20; + public const MAX_BLOCKS = 50; private Model $o; private ?Carbon $selfdestruct = NULL; diff --git a/src/Message/Attachment.php b/src/Message/Attachment.php index 267cac7..3740ab7 100644 --- a/src/Message/Attachment.php +++ b/src/Message/Attachment.php @@ -9,7 +9,7 @@ use Slack\Blockkit\Blocks; final class Attachment extends BlockKit { - protected const LIMITS = [ + public const LIMITS = [ 'footer' => 300, ]; diff --git a/src/Message/AttachmentAction.php b/src/Message/AttachmentAction.php index be84387..34da5f1 100644 --- a/src/Message/AttachmentAction.php +++ b/src/Message/AttachmentAction.php @@ -16,14 +16,14 @@ use Slack\Exceptions\SlackSyntaxException; */ final class AttachmentAction extends BlockKit { - protected const LIMITS = [ + public const LIMITS = [ 'text' => 30, 'value' => 2000, ]; - private const DATA_SOURCES = ['static','users','channels','conversastions','external']; - private const STYLES = ['default','danger','primary']; - private const TYPES = ['button','select']; + public const DATA_SOURCES = ['static','users','channels','conversastions','external']; + public const STYLES = ['default','danger','primary']; + public const TYPES = ['button','select']; // @todo options // @todo option_groups