1 <?php
2 namespace Slack\Message;
3
4 use Slack\ApiClient;
5 use Slack\ChannelInterface;
6 use Slack\User;
7
8 /**
9 * A builder object for creating new message objects.
10 */
11 class MessageBuilder
12 {
13 /**
14 * @var ApiClient An API client.
15 */
16 private $client;
17
18 /**
19 * @var array An array of data to pass to the built message.
20 */
21 protected $data = [];
22
23 /**
24 * Creates a new message builder.
25 *
26 * @param ApiClient $client The API client the builder is working for.
27 */
28 public function __construct(ApiClient $client)
29 {
30 $this->client = $client;
31 }
32
33 /**
34 * Creates and returns a new message object specified by the builder.
35 *
36 * @return Message A new message object.
37 */
38 public function create()
39 {
40 return new Message($this->client, $this->data);
41 }
42
43 /**
44 * Sets the message text.
45 *
46 * @param string $text The message body text.
47 * @param bool $markdown Enable or disable Markdown parsing of the text.
48 * @return $this
49 */
50 public function setText($text, $markdown = true)
51 {
52 $this->data['text'] = $text;
53 $this->data['mrkdwn'] = $markdown;
54 return $this;
55 }
56
57 /**
58 * Sets the channel the message should be posted to.
59 *
60 * @param ChannelInterface $channel A channel to post to.
61 * @return $this
62 */
63 public function setChannel(ChannelInterface $channel)
64 {
65 $this->data['channel'] = $channel->getId();
66 return $this;
67 }
68
69 /**
70 * Sets the user the message is sent from.
71 *
72 * @param User $user A user.
73 * @return $this
74 */
75 public function setUser(User $user)
76 {
77 $this->data['user'] = $user->getId();
78 return $this;
79 }
80
81 /**
82 * Adds an attachment to the message.
83 *
84 * @param Attachment $attachment The attachment to add.
85 * @return $this
86 */
87 public function addAttachment(Attachment $attachment)
88 {
89 $this->data['attachments'][] = $attachment;
90 return $this;
91 }
92 }
93