Add ExternalSelect, fix spelling for PlainTextInput, other minor syntax errors

This commit is contained in:
Deon George 2022-08-24 23:44:36 +10:00
parent 32a5d7f05a
commit 64228796ae
7 changed files with 122 additions and 5 deletions

View File

@ -0,0 +1,77 @@
<?php
namespace Slack\Blockkit\Blocks\Elements;
use \Exception;
use Slack\Blockkit\Element;
final class ExternalSelect extends Element
{
protected const LIMITS = [
'action_id' => 255,
'placeholder' => 150,
];
/**
* @param Text $placeholder
* @param string $action_id
* @throws Exception
*/
public function __construct(Text $placeholder,string $action_id)
{
parent::__construct();
// Defaults
$this->type = 'external_select';
if ($placeholder->type != 'plain_text')
throw new Exception(sprintf('Text must be plain_text not %s',$placeholder->type));
if (strlen($placeholder->text) > self::LIMITS['placeholder'])
throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['placeholder']));
$this->placeholder = $placeholder;
$this->action_id = $this->validate('action_id',$action_id);
}
public static function item(Text $placeholder,string $action_id): self
{
return new self($placeholder,$action_id);
}
/* OPTIONAL ITEMS */
public function confirm(Confirm $confirm): self
{
$this->confirm = $confirm;
return $this;
}
// @note only 1 element in a view can have this set to true
public function focus_on_load(bool $bool): self
{
$this->focus_on_load = $bool;
return $this;
}
public function initial_option(string $option): self
{
$this->initial_option = $option;
return $this;
}
public function min_query_length(int $int): self
{
if ($int < 1)
throw new Exception('Minimum 1 options must be configured');
$this->min_query_length = $int;
return $this;
}
}

View File

@ -76,7 +76,7 @@ final class MultiExternalSelect extends Element
if ($int < 1) if ($int < 1)
throw new Exception('Minimum 1 options must be configured'); throw new Exception('Minimum 1 options must be configured');
$this->max_selected_items = $int; $this->min_query_length = $int;
return $this; return $this;
} }

View File

@ -7,7 +7,7 @@ use Slack\Blockkit\Element;
/** /**
* This is an element of an input dialog * This is an element of an input dialog
*/ */
final class PlaintTextInput extends Element final class PlainTextInput extends Element
{ {
protected const LIMITS = [ protected const LIMITS = [
'action_id' => 255, // @todo Should be unique for each message 'action_id' => 255, // @todo Should be unique for each message

View File

@ -15,7 +15,7 @@ final class Input extends Blocks
]; ];
private const VALID_ELEMENTS = [ private const VALID_ELEMENTS = [
Elements\PlaintTextInput::class, Elements\PlainTextInput::class,
Elements\MultiStaticSelect::class Elements\MultiStaticSelect::class
]; ];

View File

@ -148,7 +148,7 @@ final class BlockActions extends Base
* Separate out an action command to the id that the command relates to * Separate out an action command to the id that the command relates to
* *
* @param string $key * @param string $key
* @return string * @return string|null
* @throws SlackException * @throws SlackException
*/ */
private function action(string $key): ?string private function action(string $key): ?string

View File

@ -7,6 +7,7 @@ use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Slack\Blockkit\Modal; use Slack\Blockkit\Modal;
use Slack\Exceptions\SlackException;
use Slack\Models\Team; use Slack\Models\Team;
/** /**
@ -27,8 +28,14 @@ class ViewSubmission extends Base
case 'blocks': case 'blocks':
return collect(object_get($this->_data,'view.'.$key)); return collect(object_get($this->_data,'view.'.$key));
case 'callback':
return object_get($this->_data,'view.callback_id');
case 'callback_id': case 'callback_id':
return object_get($this->_data,'view.'.$key); return $this->callback('id');
case 'callback_value':
return $this->callback('value');
case 'meta': case 'meta':
return object_get($this->_data,'view.private_metadata'); return object_get($this->_data,'view.private_metadata');
@ -44,6 +51,35 @@ class ViewSubmission extends Base
} }
} }
/**
* Separate out an callback command to the id that the command relates to
*
* @param string $key
* @return string|null
* @throws SlackException
*/
private function callback(string $key): ?string
{
$regex = '/^([a-z_]+)\|([0-9]+)$/';
$id = NULL;
$value = NULL;
if (preg_match($regex,$this->callback)) {
$id = preg_replace($regex,'$1',$this->callback);
$value = preg_replace($regex,'$2',$this->callback);
}
switch ($key) {
case 'id':
return $id ?: $this->callback;
case 'value':
return $value;
default:
throw new SlackException('Unknown key: '.$key);
}
}
/** /**
* This method should be overridden by a local implementation * This method should be overridden by a local implementation
* *

View File

@ -43,6 +43,10 @@ abstract class Base extends SlackBase
return object_get($this->_data,'team.id'); return object_get($this->_data,'team.id');
case 'channel_id': case 'channel_id':
return object_get($this->_data,'channel.id'); return object_get($this->_data,'channel.id');
case 'enterprise_id':
return object_get($this->_data,'team.'.$key);
case 'user_id': case 'user_id':
return object_get($this->_data,'user.id'); return object_get($this->_data,'user.id');