Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Add fluent builder for SlackMessageAttachmentField #16535

Merged
merged 5 commits into from
Nov 28, 2016
Merged
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
5 changes: 5 additions & 0 deletions src/Illuminate/Notifications/Channels/SlackWebhookChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Notifications\Channels;

use GuzzleHttp\Client as HttpClient;
use Illuminate\Notifications\Messages\SlackAttachmentField;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Messages\SlackAttachment;
Expand Down Expand Up @@ -99,6 +100,10 @@ protected function attachments(SlackMessage $message)
protected function fields(SlackAttachment $attachment)
{
return collect($attachment->fields)->map(function ($value, $key) {
if ($value instanceof SlackAttachmentField) {
return $value->toArray();
}

return ['title' => $key, 'value' => $value, 'short' => true];
})->values()->all();
}
Expand Down
25 changes: 25 additions & 0 deletions src/Illuminate/Notifications/Messages/SlackAttachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,31 @@ public function color($color)
return $this;
}

/**
* Add a field to the attachment.
*
* @param \Closure|array $title
* @param string $content
* @return $this
*/
public function field($title, $content = '')
{
if (is_callable($title)) {
$callback = $title;

$attachmentField = new SlackAttachmentField();

$callback($attachmentField);
$this->fields[] = $attachmentField;

return $this;
}

$this->fields[$title] = $content;

return $this;
}

/**
* Set the fields of the attachment.
*
Expand Down
88 changes: 88 additions & 0 deletions src/Illuminate/Notifications/Messages/SlackAttachmentField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Illuminate\Notifications\Messages;

class SlackAttachmentField
{
/**
* The title field of the attachment field.
*
* @var string
*/
protected $title;

/**
* The content of the attachment field.
*
* @var string
*/
protected $content;

/**
* Whether the content is short enough to fit side by side with
* other contents.
*
* @var bool
*/
protected $short = true;

/**
* Set the title of the field.
*
* @param string $title
* @return $this
*/
public function title($title)
{
$this->title = $title;

return $this;
}

/**
* Set the content of the field.
*
* @param string $content
* @return $this
*/
public function content($content)
{
$this->content = $content;

return $this;
}

/**
* @return $this
*/
public function displaySideBySide()
{
$this->short = true;

return $this;
}

/**
* @return $this
*/
public function dontDisplaySideBySide()
{
$this->short = false;

return $this;
}

/**
* Get the array representation of the attachment field.
*
* @return array
*/
public function toArray()
{
return [
'title' => $this->title,
'value' => $this->content,
'short' => $this->short,
];
}
}
51 changes: 51 additions & 0 deletions tests/Notifications/NotificationSlackChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,37 @@ public function testCorrectPayloadWithoutOptionalFieldsIsSentToSlack()
]
);
}

public function testCorrectPayloadWithAttachmentFieldBuilderIsSentToSlack()
{
$this->validatePayload(
new NotificationSlackChannelWithAttachmentFieldBuilderTestNotification,
[
'json' => [
'text' => 'Content',
'attachments' => [
[
'title' => 'Laravel',
'text' => 'Attachment Content',
'title_link' => 'https://laravel.com',
'fields' => [
[
'title' => 'Project',
'value' => 'Laravel',
'short' => true,
],
[
'title' => 'Special powers',
'value' => 'Zonda',
'short' => false,
],
],
],
],
],
]
);
}
}

class NotificationSlackChannelTestNotifiable
Expand Down Expand Up @@ -136,3 +167,23 @@ public function toSlack($notifiable)
});
}
}

class NotificationSlackChannelWithAttachmentFieldBuilderTestNotification extends Notification
{
public function toSlack($notifiable)
{
return (new SlackMessage)
->content('Content')
->attachment(function ($attachment) {
$attachment->title('Laravel', 'https://laravel.com')
->content('Attachment Content')
->field('Project', 'Laravel')
->field(function ($attachmentField) {
$attachmentField
->title('Special powers')
->content('Zonda')
->dontDisplaySideBySide();
});
});
}
}