Skip to content
This repository has been archived by the owner on Jun 4, 2018. It is now read-only.

Added ability to add actions (buttons) within the attachment #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions src/Message/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@ class Attachment extends DataObject
* @param string $title The attachment title.
* @param string $text The attachment body text.
* @param string $fallback A plain-text summary of the attachment.
* @param string $color A color value
* @param string $pretext Pretext value
* @param array $fields Attachment fields
* @param array $actions Attachment actions
* @param string $callback_id A unique text callback_id.
*/
public function __construct($title, $text, $fallback = null, $color = null, $pretext = null, array $fields = [])
public function __construct($title, $text, $fallback = null, $color = null, $pretext = null, array $fields = [], array $actions = [], $callback_id = null)
{
$this->data['title'] = $title;
$this->data['text'] = $text;
$this->data['fallback'] = $fallback ?: $text;
$this->data['color'] = $color;
$this->data['pretext'] = $pretext;
$this->data['fields'] = $fields;
$this->data['actions'] = $actions;
$this->data['callback_id'] = $callback_id;
}

/**
Expand Down Expand Up @@ -158,17 +165,43 @@ public function getFields()
return isset($this->data['fields']) ? $this->data['fields'] : [];
}

/**
* Checks if the attachment has actions.
*
* @return bool
*/
public function hasActions()
{
return isset($this->data['actions']) && count($this->data['actions']) > 0;
}

/**
* Gets all the attachment's actions.
*
* @return AttachmentAction[]
*/
public function getActions()
{
return isset($this->data['actions']) ? $this->data['actions'] : [];
}

/**
* {@inheritDoc}
*/
public function jsonUnserialize(array $data)
{
if (!isset($this->data['fields'])) {
return;
// Check that we have an array - add fields to attachment
if (isset($this->data['fields'])) {
for ($i = 0; $i < count($this->data['fields']); $i++) {
$this->data['fields'][$i] = AttachmentField::fromData($this->data['fields'][$i]);
}
}

for ($i = 0; $i < count($this->data['fields']); $i++) {
$this->data['fields'][$i] = AttachmentField::fromData($this->data['fields'][$i]);
// Check that we have an array - add actions to attachment
if (isset($this->data['actions'])) {
for ($i = 0; $i < count($this->data['actions']); $i++) {
$this->data['actions'][$i] = AttachmentAction::fromData($this->data['actions'][$i]);
}
}
}
}
83 changes: 83 additions & 0 deletions src/Message/AttachmentAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
namespace Slack\Message;

use Slack\DataObject;

/**
* An action inside a message attachment.
*
* @see https://api.slack.com/docs/attachments
*/
class AttachmentAction extends DataObject
{
/**
* Creates a new attachment action.
*
* @param string $name A text heading for the field.
* @param string $text The text value of the field.
* @param string $type The type value of the field.
* @param string $value The value of the field.
* @param array $confirm Array for button confirmation
*/
public function __construct($name, $text, $type, $value, array $confirm = null)
{
$this->data['name'] = $name;
$this->data['text'] = $text;
$this->data['type'] = $type;
$this->data['value'] = $value;

if (isset($confirm)) {
$this->data['confirm'] = $confirm;
}
}

/**
* Gets the text heading for the field.
*
* @return string The text heading for the field.
*/
public function getName()
{
return $this->data['name'];
}

/**
* Gets the text value of the field.
*
* @return string The text value of the field.
*/
public function getText()
{
return $this->data['text'];
}

/**
* Gets the text type of the field.
*
* @return string The type value of the field.
*/
public function getType()
{
return $this->data['type'];
}

/**
* Gets the text value of the field.
*
* @return string The text value of the field.
*/
public function getValue()
{
return $this->data['value'];
}

/**
* Gets the confirmation details for the action.
*
* @return array Array containing the confirmation.
*/
public function getConfirm()
{
return isset($this->data['confirm']) ? $this->data['confirm'] : '';
}
}
59 changes: 59 additions & 0 deletions tests/Message/AttachmentActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
namespace Slack\Tests\Message;

use Slack\Message\AttachmentAction;
use Slack\Tests\TestCase;

class AttachmentActionTest extends TestCase
{
public function testConstructor()
{
$name = $this->faker->title;
$text = $this->faker->title;
$type = 'button';
$confirm = null;

$field = new AttachmentAction($name, $text, $type, $confirm);

$this->assertEquals($name, $field->getName());
$this->assertEquals($text, $field->getText());
$this->assertEquals($type, $field->getType());
$this->assertEquals($confirm, $field->getConfirm());
}

public function testGetName()
{
$field = AttachmentAction::fromData([
'name' => $this->faker->title,
]);

$this->assertEquals($field->data['name'], $field->getName());
}

public function testGetText()
{
$field = AttachmentAction::fromData([
'text' => $this->faker->title,
]);

$this->assertEquals($field->data['text'], $field->getText());
}

public function testGetType()
{
$field = AttachmentAction::fromData([
'type' => $this->faker->title,
]);

$this->assertEquals($field->data['type'], $field->getType());
}

public function testConfirm()
{
$field = AttachmentAction::fromData([
'confirm' => null,
]);

$this->assertEquals($field->data['confirm'], $field->getConfirm());
}
}
2 changes: 1 addition & 1 deletion tests/Message/MessageBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testSetUser()

public function testAddAttachment()
{
$attachment = new Attachment('title', 'text', 'fallback');
$attachment = new Attachment('title', 'text', 'fallback', 'test_app');
$message = $this->builder->addAttachment($attachment)->create();

$this->assertTrue($message->hasAttachments());
Expand Down
6 changes: 3 additions & 3 deletions tests/Message/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testGetText()

public function testHasAttachmentsIsFalseWhenEmpty()
{
$attachment = new Attachment($this->faker->title, $this->faker->sentence);
$attachment = new Attachment($this->faker->title, $this->faker->sentence, $this->faker->title, $this->faker->title);
$message = new Message($this->client, []);
$this->assertFalse($message->hasAttachments());
}
Expand All @@ -31,7 +31,7 @@ public function testHasAttachmentsIsTrueWhenAttachments()
{
$message = new Message($this->client, [
'attachments' => [
new Attachment($this->faker->title, $this->faker->sentence),
new Attachment($this->faker->title, $this->faker->sentence, $this->faker->title, $this->faker->title),
],
]);
$this->assertTrue($message->hasAttachments());
Expand All @@ -45,7 +45,7 @@ public function testGetAttachments()

$count = rand(1, 10);
foreach (range(1, $count) as $i) {
$message->data['attachments'][] = new Attachment($this->faker->title, $this->faker->sentence);
$message->data['attachments'][] = new Attachment($this->faker->title, $this->faker->sentence, $this->faker->title, $this->faker->title);
}

$this->assertCount($count, $message->getAttachments());
Expand Down