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
{
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

View File

@ -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,

View File

@ -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

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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

View File

@ -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);
}

View File

@ -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;

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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

View File

@ -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)
if ($title) {
if ($type != 'modal')
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 (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;
}

View File

@ -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 {

View File

@ -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;

View File

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

View File

@ -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