diff --git a/src/Message/Attachment.php b/src/Message/Attachment.php index 1eb49bb..1f72dff 100644 --- a/src/Message/Attachment.php +++ b/src/Message/Attachment.php @@ -16,8 +16,13 @@ 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; @@ -25,6 +30,8 @@ public function __construct($title, $text, $fallback = null, $color = null, $pre $this->data['color'] = $color; $this->data['pretext'] = $pretext; $this->data['fields'] = $fields; + $this->data['actions'] = $actions; + $this->data['callback_id'] = $callback_id; } /** @@ -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]); + } } } } diff --git a/src/Message/AttachmentAction.php b/src/Message/AttachmentAction.php new file mode 100644 index 0000000..9d8aa51 --- /dev/null +++ b/src/Message/AttachmentAction.php @@ -0,0 +1,83 @@ +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'] : ''; + } +} diff --git a/tests/Message/AttachmentActionTest.php b/tests/Message/AttachmentActionTest.php new file mode 100644 index 0000000..be73d0d --- /dev/null +++ b/tests/Message/AttachmentActionTest.php @@ -0,0 +1,59 @@ +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()); + } +} diff --git a/tests/Message/MessageBuilderTest.php b/tests/Message/MessageBuilderTest.php index 35d15d4..6f83753 100644 --- a/tests/Message/MessageBuilderTest.php +++ b/tests/Message/MessageBuilderTest.php @@ -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()); diff --git a/tests/Message/MessageTest.php b/tests/Message/MessageTest.php index 33d88d1..41c8a93 100644 --- a/tests/Message/MessageTest.php +++ b/tests/Message/MessageTest.php @@ -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()); } @@ -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()); @@ -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());