slack/src/BlockKit.php

105 lines
2.1 KiB
PHP
Raw Normal View History

<?php
namespace Slack;
use Illuminate\Support\Collection;
/**
* Class BlockKit - Slack Blockit Objects
*
* @package Slack
*/
class BlockKit implements \JsonSerializable
{
protected Collection $_data;
public function __construct()
{
$this->_data = collect();
}
public function jsonSerialize()
{
return $this->_data;
}
/**
* Render a BlockKit Button
*
* @param string $label
* @param string $value
* @param string|null $action_id
* @return \Illuminate\Support\Collection
* @throws \Exception
*/
public function button(string $label,string $value,string $action_id=NULL): Collection
{
$x = collect();
$x->put('type','button');
$x->put('text',$this->text($label,'plain_text'));
$x->put('value',$value);
if ($action_id)
$x->put('action_id',$action_id);
return $x;
}
/**
* Render the input dialog
*
* @param string $label
* @param string $action
* @param int $minlength
* @param string $placeholder
* @param bool $multiline
* @param string $hint
* @param string $initial
* @return $this
* @throws \Exception
*/
protected function input(string $label,string $action,int $minlength,string $placeholder='',bool $multiline=FALSE,string $hint='',string $initial='')
{
$this->_data->put('type','input');
$this->_data->put('element',[
'type'=>'plain_text_input',
'action_id'=>$action,
'placeholder'=>$this->text($placeholder ?: ' ','plain_text'),
'multiline'=>$multiline,
'min_length'=>$minlength,
'initial_value'=>$initial,
]);
$this->_data->put('label',[
'type'=>'plain_text',
'text'=>$label,
'emoji'=>true,
]);
$this->_data->put('optional',$minlength ? FALSE : TRUE);
if ($hint)
$this->_data->put('hint',$this->text($hint,'plain_text'));
return $this;
}
/**
* Returns a BlockKit Text item
*
* @param string $text
* @param string $type
* @return array
* @throws \Exception
*/
public function text(string $text,string $type='mrkdwn'): array
{
// Quick Validation
if (! in_array($type,['mrkdwn','plain_text']))
throw new \Exception('Invalid text type: '.$type);
return [
'type'=>$type,
'text'=>$text,
];
}
}