Added Input/PlainTextInput, some minor fixes

This commit is contained in:
Deon George 2022-02-22 17:47:32 +11:00
parent 6b16d07d80
commit 181365f456
12 changed files with 199 additions and 53 deletions

View File

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

View File

@ -3,7 +3,6 @@
namespace Slack\Blockkit\Blocks\Elements;
use \Exception;
use Slack\BlockKit;
use Slack\Blockkit\Element;
final class Button extends Element

View File

@ -3,7 +3,6 @@
namespace Slack\Blockkit\Blocks\Elements;
use \Exception;
use Slack\BlockKit;
use Slack\Blockkit\Element;
final class Confirm extends Element

View File

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

View File

@ -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']));

View File

@ -0,0 +1,78 @@
<?php
namespace Slack\Blockkit\Blocks\Elements;
use Slack\Blockkit\Element;
/**
* This is an element of an input dialog
*/
final class PlaintTextInput extends Element
{
protected const LIMITS = [
'action_id' => 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;
}
}

View File

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

View File

@ -0,0 +1,92 @@
<?php
namespace Slack\Blockkit\Blocks;
use \Exception;
use Illuminate\Support\Collection;
use Slack\Blockkit\{Blocks,Element};
final class Input extends Blocks
{
protected const LIMITS = [
'block_id' => 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;
}
}

View File

@ -1,22 +0,0 @@
<?php
namespace Slack\Blockkit\Input;
final class Element
{
public string $type;
public string $action_id;
public bool $multiline;
public function __construct(string $type,string $action_id,bool $multiline=FALSE)
{
$this->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);
}
}

View File

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

View File

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

View File

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