1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\RocketChat; |
4
|
|
|
|
5
|
|
|
use http\Exception\InvalidArgumentException; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class RocketChatAttachment |
10
|
|
|
* @package NotificationChannels\RocketChat |
11
|
|
|
*/ |
12
|
|
|
class RocketChatAttachment |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string The color you want the order on the left side to be, any value background-css supports. |
17
|
|
|
*/ |
18
|
|
|
protected $color = ''; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string The text to display for this attachment, it is different than the message’s text. |
22
|
|
|
*/ |
23
|
|
|
protected $text = ''; |
24
|
|
|
/** |
25
|
|
|
* @var string Displays the time next to the text portion. |
26
|
|
|
*/ |
27
|
|
|
protected $timestamp = ''; |
28
|
|
|
/** |
29
|
|
|
* @var string An image that displays to the left of the text, looks better when this is relatively small. |
30
|
|
|
*/ |
31
|
|
|
protected $thumbnailUrl = ''; |
32
|
|
|
/** |
33
|
|
|
* @var string Only applicable if the ts is provided, as it makes the time clickable to this link. |
34
|
|
|
*/ |
35
|
|
|
protected $messageLink = ''; |
36
|
|
|
/** |
37
|
|
|
* @var bool Causes the image, audio, and video sections to be hiding when collapsed is true. |
38
|
|
|
*/ |
39
|
|
|
protected $collapsed = false; |
40
|
|
|
/** |
41
|
|
|
* @var string Name of the author. |
42
|
|
|
*/ |
43
|
|
|
protected $authorName = ''; |
44
|
|
|
/** |
45
|
|
|
* @var string Providing this makes the author name clickable and points to this link. |
46
|
|
|
*/ |
47
|
|
|
protected $authorLink = ''; |
48
|
|
|
/** |
49
|
|
|
* @var string Displays a tiny icon to the left of the Author’s name. |
50
|
|
|
*/ |
51
|
|
|
protected $authorIcon = ''; |
52
|
|
|
/** |
53
|
|
|
* @var string Title to display for this attachment, displays under the author. |
54
|
|
|
*/ |
55
|
|
|
protected $title = ''; |
56
|
|
|
/** |
57
|
|
|
* @var string Providing this makes the title clickable, pointing to this link. |
58
|
|
|
*/ |
59
|
|
|
protected $titleLink = ''; |
60
|
|
|
/** |
61
|
|
|
* @var bool When this is true, a download icon appears and clicking this saves the link to file. |
62
|
|
|
*/ |
63
|
|
|
protected $titleLinkDownload = false; |
64
|
|
|
/** |
65
|
|
|
* @var string The image to display, will be “big” and easy to see. |
66
|
|
|
*/ |
67
|
|
|
protected $imageUrl = ''; |
68
|
|
|
/** |
69
|
|
|
* @var string Audio file to play, only supports what html audio does. |
70
|
|
|
*/ |
71
|
|
|
protected $audioUrl = ''; |
72
|
|
|
/** |
73
|
|
|
* @var string Video file to play, only supports what html video does. |
74
|
|
|
*/ |
75
|
|
|
protected $videoUrl = ''; |
76
|
|
|
/** |
77
|
|
|
* @var array An array of Attachment Field Objects. |
78
|
|
|
*/ |
79
|
|
|
protected $fields = []; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* RocketChatAttachment constructor. |
83
|
|
|
* @param array|null $config |
84
|
|
|
*/ |
85
|
|
|
public function __construct(array $config = null) |
86
|
|
|
{ |
87
|
|
|
if ($config != null) { |
88
|
|
|
$this->setFromArray($config); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Create a new instance of RocketChatAttachment |
95
|
|
|
* |
96
|
|
|
* @param array|null $config |
97
|
|
|
* @return RocketChatAttachment |
98
|
|
|
*/ |
99
|
|
|
public static function create(array $config = null) |
100
|
|
|
{ |
101
|
|
|
return new self($config); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* set attachment data form array |
106
|
|
|
* |
107
|
|
|
* @param array $data |
108
|
|
|
*/ |
109
|
|
|
protected function setFromArray(array $data) |
110
|
|
|
{ |
111
|
|
|
foreach ($data as $key => $value) { |
112
|
|
|
$method = Str::camel($key); |
113
|
|
|
if(!method_exists($this, $method)) { |
114
|
|
|
continue; |
115
|
|
|
} |
116
|
|
|
$this->{$method}($value); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $color |
122
|
|
|
* @return RocketChatAttachment |
123
|
|
|
*/ |
124
|
|
|
public function color(string $color): self |
125
|
|
|
{ |
126
|
|
|
$this->color = $color; |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $text |
132
|
|
|
* @return RocketChatAttachment |
133
|
|
|
*/ |
134
|
|
|
public function text(string $text): self |
135
|
|
|
{ |
136
|
|
|
$this->text = $text; |
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string|\DateTime $timestamp |
142
|
|
|
* @return RocketChatAttachment |
143
|
|
|
*/ |
144
|
|
|
public function timestamp($timestamp): self |
145
|
|
|
{ |
146
|
|
|
if(!($timestamp instanceof \DateTime) && !is_string($timestamp)) { |
147
|
|
|
throw new \InvalidArgumentException('Timestamp must be string or DateTime, '.gettype($timestamp).' given.'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if($timestamp instanceof \DateTime) { |
151
|
|
|
$date = clone $timestamp; |
152
|
|
|
$timestamp = $date->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d\TH:i:s.v\Z'); |
153
|
|
|
} |
154
|
|
|
$this->timestamp = $timestamp; |
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string $thumbnailUrl |
160
|
|
|
* @return RocketChatAttachment |
161
|
|
|
*/ |
162
|
|
|
public function thumbnailUrl(string $thumbnailUrl): self |
163
|
|
|
{ |
164
|
|
|
$this->thumbnailUrl = $thumbnailUrl; |
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string $messageLink |
170
|
|
|
* @return RocketChatAttachment |
171
|
|
|
*/ |
172
|
|
|
public function messageLink(string $messageLink): self |
173
|
|
|
{ |
174
|
|
|
$this->messageLink = $messageLink; |
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param bool $collapsed |
180
|
|
|
* @return RocketChatAttachment |
181
|
|
|
*/ |
182
|
|
|
public function collapsed(bool $collapsed): self |
183
|
|
|
{ |
184
|
|
|
$this->collapsed = $collapsed; |
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string $name |
190
|
|
|
* @param string $link |
191
|
|
|
* @param string $icon |
192
|
|
|
* @return RocketChatAttachment |
193
|
|
|
*/ |
194
|
|
|
public function author(string $name, string $link = '', string $icon = ''): self |
195
|
|
|
{ |
196
|
|
|
$this->authorName($name); |
197
|
|
|
$this->authorLink($link); |
198
|
|
|
$this->authorIcon($icon); |
199
|
|
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $authorName |
204
|
|
|
* @return RocketChatAttachment |
205
|
|
|
*/ |
206
|
|
|
public function authorName(string $authorName): self |
207
|
|
|
{ |
208
|
|
|
$this->authorName = $authorName; |
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param string $authorLink |
214
|
|
|
* @return RocketChatAttachment |
215
|
|
|
*/ |
216
|
|
|
public function authorLink(string $authorLink): self |
217
|
|
|
{ |
218
|
|
|
$this->authorLink = $authorLink; |
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param string $authorIcon |
224
|
|
|
* @return RocketChatAttachment |
225
|
|
|
*/ |
226
|
|
|
public function authorIcon(string $authorIcon): self |
227
|
|
|
{ |
228
|
|
|
$this->authorIcon = $authorIcon; |
229
|
|
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param string $title |
234
|
|
|
* @return RocketChatAttachment |
235
|
|
|
*/ |
236
|
|
|
public function title(string $title): self |
237
|
|
|
{ |
238
|
|
|
$this->title = $title; |
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param string $titleLink |
244
|
|
|
* @return RocketChatAttachment |
245
|
|
|
*/ |
246
|
|
|
public function titleLink(string $titleLink): self |
247
|
|
|
{ |
248
|
|
|
$this->titleLink = $titleLink; |
249
|
|
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param bool $titleLinkDownload |
254
|
|
|
* @return RocketChatAttachment |
255
|
|
|
*/ |
256
|
|
|
public function titleLinkDownload(bool $titleLinkDownload): self |
257
|
|
|
{ |
258
|
|
|
$this->titleLinkDownload = $titleLinkDownload; |
259
|
|
|
return $this; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @param string $imageUrl |
264
|
|
|
* @return RocketChatAttachment |
265
|
|
|
*/ |
266
|
|
|
public function imageUrl(string $imageUrl): self |
267
|
|
|
{ |
268
|
|
|
$this->imageUrl = $imageUrl; |
269
|
|
|
return $this; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param string $audioUrl |
274
|
|
|
* @return RocketChatAttachment |
275
|
|
|
*/ |
276
|
|
|
public function audioUrl(string $audioUrl): self |
277
|
|
|
{ |
278
|
|
|
$this->audioUrl = $audioUrl; |
279
|
|
|
return $this; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @param string $videoUrl |
284
|
|
|
* @return RocketChatAttachment |
285
|
|
|
*/ |
286
|
|
|
public function videoUrl(string $videoUrl): self |
287
|
|
|
{ |
288
|
|
|
$this->videoUrl = $videoUrl; |
289
|
|
|
return $this; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @param array $fields |
294
|
|
|
* @return RocketChatAttachment |
295
|
|
|
*/ |
296
|
|
|
public function fields(array $fields): self |
297
|
|
|
{ |
298
|
|
|
$this->fields = $fields; |
299
|
|
|
return $this; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Get an array representation of the RocketChatAttachment. |
305
|
|
|
* |
306
|
|
|
* @return array |
307
|
|
|
*/ |
308
|
|
|
public function toArray() : array |
309
|
|
|
{ |
310
|
|
|
$message = array_filter([ |
311
|
|
|
'color' => $this->color, |
312
|
|
|
'text' => $this->text, |
313
|
|
|
'ts' => $this->timestamp, |
314
|
|
|
'thumb_url' => $this->thumbnailUrl, |
315
|
|
|
'message_link' => $this->messageLink, |
316
|
|
|
'collapsed' => $this->collapsed, |
317
|
|
|
'author_name' => $this->authorName, |
318
|
|
|
'author_link' => $this->authorLink, |
319
|
|
|
'author_icon' => $this->authorIcon, |
320
|
|
|
'title' => $this->title, |
321
|
|
|
'title_link' => $this->titleLink, |
322
|
|
|
'title_link_download' => $this->titleLinkDownload, |
323
|
|
|
'image_url' => $this->imageUrl, |
324
|
|
|
'audio_url' => $this->audioUrl, |
325
|
|
|
'video_url' => $this->videoUrl, |
326
|
|
|
'fields' => $this->fields, |
327
|
|
|
]); |
328
|
|
|
|
329
|
|
|
return $message; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
|
333
|
|
|
} |
334
|
|
|
|