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