1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Crummy\Phlack\Message; |
4
|
|
|
|
5
|
|
|
use Crummy\Phlack\Common\OptionsResolver; |
6
|
|
|
use Crummy\Phlack\Message\Collection\AttachmentCollection; |
7
|
|
|
use Symfony\Component\OptionsResolver\Options; |
8
|
|
|
|
9
|
|
|
class Message extends Partial implements MessageInterface |
10
|
|
|
{ |
11
|
|
|
protected $optional = ['text', 'channel', 'username', 'icon_emoji', 'attachments']; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param $text |
15
|
|
|
* @param null $channel |
16
|
|
|
* @param null $username |
17
|
|
|
* @param null $iconEmoji |
18
|
|
|
*/ |
19
|
15 |
|
public function __construct($text, $channel = null, $username = null, $iconEmoji = null) |
20
|
|
|
{ |
21
|
15 |
|
parent::__construct([ |
22
|
15 |
|
'text' => $text, |
23
|
15 |
|
'channel' => $channel, |
24
|
15 |
|
'username' => $username, |
25
|
15 |
|
'icon_emoji' => $iconEmoji, |
26
|
15 |
|
'attachments' => new AttachmentCollection(), |
27
|
15 |
|
]); |
28
|
15 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
15 |
|
protected function setDefaultOptions(OptionsResolver $resolver) |
34
|
|
|
{ |
35
|
15 |
|
parent::setDefaultOptions($resolver); |
36
|
|
|
|
37
|
15 |
|
$resolver->setTypesAllowed([ |
38
|
15 |
|
'attachments' => '\Crummy\Phlack\Message\Collection\AttachmentCollection', |
39
|
15 |
|
]); |
40
|
|
|
|
41
|
15 |
|
$resolver->setNormalizers([ |
42
|
15 |
|
'icon_emoji' => function (Options $options, $value) { |
43
|
15 |
|
return empty($value) ? $value : sprintf(':%s:', trim($value, ':')); |
44
|
15 |
|
}, |
45
|
15 |
|
]); |
46
|
15 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param AttachmentInterface $attachment |
50
|
|
|
* |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
2 |
|
public function addAttachment(AttachmentInterface $attachment) |
54
|
|
|
{ |
55
|
2 |
|
$this['attachments']->add($attachment); |
56
|
|
|
|
57
|
2 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return AttachmentCollection |
62
|
|
|
*/ |
63
|
1 |
|
public function getAttachments() |
64
|
|
|
{ |
65
|
1 |
|
return $this->data['attachments']; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|