1
|
|
|
<?php |
2
|
|
|
namespace Maknz\Slack; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
|
6
|
|
|
class Message |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* The text to send with the message. |
10
|
|
|
* |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $text; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The channel the message should be sent to. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $channel; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The username the message should be sent as. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $username; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The URL to the icon to use. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $icon; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The type of icon we are using. |
38
|
|
|
* |
39
|
|
|
* @var string (one of self::ICON_* constants) |
40
|
|
|
*/ |
41
|
|
|
protected $iconType; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Whether the message text should be interpreted in Slack's |
45
|
|
|
* Markdown-like language. |
46
|
|
|
* |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
protected $allow_markdown = true; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The attachment fields which should be formatted with |
53
|
|
|
* Slack's Markdown-like language. |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
protected $markdown_in_attachments = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* An array of attachments to send. |
61
|
|
|
* |
62
|
|
|
* @var Attachment[] |
63
|
|
|
*/ |
64
|
|
|
protected $attachments = []; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* An array of blocks to send. |
68
|
|
|
* |
69
|
|
|
* @var Block[] |
70
|
|
|
*/ |
71
|
|
|
protected $blocks = []; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var string |
75
|
|
|
*/ |
76
|
|
|
const ICON_TYPE_URL = 'icon_url'; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var string |
80
|
|
|
*/ |
81
|
|
|
const ICON_TYPE_EMOJI = 'icon_emoji'; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the message text. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
7 |
|
public function getText() |
89
|
|
|
{ |
90
|
7 |
|
return $this->text; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set the message text. |
95
|
|
|
* |
96
|
|
|
* @param string $text |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
7 |
|
public function setText($text) |
101
|
|
|
{ |
102
|
7 |
|
$this->text = $text; |
103
|
|
|
|
104
|
7 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the channel we will post to. |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
10 |
|
public function getChannel() |
113
|
|
|
{ |
114
|
10 |
|
return $this->channel; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Set the channel we will post to. |
119
|
|
|
* |
120
|
|
|
* @param string $channel |
121
|
|
|
* |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
10 |
|
public function setChannel($channel) |
125
|
|
|
{ |
126
|
10 |
|
$this->channel = $channel; |
127
|
|
|
|
128
|
10 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get the username we will post as. |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
9 |
|
public function getUsername() |
137
|
|
|
{ |
138
|
9 |
|
return $this->username; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set the username we will post as. |
143
|
|
|
* |
144
|
|
|
* @param string $username |
145
|
|
|
* |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
10 |
|
public function setUsername($username) |
149
|
|
|
{ |
150
|
10 |
|
$this->username = $username; |
151
|
|
|
|
152
|
10 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get the icon (either URL or emoji) we will post as. |
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
9 |
|
public function getIcon() |
161
|
|
|
{ |
162
|
9 |
|
return $this->icon; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Set the icon (either URL or emoji) we will post as. |
167
|
|
|
* |
168
|
|
|
* @param string $icon |
169
|
|
|
* |
170
|
|
|
* @return $this |
171
|
|
|
*/ |
172
|
10 |
|
public function setIcon($icon) |
173
|
|
|
{ |
174
|
10 |
|
if ($icon == null) { |
175
|
7 |
|
$this->icon = $this->iconType = null; |
176
|
|
|
|
177
|
7 |
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
3 |
|
if (mb_substr($icon, 0, 1) == ':' && mb_substr($icon, mb_strlen($icon) - 1, 1) == ':') { |
181
|
2 |
|
$this->iconType = self::ICON_TYPE_EMOJI; |
182
|
|
|
} else { |
183
|
1 |
|
$this->iconType = self::ICON_TYPE_URL; |
184
|
|
|
} |
185
|
|
|
|
186
|
3 |
|
$this->icon = $icon; |
187
|
|
|
|
188
|
3 |
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get the icon type being used, if an icon is set. |
193
|
|
|
* |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
2 |
|
public function getIconType() |
197
|
|
|
{ |
198
|
2 |
|
return $this->iconType; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Get whether message text should be formatted with |
203
|
|
|
* Slack's Markdown-like language. |
204
|
|
|
* |
205
|
|
|
* @return bool |
206
|
|
|
*/ |
207
|
6 |
|
public function getAllowMarkdown() |
208
|
|
|
{ |
209
|
6 |
|
return $this->allow_markdown; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Set whether message text should be formatted with |
214
|
|
|
* Slack's Markdown-like language. |
215
|
|
|
* |
216
|
|
|
* @param bool $value |
217
|
|
|
* |
218
|
|
|
* @return $this |
219
|
|
|
*/ |
220
|
8 |
|
public function setAllowMarkdown($value) |
221
|
|
|
{ |
222
|
8 |
|
$this->allow_markdown = (bool)$value; |
223
|
|
|
|
224
|
8 |
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Enable Markdown formatting for the message. |
229
|
|
|
* |
230
|
|
|
* @return $this |
231
|
|
|
*/ |
232
|
|
|
public function enableMarkdown() |
233
|
|
|
{ |
234
|
|
|
$this->setAllowMarkdown(true); |
235
|
|
|
|
236
|
|
|
return $this; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Disable Markdown formatting for the message. |
241
|
|
|
* |
242
|
|
|
* @return $this |
243
|
|
|
*/ |
244
|
|
|
public function disableMarkdown() |
245
|
|
|
{ |
246
|
|
|
$this->setAllowMarkdown(false); |
247
|
|
|
|
248
|
|
|
return $this; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Get the attachment fields which should be formatted |
253
|
|
|
* in Slack's Markdown-like language. |
254
|
|
|
* |
255
|
|
|
* @return array |
256
|
|
|
*/ |
257
|
2 |
|
public function getMarkdownInAttachments() |
258
|
|
|
{ |
259
|
2 |
|
return $this->markdown_in_attachments; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Set the attachment fields which should be formatted |
264
|
|
|
* in Slack's Markdown-like language. |
265
|
|
|
* |
266
|
|
|
* @param array $fields |
267
|
|
|
* |
268
|
|
|
* @return $this |
269
|
|
|
*/ |
270
|
8 |
|
public function setMarkdownInAttachments(array $fields) |
271
|
|
|
{ |
272
|
8 |
|
$this->markdown_in_attachments = $fields; |
273
|
|
|
|
274
|
8 |
|
return $this; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Change the name of the user the post will be made as. |
279
|
|
|
* |
280
|
|
|
* @param string $username |
281
|
|
|
* |
282
|
|
|
* @return $this |
283
|
|
|
*/ |
284
|
2 |
|
public function from($username) |
285
|
|
|
{ |
286
|
2 |
|
$this->setUsername($username); |
287
|
|
|
|
288
|
2 |
|
return $this; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Change the channel the post will be made to. |
293
|
|
|
* |
294
|
|
|
* @param string $channel |
295
|
|
|
* |
296
|
|
|
* @return $this |
297
|
|
|
*/ |
298
|
3 |
|
public function to($channel) |
299
|
|
|
{ |
300
|
3 |
|
$this->setChannel($channel); |
301
|
|
|
|
302
|
3 |
|
return $this; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Chainable method for setting the icon. |
307
|
|
|
* |
308
|
|
|
* @param string $icon |
309
|
|
|
* |
310
|
|
|
* @return $this |
311
|
|
|
*/ |
312
|
|
|
public function withIcon($icon) |
313
|
|
|
{ |
314
|
|
|
$this->setIcon($icon); |
315
|
|
|
|
316
|
|
|
return $this; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Add an attachment to the message. |
321
|
|
|
* |
322
|
|
|
* @param mixed $attachment |
323
|
|
|
* |
324
|
|
|
* @return $this |
325
|
|
|
* |
326
|
|
|
* @throws \InvalidArgumentException |
327
|
|
|
*/ |
328
|
7 |
|
public function attach($attachment) |
329
|
|
|
{ |
330
|
7 |
|
if ($attachment instanceof Attachment) { |
331
|
6 |
|
$this->attachments[] = $attachment; |
332
|
|
|
|
333
|
6 |
|
return $this; |
334
|
2 |
|
} elseif (is_array($attachment)) { |
335
|
2 |
|
$attachmentObject = new Attachment($attachment); |
336
|
|
|
|
337
|
2 |
|
if ( ! isset($attachment['mrkdwn_in'])) { |
338
|
2 |
|
$attachmentObject->setMarkdownFields($this->getMarkdownInAttachments()); |
339
|
|
|
} |
340
|
|
|
|
341
|
2 |
|
$this->attachments[] = $attachmentObject; |
342
|
|
|
|
343
|
2 |
|
return $this; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
throw new InvalidArgumentException('Attachment must be an instance of Maknz\\Slack\\Attachment or a keyed array'); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Get the attachments for the message. |
351
|
|
|
* |
352
|
|
|
* @return Attachment[] |
353
|
|
|
*/ |
354
|
9 |
|
public function getAttachments() |
355
|
|
|
{ |
356
|
9 |
|
return $this->attachments; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Set the attachments for the message. |
361
|
|
|
* |
362
|
|
|
* @param array $attachments |
363
|
|
|
* |
364
|
|
|
* @return $this |
365
|
|
|
* |
366
|
|
|
* @throws \InvalidArgumentException |
367
|
|
|
*/ |
368
|
1 |
|
public function setAttachments(array $attachments) |
369
|
|
|
{ |
370
|
1 |
|
$this->clearAttachments(); |
371
|
|
|
|
372
|
1 |
|
foreach ($attachments as $attachment) { |
373
|
1 |
|
$this->attach($attachment); |
374
|
|
|
} |
375
|
|
|
|
376
|
1 |
|
return $this; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Remove all attachments for the message. |
381
|
|
|
* |
382
|
|
|
* @return $this |
383
|
|
|
*/ |
384
|
1 |
|
public function clearAttachments() |
385
|
|
|
{ |
386
|
1 |
|
$this->attachments = []; |
387
|
|
|
|
388
|
1 |
|
return $this; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Add a block to the message. |
393
|
|
|
* |
394
|
|
|
* @param mixed $block |
395
|
|
|
* |
396
|
|
|
* @return $this |
397
|
|
|
* |
398
|
|
|
* @throws \InvalidArgumentException |
399
|
|
|
*/ |
400
|
2 |
|
public function withBlock($block) |
401
|
|
|
{ |
402
|
2 |
|
if ($block instanceof Block) { |
403
|
1 |
|
$this->blocks[] = $block; |
404
|
|
|
|
405
|
1 |
|
return $this; |
406
|
1 |
|
} elseif (is_array($block)) { |
407
|
1 |
|
$blockObject = Block::factory($block); |
408
|
1 |
|
$this->blocks[] = $blockObject; |
409
|
|
|
|
410
|
1 |
|
return $this; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
throw new InvalidArgumentException('Block must be an instance of Maknz\\Slack\\Block or a keyed array'); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Get the blocks for the message. |
418
|
|
|
* |
419
|
|
|
* @return Block[] |
420
|
|
|
*/ |
421
|
7 |
|
public function getBlocks() |
422
|
|
|
{ |
423
|
7 |
|
return $this->blocks; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Get the blocks for the message in array format. |
428
|
|
|
* |
429
|
|
|
* @return array |
430
|
|
|
*/ |
431
|
1 |
|
public function getBlocksAsArrays() |
432
|
|
|
{ |
433
|
1 |
|
$blocks = []; |
434
|
|
|
|
435
|
1 |
|
foreach ($this->getBlocks() as $block) { |
436
|
1 |
|
$blocks[] = $block->toArray(); |
437
|
|
|
} |
438
|
|
|
|
439
|
1 |
|
return $blocks; |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Set the blocks for the message. |
444
|
|
|
* |
445
|
|
|
* @param array $blocks |
446
|
|
|
* |
447
|
|
|
* @return $this |
448
|
|
|
* |
449
|
|
|
* @throws \InvalidArgumentException |
450
|
|
|
*/ |
451
|
|
|
public function setBlocks(array $blocks) |
452
|
|
|
{ |
453
|
|
|
$this->clearBlocks(); |
454
|
|
|
|
455
|
|
|
foreach ($blocks as $block) { |
456
|
|
|
$this->withBlock($block); |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
return $this; |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* Remove all blocks for the message. |
464
|
|
|
* |
465
|
|
|
* @return $this |
466
|
|
|
*/ |
467
|
|
|
public function clearBlocks() |
468
|
|
|
{ |
469
|
|
|
$this->blocks = []; |
470
|
|
|
|
471
|
|
|
return $this; |
472
|
|
|
} |
473
|
|
|
} |
474
|
|
|
|