102 lines
2.6 KiB
PHP
102 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace Slack\Blockkit\Blocks\Elements;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Slack\Blockkit\Element;
|
|
use Slack\Exceptions\SlackSyntaxException;
|
|
|
|
final class MultiStaticSelect extends Element
|
|
{
|
|
protected const LIMITS = [
|
|
'action_id' => 255,
|
|
'placeholder' => 150,
|
|
];
|
|
|
|
private const MAX_OPTIONS = 100;
|
|
|
|
// @todo option_group? (double check it is applicable to this item)
|
|
|
|
/**
|
|
* @param Text $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)
|
|
{
|
|
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->action_id = $this->validate('action_id',$action_id);
|
|
|
|
if (! $options->count())
|
|
throw new SlackSyntaxException('There are no options?');
|
|
|
|
if ($options->count() > self::MAX_OPTIONS)
|
|
throw new SlackSyntaxException(sprintf('Can only have maximum %d options',self::MAX_OPTIONS));
|
|
|
|
$this->options = $options->transform(function($item) {
|
|
return ['text'=>Text::item($item->name,'plain_text'),'value'=>(string)$item->value];
|
|
});
|
|
}
|
|
|
|
public static function item(Text $placeholder,string $action_id,Collection $options): self
|
|
{
|
|
return new self($placeholder,$action_id,$options);
|
|
}
|
|
|
|
/* OPTIONAL ITEMS */
|
|
|
|
public function confirm(Confirm $confirm): self
|
|
{
|
|
$this->confirm = $confirm;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function focus_on_load(bool $bool): self
|
|
{
|
|
$this->focus_on_load = $bool;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function initial_options(array $initial=NULL): self
|
|
{
|
|
// No initial options.
|
|
if (count($initial)) {
|
|
if (! $this->options)
|
|
throw new SlackSyntaxException('Cannot set an initial value without options defined first');
|
|
|
|
if (count($initial) > self::MAX_OPTIONS)
|
|
throw new SlackSyntaxException(sprintf('Can only have maximum %d options',self::MAX_OPTIONS));
|
|
|
|
$this->initial_options = $this->options->filter(function($item) use ($initial) { return in_array($item['value'],$initial); });
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function max_selected_items(int $int): self
|
|
{
|
|
if ($int < 1)
|
|
throw new SlackSyntaxException('Minimum 1 options must be configured');
|
|
|
|
$this->max_selected_items = $int;
|
|
|
|
return $this;
|
|
}
|
|
} |