diff --git a/src/API.php b/src/API.php index 41bd5b3..c0f5b08 100644 --- a/src/API.php +++ b/src/API.php @@ -316,7 +316,7 @@ final class API { 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'=>json_encode($view)]))); + return new Generic($this->execute('views.open',json_encode(['trigger_id'=>$trigger,'view'=>$view]))); } /** @@ -329,21 +329,21 @@ final class API * @throws \Exception * @todo Add some smarts to detect if the new view is the same as the current view, and thus no need to post. */ - public function viewPublish(string $user,string $view,string $hash=''): Generic + public function viewPublish(string $user,Modal $view,string $hash=''): Generic { Log::debug(sprintf('%s:Publish a view',static::LOGKEY),['m'=>__METHOD__,'u'=>$user,'h'=>$hash]); return new Generic($this->execute('views.publish',json_encode($hash ? ['user_id'=>$user,'view'=>$view,'hash'=>$hash] : ['user_id'=>$user,'view'=>$view]))); } - public function viewPush(string $trigger,string $view): Generic + public function viewPush(string $trigger,Modal $view): Generic { Log::debug(sprintf('%s:Push a view',static::LOGKEY),['m'=>__METHOD__,'t'=>$trigger]); return new Generic($this->execute('views.push',json_encode(['trigger_id'=>$trigger,'view'=>$view]))); } - public function viewUpdate(string $view_id,string $view): Generic + public function viewUpdate(string $view_id,Modal $view): Generic { Log::debug(sprintf('%s:Update a view',static::LOGKEY),['m'=>__METHOD__,'id'=>$view_id]); diff --git a/src/Blockkit/Blocks/Actions.php b/src/Blockkit/Blocks/Actions.php index 73182b9..1432f6b 100644 --- a/src/Blockkit/Blocks/Actions.php +++ b/src/Blockkit/Blocks/Actions.php @@ -5,7 +5,7 @@ namespace Slack\Blockkit\Blocks; use \Exception; use Illuminate\Support\Collection; use Slack\Blockkit\Blocks; -use Slack\Blockkit\Blocks\Elements\Text; +use Slack\Blockkit\Blocks\Elements\{Button,MultiStaticSelect,Text}; use Slack\Blockkit\Element; final class Actions extends Blocks @@ -16,6 +16,12 @@ final class Actions extends Blocks private const MAX_ELEMENTS = 5; + private const VALID_ELEMENTS = [ + Button::class, + MultiStaticSelect::class, + Blocks\Accessories\Overflow::class, + ]; + /** * @param Text $text * @throws Exception @@ -33,6 +39,14 @@ final class Actions extends Blocks return new self(); } + public function jsonSerialize() + { + if (! $this->elements) + throw new Exception('Must define at least 1 element'); + + return parent::jsonSerialize(); + } + /* OPTIONAL ITEMS */ public function block_id(string $string): self @@ -47,6 +61,8 @@ final class Actions extends Blocks if (count($collection) > self::MAX_ELEMENTS) throw new Exception(sprintf('Can only have maximum %d elements',self::MAX_ELEMENTS)); + // @todo Check that a valid element is added. https://api.slack.com/reference/block-kit/blocks#actions + $this->elements = $collection; return $this; diff --git a/src/Blockkit/Blocks/Elements/MultiStaticSelect.php b/src/Blockkit/Blocks/Elements/MultiStaticSelect.php index 5763d5c..42a7cd3 100644 --- a/src/Blockkit/Blocks/Elements/MultiStaticSelect.php +++ b/src/Blockkit/Blocks/Elements/MultiStaticSelect.php @@ -15,6 +15,8 @@ final class MultiStaticSelect extends Element private const MAX_OPTIONS = 100; + // @todo option_group? (double check it is applicable to this item) + /** * @param Text $placeholder * @param string $action_id @@ -43,7 +45,7 @@ final class MultiStaticSelect extends Element throw new Exception(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->id]; + return ['text'=>Text::item($item->name,'plain_text'),'value'=>(string)$item->value]; }); } @@ -68,18 +70,18 @@ final class MultiStaticSelect extends Element return $this; } - public function initial_options(Collection $collection): self + public function initial_options(array $initial=NULL): self { // No initial options. - if (! count($collection)) - return $this; + if (count($initial)) { + if (! $this->options) + throw new Exception('Cannot set an initial value without options defined first'); - if (count($collection) > self::MAX_OPTIONS) - throw new Exception(sprintf('Can only have maximum %d options',self::MAX_OPTIONS)); + if (count($initial) > self::MAX_OPTIONS) + throw new Exception(sprintf('Can only have maximum %d options',self::MAX_OPTIONS)); - $this->initial_options = $collection->transform(function($item) { - return ['text'=>Text::item($item->name,'plain_text'),'value'=>(string)$item->id]; - }); + $this->initial_options = $this->options->filter(function($item) use ($initial) { return in_array($item['value'],$initial); }); + } return $this; } diff --git a/src/Blockkit/Blocks/Elements/StaticSelect.php b/src/Blockkit/Blocks/Elements/StaticSelect.php new file mode 100644 index 0000000..dba3330 --- /dev/null +++ b/src/Blockkit/Blocks/Elements/StaticSelect.php @@ -0,0 +1,83 @@ + 255, + 'placeholder' => 150, + ]; + + private const MAX_OPTIONS = 100; + + // @todo option_group + + /** + * @param Text $placeholder + * @param string $action_id + * @param Collection $options + * @throws Exception + * @todo We dont handle option_groups yet. + */ + public function __construct(Text $placeholder,string $action_id,Collection $options) + { + parent::__construct(); + + // Defaults + $this->type = 'static_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); + + if (count($options) > self::MAX_OPTIONS) + throw new Exception(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_option(string $string=NULL): self + { + if (! $this->options) + throw new Exception('Cannot set an initial value without options defined first'); + + if ($string) + $this->initial_option = $this->options->first(function($item) use ($string) { return $item['value'] == $string; }); + + return $this; + } +} \ No newline at end of file diff --git a/src/Models/Team.php b/src/Models/Team.php index 18d4aca..6d9d9d3 100644 --- a/src/Models/Team.php +++ b/src/Models/Team.php @@ -56,9 +56,9 @@ class Team extends Model */ public function getAppTokenObfuscateAttribute(): string { - $attrs = explode('-',$this->getAppTokenAttribute()->token); + $attrs = explode('-',$this->token->token); $items = count($attrs)-1; - $attrs[$items] = '...'.substr($attrs[$items],-5); + $attrs[$items] = '...'.substr($attrs[$items],-3); return implode('-',$attrs); }