1
|
|
|
<?php |
2
|
|
|
namespace Slack\Message; |
3
|
|
|
|
4
|
|
|
use Slack\ClientObject; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Represents a chat message and its data. |
8
|
|
|
*/ |
9
|
|
|
class Message extends ClientObject |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Gets the message text. |
13
|
|
|
* |
14
|
|
|
* @return string |
15
|
|
|
*/ |
16
|
2 |
|
public function getText() |
17
|
|
|
{ |
18
|
2 |
|
return $this->data['text']; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Checks if Markdown is enabled for the message text. |
23
|
|
|
* |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
|
|
public function isMarkdownEnabled() |
27
|
|
|
{ |
28
|
|
|
return isset($this->data['mrkdwn']) ? $this->data['mrkdwn'] == true : true; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Checks if the message has attachments. |
33
|
|
|
* |
34
|
|
|
* @return bool True if the message has attachments, otherwise false. |
35
|
|
|
*/ |
36
|
3 |
|
public function hasAttachments() |
37
|
|
|
{ |
38
|
3 |
|
return isset($this->data['attachments']) && count($this->data['attachments']) > 0; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Gets all message attachments. |
43
|
|
|
* |
44
|
|
|
* @return Attachment[] |
45
|
|
|
*/ |
46
|
2 |
|
public function getAttachments() |
47
|
|
|
{ |
48
|
2 |
|
return isset($this->data['attachments']) ? $this->data['attachments'] : []; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Gets the channel the message is in. |
53
|
|
|
* |
54
|
|
|
* @return \React\Promise\PromiseInterface |
55
|
|
|
*/ |
56
|
2 |
|
public function getChannel() |
57
|
|
|
{ |
58
|
2 |
|
return $this->client->getChannelById($this->data['channel']); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Gets the user that sent the message. |
63
|
|
|
* |
64
|
|
|
* @return \React\Promise\PromiseInterface |
65
|
|
|
*/ |
66
|
2 |
|
public function getUser() |
67
|
|
|
{ |
68
|
2 |
|
return $this->client->getUserById($this->data['user']); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Send message as user or not. |
73
|
|
|
* By default sends as user. |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function isAsUser() |
78
|
|
|
{ |
79
|
|
|
return isset($this->data['as_user']) ? $this->data['as_user'] : true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get message's user display name |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getUsername() |
88
|
|
|
{ |
89
|
|
|
return isset($this->data['username']) ? $this->data['username'] : ''; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function jsonUnserialize(array $data) |
96
|
|
|
{ |
97
|
|
|
if (!isset($this->data['attachments'])) { |
98
|
|
|
return; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
for ($i = 0; $i < count($this->data['attachments']); $i++) { |
|
|
|
|
102
|
|
|
$this->data['attachments'][$i] = Attachment::fromData($this->data['attachments'][$i]); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: