|
1
|
|
|
<?php |
|
2
|
|
|
namespace Slack\Message; |
|
3
|
|
|
|
|
4
|
|
|
use Slack\DataObject; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* A message attachment containing rich text data. |
|
8
|
|
|
* |
|
9
|
|
|
* @see https://api.slack.com/docs/attachments |
|
10
|
|
|
*/ |
|
11
|
|
|
class Attachment extends DataObject |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Creates a new message attachment. |
|
15
|
|
|
* |
|
16
|
|
|
* @param string $title The attachment title. |
|
17
|
|
|
* @param string $text The attachment body text. |
|
18
|
|
|
* @param string $fallback A plain-text summary of the attachment. |
|
19
|
|
|
*/ |
|
20
|
4 |
|
public function __construct($title, $text, $fallback = null, $color = null, $pretext = null, array $fields = []) |
|
21
|
|
|
{ |
|
22
|
4 |
|
$this->data['title'] = $title; |
|
23
|
4 |
|
$this->data['text'] = $text; |
|
24
|
4 |
|
$this->data['fallback'] = $fallback ?: $text; |
|
25
|
4 |
|
$this->data['color'] = $color; |
|
26
|
4 |
|
$this->data['pretext'] = $pretext; |
|
27
|
4 |
|
$this->data['fields'] = $fields; |
|
28
|
4 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Gets a plain-text summary of the attachment. |
|
32
|
|
|
* |
|
33
|
|
|
* @return string A plain-text summary of the attachment. |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public function getFallbackText() |
|
36
|
|
|
{ |
|
37
|
1 |
|
return $this->data['fallback']; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Gets the attachment border color. |
|
42
|
|
|
* |
|
43
|
|
|
* @return string The attachment border color. Can be "good", "warning", "danger", or a hex color code. |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function getColor() |
|
46
|
|
|
{ |
|
47
|
1 |
|
return isset($this->data['color']) ? $this->data['color'] : null; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Gets the attachment pretext. |
|
52
|
|
|
* |
|
53
|
|
|
* @return string Optional text that appears above the message attachment block. |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public function getPretext() |
|
56
|
|
|
{ |
|
57
|
1 |
|
return isset($this->data['pretext']) ? $this->data['pretext'] : ''; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Gets the author name. |
|
62
|
|
|
* |
|
63
|
|
|
* @return string The attachment author's name. |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function getAuthorName() |
|
66
|
|
|
{ |
|
67
|
1 |
|
return isset($this->data['author_name']) ? $this->data['author_name'] : null; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Gets the author link. |
|
72
|
|
|
* |
|
73
|
|
|
* @return string A link URL for the author. |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function getAuthorLink() |
|
76
|
|
|
{ |
|
77
|
1 |
|
return isset($this->data['author_link']) ? $this->data['author_link'] : null; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Gets the author icon. |
|
82
|
|
|
* |
|
83
|
|
|
* @return string An icon URL to show next to the author name. |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public function getAuthorIcon() |
|
86
|
|
|
{ |
|
87
|
1 |
|
return isset($this->data['author_icon']) ? $this->data['author_icon'] : null; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Gets the title. |
|
92
|
|
|
* |
|
93
|
|
|
* @return string The attachment title. |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public function getTitle() |
|
96
|
|
|
{ |
|
97
|
1 |
|
return $this->data['title']; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Gets the title link. |
|
102
|
|
|
* |
|
103
|
|
|
* @return string A link URL the title should link to. |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function getTitleLink() |
|
106
|
|
|
{ |
|
107
|
1 |
|
return isset($this->data['title_link']) ? $this->data['title_link'] : null; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Gets the attachment body text. |
|
112
|
|
|
* |
|
113
|
|
|
* @return string The attachment body text. |
|
114
|
|
|
*/ |
|
115
|
2 |
|
public function getText() |
|
116
|
|
|
{ |
|
117
|
2 |
|
return $this->data['text']; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Gets the image URL. |
|
122
|
|
|
* |
|
123
|
|
|
* @return string A URL to an image to display in the attachment body. |
|
124
|
|
|
*/ |
|
125
|
1 |
|
public function getImageUrl() |
|
126
|
|
|
{ |
|
127
|
1 |
|
return isset($this->data['image_url']) ? $this->data['image_url'] : null; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Gets the thumbnail URL. |
|
132
|
|
|
* |
|
133
|
|
|
* @return string A URL to an image to display as a thumbnail. |
|
134
|
|
|
*/ |
|
135
|
1 |
|
public function getThumbUrl() |
|
136
|
|
|
{ |
|
137
|
1 |
|
return isset($this->data['thumb_url']) ? $this->data['thumb_url'] : null; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Gets the footer text. |
|
142
|
|
|
* |
|
143
|
|
|
* @return string The footer text. |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getFooterText() |
|
146
|
|
|
{ |
|
147
|
|
|
return isset($this->data['footer']) ? $this->data['footer'] : null; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Gets a URL to an image to show to the left of the footer text. |
|
152
|
|
|
* |
|
153
|
|
|
* @return string The footer icon URL. |
|
154
|
|
|
*/ |
|
155
|
|
|
public function getFooterIcon() |
|
156
|
|
|
{ |
|
157
|
|
|
return isset($this->data['footer_icon']) ? $this->data['footer_icon'] : null; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Gets an extra timestamp value in the footer. |
|
162
|
|
|
* |
|
163
|
|
|
* @return \DateTime The time of the timestamp. |
|
164
|
|
|
*/ |
|
165
|
1 |
|
public function getTimestamp() |
|
166
|
|
|
{ |
|
167
|
1 |
|
if (!isset($this->data['ts'])) { |
|
168
|
|
|
return null; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
1 |
|
$time = new \DateTime(); |
|
172
|
1 |
|
$time->setTimestamp($this->data['ts']); |
|
173
|
1 |
|
return $time; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Checks if the attachment has fields. |
|
178
|
|
|
* |
|
179
|
|
|
* @return bool |
|
180
|
|
|
*/ |
|
181
|
1 |
|
public function hasFields() |
|
182
|
|
|
{ |
|
183
|
1 |
|
return isset($this->data['fields']) && count($this->data['fields']) > 0; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Gets all the attachment's fields. |
|
188
|
|
|
* |
|
189
|
|
|
* @return AttachmentField[] |
|
190
|
|
|
*/ |
|
191
|
1 |
|
public function getFields() |
|
192
|
|
|
{ |
|
193
|
1 |
|
return isset($this->data['fields']) ? $this->data['fields'] : []; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* {@inheritDoc} |
|
198
|
|
|
*/ |
|
199
|
15 |
View Code Duplication |
public function jsonUnserialize(array $data) |
|
200
|
|
|
{ |
|
201
|
15 |
|
if (!isset($this->data['fields'])) { |
|
202
|
14 |
|
return; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
1 |
|
for ($i = 0; $i < count($this->data['fields']); $i++) { |
|
|
|
|
|
|
206
|
1 |
|
$this->data['fields'][$i] = AttachmentField::fromData($this->data['fields'][$i]); |
|
207
|
1 |
|
} |
|
208
|
1 |
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
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: