slack/src/Message/Attachments.php
2021-08-12 14:09:40 +10:00

228 lines
4.1 KiB
PHP

<?php
namespace Slack\Message;
use Slack\BlockKit;
use Slack\Blockkit\BlockAction;
use Slack\Blockkit\Blocks;
/**
* Class MessageAttachment - Slack Message Attachments
* Represents an Single Attachment that can be added to a Message
*
* @package Slack\Message
*/
class Attachments implements \JsonSerializable
{
private $_data;
private $actions;
//private $blocks;
private $blockactions;
// @todo To rework
public function __construct()
{
$this->actions = collect();
//$this->blocks = collect();
$this->blockactions = collect();
$this->_data = collect();
}
// @todo To rework
public function jsonSerialize()
{
/*
if ($this->actions->count() AND ! $this->_data->has('callback_id'))
abort(500,'Actions without a callback ID');
if ($this->blockactions->count()) {
$x = collect();
$x->put('type','actions');
$x->put('elements',$this->blockactions);
$this->blocks->push($x);
// Empty out our blockactions, incase we are converted to json a second time.
$this->blockactions = collect();
}
if ($this->actions->count())
$this->_data->put('actions',$this->actions);
if ($this->blocks->count())
$this->_data->put('blocks',$this->blocks);
*/
return $this->_data;
}
/**
* Add an attachment to a message
*
* @param AttachmentAction $action
* @return Attachment
* @todo To rework
*/
public function addAction(AttachmentAction $action): self
{
$this->actions->push($action);
return $this;
}
/**
* Add a block to message
*
* @param BlockKit $block
* @return Attachment
* @deprecated
*/
public function addBlock(BlockKit $block): self
{
$this->blocks->push($block);
return $this;
}
/**
* Add a BlockAction to a Block
*
* @param BlockAction $action
* @return $this
* @todo To rework
*/
public function addBlockAction(BlockAction $action): self
{
$this->blockactions->push($action);
return $this;
}
//* @todo To rework
public function addField(string $title,string $value,bool $short): self
{
if (! $this->_data->has('fields'))
$this->_data->put('fields',collect());
$this->_data->get('fields')->push([
'title'=>$title,
'value'=>$value,
'short'=>$short
]);
return $this;
}
/**
* Set where markdown should be parsed by slack
*
* @param array $array
* @return $this
* @todo To rework
*/
public function markdownIn(array $array): self
{
// @todo Add array check to make sure it has valid items
$this->_data->put('mrkdown_in',$array);
return $this;
}
/**
* Configure the attachment color (on the left of the attachment)
*
* @param string $string
* @return $this
* @todo To rework
*/
public function setCallbackID(string $string): self
{
$this->_data->put('callback_id',$string);
return $this;
}
/**
* Add a blocks to the message attachment
*
* @param Blocks $blocks
* @return self
*/
public function setBlocks(Blocks $blocks): self
{
$this->_data->put('blocks',$blocks);
return $this;
}
/**
* Configure the attachment color (on the left of the attachment)
*
* @param string $string
* @return $this
* @todo To rework
*/
public function setColor(string $string): self
{
$this->_data->put('color',$string);
return $this;
}
/**
* Set the text used in the attachments footer
*
* @param string $string
* @return $this
* @todo To rework
*/
public function setFooter(string $string): self
{
$this->_data->put('footer',$string);
return $this;
}
/**
* Add the pre-text, displayed after the title.
*
* @param string $string
* @return $this
* @todo To rework
*/
public function setPretext(string $string): self
{
$this->_data->put('pretext',$string);
return $this;
}
/**
* Set the text used in the attachment
*
* @param string $string
* @return $this
* @todo To rework
*/
public function setText(string $string): self
{
$this->_data->put('text',$string);
return $this;
}
/**
* Set the Title used in the attachment
*
* @param string $string
* @return $this
* @todo To rework
*/
public function setTitle(string $string): self
{
$this->_data->put('title',$string);
return $this;
}
}