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