Change elements that require plain_text to only accept string , make public the consts that describe element limits

This commit is contained in:
Deon George 2022-09-07 19:27:30 +10:00
parent e9980ff9fd
commit b289f29948
20 changed files with 81 additions and 167 deletions

View File

@ -11,12 +11,12 @@ use Slack\Exceptions\SlackSyntaxException;
final class Overflow extends Element final class Overflow extends Element
{ {
protected const LIMITS = [ public const LIMITS = [
'action_id' => 255, 'action_id' => 255,
]; ];
private const MIN_OPTIONS = 1; public const MIN_OPTIONS = 1;
private const MAX_OPTIONS = 5; public const MAX_OPTIONS = 5;
/** /**
* @param string $action_id * @param string $action_id

View File

@ -10,13 +10,13 @@ use Slack\Exceptions\SlackSyntaxException;
final class Actions extends Blocks final class Actions extends Blocks
{ {
protected const LIMITS = [ public const LIMITS = [
'block_id' => 255, // @todo Should be unique for each message '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, Button::class,
MultiStaticSelect::class, MultiStaticSelect::class,
Blocks\Accessories\Overflow::class, Blocks\Accessories\Overflow::class,

View File

@ -9,11 +9,11 @@ use Slack\Exceptions\SlackSyntaxException;
final class Context extends Blocks final class Context extends Blocks
{ {
protected const LIMITS = [ public const LIMITS = [
'block_id' => 255, // @todo Should be unique for each message 'block_id' => 255, // @todo Should be unique for each message
]; ];
private const MAX_ELEMENTS = 10; public const MAX_ELEMENTS = 10;
/** /**
* @param Collection $collection * @param Collection $collection

View File

@ -15,26 +15,19 @@ final class Button extends Element
'value' => 2000, 'value' => 2000,
]; ];
public function __construct(Text $text,string $value,string $action_id) public function __construct(string $text,string $value,string $action_id)
{ {
parent::__construct(); parent::__construct();
// Defaults // Defaults
$this->type = 'button'; $this->type = 'button';
if ($text->type != 'plain_text') $this->text = Text::item($this->validate('text',$text),'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->value = $this->validate('value',$value); $this->value = $this->validate('value',$value);
$this->action_id = $this->validate('action_id',$action_id); $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); return new self($text,$value,$action_id);
} }

View File

@ -14,29 +14,17 @@ final class Confirm extends Element
'deny' => 30, '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(); parent::__construct();
if ($title->type != 'plain_text') $this->title = Text::item($this->validate('title',$title),'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->text = $this->validate('text',$text->text) ? $text : NULL; $this->text = $this->validate('text',$text->text) ? $text : NULL;
$this->confirm = Text::item($this->validate('confirm',$confirm),'plain_text');
$this->confirm = $this->validate('confirm',$confirm->text) ? $confirm : NULL; $this->deny = Text::item($this->validate('deny',$deny),'plain_text');
$this->deny = $this->validate('deny',$deny->text) ? $deny : NULL;
} }
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); return new self($title,$text,$confirm,$deny);
} }

View File

@ -13,29 +13,22 @@ final class ExternalSelect extends Element
]; ];
/** /**
* @param Text $placeholder * @param string $placeholder
* @param string $action_id * @param string $action_id
* @throws SlackSyntaxException * @throws SlackSyntaxException
*/ */
public function __construct(Text $placeholder,string $action_id) public function __construct(string $placeholder,string $action_id)
{ {
parent::__construct(); parent::__construct();
// Defaults // Defaults
$this->type = 'external_select'; $this->type = 'external_select';
if ($placeholder->type != 'plain_text') $this->placeholder = Text::item($this->validate('placeholder',$placeholder),'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->action_id = $this->validate('action_id',$action_id); $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); return new self($placeholder,$action_id);
} }

View File

@ -9,40 +9,32 @@ use Slack\Exceptions\SlackSyntaxException;
final class MultiExternalSelect extends Element final class MultiExternalSelect extends Element
{ {
protected const LIMITS = [ public const LIMITS = [
'action_id' => 255, 'action_id' => 255,
'placeholder' => 150, 'placeholder' => 150,
]; ];
private const MAX_OPTIONS = 100; public const MAX_OPTIONS = 100;
// @todo option_group? (double check it is applicable to this item) // @todo option_group? (double check it is applicable to this item)
/** /**
* @param Text $placeholder * @param string $placeholder
* @param string $action_id * @param string $action_id
* @throws SlackSyntaxException * @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(); parent::__construct();
// Defaults // Defaults
$this->type = 'multi_external_select'; $this->type = 'multi_external_select';
if ($placeholder->type != 'plain_text') $this->placeholder = Text::item($this->validate('placeholder',$placeholder),'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->action_id = $this->validate('action_id',$action_id); $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); return new self($placeholder,$action_id);
} }

View File

@ -9,37 +9,30 @@ use Slack\Exceptions\SlackSyntaxException;
final class MultiStaticSelect extends Element final class MultiStaticSelect extends Element
{ {
protected const LIMITS = [ public const LIMITS = [
'action_id' => 255, 'action_id' => 255,
'placeholder' => 150, 'placeholder' => 150,
]; ];
private const MAX_OPTIONS = 100; public const MAX_OPTIONS = 100;
// @todo option_group? (double check it is applicable to this item) // @todo option_group? (double check it is applicable to this item)
/** /**
* @param Text $placeholder * @param string $placeholder
* @param string $action_id * @param string $action_id
* @param Collection $options * @param Collection $options
* @throws SlackSyntaxException * @throws SlackSyntaxException
* @todo We dont handle option_groups yet. * @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(); parent::__construct();
// Defaults // Defaults
$this->type = 'multi_static_select'; $this->type = 'multi_static_select';
if ($placeholder->type != 'plain_text') $this->placeholder = Text::item($this->validate('placeholder',$placeholder),'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->action_id = $this->validate('action_id',$action_id); $this->action_id = $this->validate('action_id',$action_id);
if (! $options->count()) 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); return new self($placeholder,$action_id,$options);
} }

View File

@ -36,15 +36,9 @@ final class Options extends Element
/* OPTIONAL ITEMS */ /* OPTIONAL ITEMS */
public function description(Text $text): self public function description(string $text): self
{ {
if ($text->type != 'plain_text') $this->description = Text::item($this->validate('description',$text),'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;
return $this; return $this;
} }

View File

@ -10,12 +10,12 @@ use Slack\Exceptions\SlackSyntaxException;
*/ */
final class PlainTextInput extends Element final class PlainTextInput extends Element
{ {
protected const LIMITS = [ public const LIMITS = [
'action_id' => 255, // @todo Should be unique for each message 'action_id' => 255, // @todo Should be unique for each message
'placeholder' => 150, 'placeholder' => 150,
]; ];
private const MAX_MIN_LENGTH = 3000; public const MAX_MIN_LENGTH = 3000;
// @todo dispatch_action_config // @todo dispatch_action_config
// @todo focus_on_load // @todo focus_on_load

View File

@ -9,37 +9,30 @@ use Slack\Exceptions\SlackSyntaxException;
final class StaticSelect extends Element final class StaticSelect extends Element
{ {
protected const LIMITS = [ public const LIMITS = [
'action_id' => 255, 'action_id' => 255,
'placeholder' => 150, 'placeholder' => 150,
]; ];
private const MAX_OPTIONS = 100; public const MAX_OPTIONS = 100;
// @todo option_group // @todo option_group
/** /**
* @param Text $placeholder * @param string $placeholder
* @param string $action_id * @param string $action_id
* @param Collection $options * @param Collection $options
* @throws SlackSyntaxException * @throws SlackSyntaxException
* @todo We dont handle option_groups yet. * @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(); parent::__construct();
// Defaults // Defaults
$this->type = 'static_select'; $this->type = 'static_select';
if ($placeholder->type != 'plain_text') $this->placeholder = Text::item($this->validate('placeholder',$placeholder),'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->action_id = $this->validate('action_id',$action_id); $this->action_id = $this->validate('action_id',$action_id);
if (! $options->count()) 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); return new self($placeholder,$action_id,$options);
} }

View File

@ -7,7 +7,7 @@ use Slack\Exceptions\SlackSyntaxException;
final class Text extends Element final class Text extends Element
{ {
private const TYPES = ['mrkdwn','plain_text']; public const TYPES = ['mrkdwn','plain_text'];
public function __construct(string $text,string $type) public function __construct(string $text,string $type)
{ {
@ -16,8 +16,8 @@ final class Text extends Element
if (! in_array($type,self::TYPES)) if (! in_array($type,self::TYPES))
throw new SlackSyntaxException(sprintf('Type [%s] not valid',$type)); throw new SlackSyntaxException(sprintf('Type [%s] not valid',$type));
$this->type = $type;
$this->text = $text; $this->text = $text;
$this->type = $type;
} }
public static function item(string $text,string $type='mrkdwn'): self 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 public function emoji(bool $bool): self
{ {
if ($x=$this->type != 'plain_text') if ($this->type != 'plain_text')
throw new SlackSyntaxException(sprintf('Cannnot use emoji when type is [%s]',$x)); throw new SlackSyntaxException(sprintf('Cannnot use emoji when type is [%s]',$this->type));
$this->emoji = $bool; $this->emoji = $bool;

View File

@ -14,26 +14,20 @@ final class Header extends Blocks
]; ];
/** /**
* @param Text $text * @param string $text
* @throws SlackSyntaxException * @throws SlackSyntaxException
*/ */
public function __construct(Text $text) public function __construct(string $text)
{ {
parent::__construct(); parent::__construct();
// Defaults // Defaults
$this->type = 'header'; $this->type = 'header';
if ($text->type != 'plain_text') $this->text = Text::item($this->validate('text',$text),'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;
} }
public static function item(Text $text): self public static function item(string $text): self
{ {
return new self($text); return new self($text);
} }

View File

@ -2,18 +2,18 @@
namespace Slack\Blockkit\Blocks; namespace Slack\Blockkit\Blocks;
use Slack\Blockkit\{Blocks,Element}; use Slack\Blockkit\{Blocks,Blocks\Elements\Text,Element};
use Slack\Exceptions\SlackSyntaxException; use Slack\Exceptions\SlackSyntaxException;
final class Input extends Blocks final class Input extends Blocks
{ {
protected const LIMITS = [ public const LIMITS = [
'block_id' => 255, // @todo Should be unique for each message 'block_id' => 255, // @todo Should be unique for each message
'hint' => 2000, 'hint' => 2000,
'label' => 2000, 'label' => 2000,
]; ];
private const VALID_ELEMENTS = [ public const VALID_ELEMENTS = [
Elements\PlainTextInput::class, Elements\PlainTextInput::class,
Elements\MultiStaticSelect::class Elements\MultiStaticSelect::class
]; ];
@ -21,26 +21,20 @@ final class Input extends Blocks
// @todo dispatch_action // @todo dispatch_action
/** /**
* @param Elements\Text|null $label * @param string|null $label
* @throws SlackSyntaxException * @throws SlackSyntaxException
*/ */
public function __construct(Elements\Text $label=NULL) public function __construct(string $label=NULL)
{ {
parent::__construct(); parent::__construct();
if ($label->type != 'plain_text')
throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$label->type));
// Defaults // Defaults
$this->type = 'input'; $this->type = 'input';
if (strlen($label->text) > self::LIMITS['label']) $this->label = Text::item($this->validate('label',$label),'plain_text');
throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['label']));
$this->label = $label;
} }
public static function item(Elements\Text $label=NULL): self public static function item(string $label=NULL): self
{ {
return new self($label); return new self($label);
} }

View File

@ -10,13 +10,13 @@ use Slack\Exceptions\SlackSyntaxException;
final class Section extends Blocks final class Section extends Blocks
{ {
protected const LIMITS = [ public const LIMITS = [
'block_id' => 255, // @todo Should be unique for each message 'block_id' => 255, // @todo Should be unique for each message
'text' => 3000, 'text' => 3000,
]; ];
private const MAX_FIELDS = 10; public const MAX_FIELDS = 10;
private const MAX_FIELDS_TEXT = 2000; public const MAX_FIELDS_TEXT = 2000;
/** /**
* @param Text|NULL $text not required if fields is provided * @param Text|NULL $text not required if fields is provided

View File

@ -13,40 +13,33 @@ use Slack\Exceptions\SlackSyntaxException;
*/ */
final class Modal extends BlockKit final class Modal extends BlockKit
{ {
protected const LIMITS = [ public const LIMITS = [
'callback_id' => 255, 'callback_id' => 255,
'close' => 24, 'close' => 24,
'private_metadata' => 3000, 'private_metadata' => 3000,
'submit' => 24, 'submit' => 24,
'text' => 24, 'title' => 24,
]; ];
private const MAX_BLOCKS = 100; public const MAX_BLOCKS = 100;
private $action = NULL; private $action = NULL;
public function __construct(Text $title=NULL,string $type='modal') public function __construct(string $title=NULL,string $type='modal')
{ {
parent::__construct(); parent::__construct();
if (! in_array($type,['modal','home'])) if (! in_array($type,['modal','home']))
throw new SlackSyntaxException(sprintf('Unknown type %s',$type)); 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) {
if ($title->type != 'plain_text') if ($type != 'modal')
throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$title->type)); throw new SlackSyntaxException(sprintf('Titles are not required for %s',$type));
if (strlen($title->text) > self::LIMITS['text']) $this->title = Text::item($this->validate('title',$title),'plain_text');
throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['text']));
$this->title = $title;
} }
$this->type = $type; $this->type = $type;
$this->blocks = collect(); $this->blocks = collect();
} }
@ -61,7 +54,7 @@ final class Modal extends BlockKit
{ {
$this->blocks->push($block); $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)); throw new SlackSyntaxException(sprintf('Modal can only have %d blocks',self::MAX_BLOCKS));
return $this; return $this;
@ -113,18 +106,12 @@ final class Modal extends BlockKit
return $this; return $this;
} }
public function close(Text $text): self public function close(string $text): self
{ {
if ($this->type != 'modal') if ($this->type != 'modal')
throw new SlackSyntaxException(sprintf('Close is not required for %s',$this->type)); throw new SlackSyntaxException(sprintf('Close is not required for %s',$this->type));
if ($text->type != 'plain_text') $this->close = Text::item($this->validate('close',$text),'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;
return $this; return $this;
} }
@ -174,18 +161,12 @@ final class Modal extends BlockKit
return $this; return $this;
} }
public function submit(Text $text): self public function submit(string $text): self
{ {
if ($this->type != 'modal') if ($this->type != 'modal')
throw new SlackSyntaxException(sprintf('Submit is not required for %s',$this->type)); throw new SlackSyntaxException(sprintf('Submit is not required for %s',$this->type));
if ($text->type != 'plain_text') $this->submit = Text::item($this->validate('submit',$text),'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;
return $this; return $this;
} }

View File

@ -9,7 +9,6 @@ use Slack\Blockkit\Blocks\Elements\Text;
use Slack\Blockkit\Blocks\{Header,Section}; use Slack\Blockkit\Blocks\{Header,Section};
use Slack\Blockkit\Modal; use Slack\Blockkit\Modal;
use Slack\Interactive\Shortcut; use Slack\Interactive\Shortcut;
use Slack\Message;
class ShortcutListener //implements ShouldQueue class ShortcutListener //implements ShouldQueue
{ {
@ -28,9 +27,9 @@ class ShortcutListener //implements ShouldQueue
public function handle(Shortcut $event): void public function handle(Shortcut $event): void
{ {
if (! $event->channel() || ! $event->channel()->active) { 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.'))); ->addBlock(Section::item(Text::item('Please add the BOT to this channel and try this again.')));
try { try {

View File

@ -26,8 +26,8 @@ final class Message extends BlockKit
{ {
private const LOGKEY = 'SM-'; private const LOGKEY = 'SM-';
private const MAX_ATTACHMENTS = 20; public const MAX_ATTACHMENTS = 20;
private const MAX_BLOCKS = 50; public const MAX_BLOCKS = 50;
private Model $o; private Model $o;
private ?Carbon $selfdestruct = NULL; private ?Carbon $selfdestruct = NULL;

View File

@ -9,7 +9,7 @@ use Slack\Blockkit\Blocks;
final class Attachment extends BlockKit final class Attachment extends BlockKit
{ {
protected const LIMITS = [ public const LIMITS = [
'footer' => 300, 'footer' => 300,
]; ];

View File

@ -16,14 +16,14 @@ use Slack\Exceptions\SlackSyntaxException;
*/ */
final class AttachmentAction extends BlockKit final class AttachmentAction extends BlockKit
{ {
protected const LIMITS = [ public const LIMITS = [
'text' => 30, 'text' => 30,
'value' => 2000, 'value' => 2000,
]; ];
private const DATA_SOURCES = ['static','users','channels','conversastions','external']; public const DATA_SOURCES = ['static','users','channels','conversastions','external'];
private const STYLES = ['default','danger','primary']; public const STYLES = ['default','danger','primary'];
private const TYPES = ['button','select']; public const TYPES = ['button','select'];
// @todo options // @todo options
// @todo option_groups // @todo option_groups