2022-08-23 17:48:09 +10:00

75 lines
1.5 KiB
PHP

<?php
namespace Slack\Blockkit\Blocks\Elements;
use \Exception;
use Slack\Blockkit\Element;
final class Button extends Element
{
protected const LIMITS = [
'action_id' => 255,
'callback_id' => 255,
'text' => 75,
'url' => 3000,
'value' => 2000,
];
public function __construct(Text $text,string $value,string $action_id)
{
parent::__construct();
// Defaults
$this->type = 'button';
if ($text->type != 'plain_text')
throw new Exception(sprintf('Text must be plain_text not %s',$text->type));
if (strlen($text->text) > self::LIMITS['text'])
throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['text']));
$this->text = $text;
$this->value = $this->validate('value',$value);
$this->action_id = $this->validate('action_id',$action_id);
}
public static function item(Text $text,string $value,string $action_id): self
{
return new self($text,$value,$action_id);
}
/* OPTIONAL ITEMS */
public function callback_id(string $string): self
{
$this->callback_id = $string;
return $this;
}
public function confirm(Confirm $confirm): self
{
$this->confirm = $confirm;
return $this;
}
public function style(string $string): self
{
if (! in_array($string,['default','primary','danger']))
throw new Exception(sprintf('Unknown style %s',$string));
$this->style = $string;
return $this;
}
public function url(string $string): self
{
$this->url = $this->validate('url',$string);
return $this;
}
}