<?php
namespace Maknz\Slack\BlockElement;
use InvalidArgumentException;
use Maknz\Slack\Object\Option;
class MultiExternalSelect extends MultiDynamicSelect
{
protected $type = 'multi_external_select';
protected $min_query_length;
protected $initial_options = [];
protected static $availableAttributes = [
'placeholder' => 'placeholder',
'action_id' => 'action_id',
'min_query_length' => 'min_query_length',
'initial_options' => 'initial_options',
'confirm' => 'confirm',
'max_selected_items' => 'max_selected_items',
];
public function getMinQueryLength()
{
return $this->min_query_length;
}
public function setMinQueryLength($minQueryLength)
{
if (is_int($minQueryLength)) {
$this->min_query_length = $minQueryLength;
return $this;
}
throw new InvalidArgumentException('The minimum query length must be an integer');
}
public function getInitialOptions()
{
return $this->initial_options;
}
public function setInitialOptions(array $initialOptions)
{
$this->clearInitialOptions();
foreach ($initialOptions as $initialOption) {
$this->addInitialOption($initialOption);
}
return $this;
}
public function clearInitialOptions()
{
$this->initial_options = [];
return $this;
}
public function addInitialOption($initialOption)
{
if (is_array($initialOption)) {
$initialOption = new Option($initialOption);
}
if ($initialOption instanceof Option) {
$this->initial_options[] = $initialOption;
return $this;
}
throw new InvalidArgumentException('The initial option must be an instance of '.Option::class.' or a keyed array');
}
public function toArray()
{
$data = [
'type' => $this->getType(),
'placeholder' => $this->getPlaceholder()->toArray(),
'action_id' => $this->getActionId(),
];
if ($this->getMinQueryLength()) {
$data['min_query_length'] = $this->getMinQueryLength();
}
$initialOptions = $this->getInitialOptions();
if (count($initialOptions)) {
$data['initial_options'] = array_map(function (Option $o) {
return $o->toArray();
}, $initialOptions);
}
if ($this->getConfirm()) {
$data['confirm'] = $this->getConfirm()->toArray();
}
if ($this->getMaxSelectedItems()) {
$data['max_selected_items'] = $this->getMaxSelectedItems();
}
return $data;
}
}