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
|
5 |
|
public function __construct(ApiClient $client) |
29
|
|
|
{ |
30
|
5 |
|
$this->client = $client; |
31
|
5 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Creates and returns a new message object specified by the builder. |
35
|
|
|
* |
36
|
|
|
* @return Message A new message object. |
37
|
|
|
*/ |
38
|
5 |
|
public function create() |
39
|
|
|
{ |
40
|
5 |
|
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
|
1 |
|
public function setText($text, $markdown = true) |
51
|
|
|
{ |
52
|
1 |
|
$this->data['text'] = $text; |
53
|
1 |
|
$this->data['mrkdwn'] = $markdown; |
54
|
1 |
|
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
|
1 |
|
public function setChannel(ChannelInterface $channel) |
64
|
|
|
{ |
65
|
1 |
|
$this->data['channel'] = $channel->getId(); |
66
|
1 |
|
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
|
1 |
|
public function setUser(User $user) |
76
|
|
|
{ |
77
|
1 |
|
$this->data['user'] = $user->getId(); |
78
|
1 |
|
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
|
1 |
|
public function addAttachment(Attachment $attachment) |
88
|
|
|
{ |
89
|
1 |
|
$this->data['attachments'][] = $attachment; |
90
|
1 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|