1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the WoW-Apps/Symfony-Slack-Bot bundle for Symfony 3 |
4
|
|
|
* https://github.com/wow-apps/symfony-slack-bot |
5
|
|
|
* |
6
|
|
|
* (c) 2016 WoW-Apps |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WowApps\SlackBundle\Service; |
13
|
|
|
|
14
|
|
|
use WowApps\SlackBundle\DTO\SlackMessage; |
15
|
|
|
use WowApps\SlackBundle\Exception\SlackbotException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Alexey Samara <[email protected]> |
19
|
|
|
* @package WowApps\SlackBundle |
20
|
|
|
*/ |
21
|
|
|
class SlackMessageValidator |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param SlackMessage $slackMessage |
25
|
|
|
* @return void |
26
|
|
|
* @throws SlackbotException |
27
|
|
|
*/ |
28
|
1 |
|
public function validateMessage(SlackMessage $slackMessage) |
29
|
|
|
{ |
30
|
1 |
|
if (!$slackMessage->getText()) { |
31
|
1 |
|
throw new SlackbotException(SlackbotException::E_EMPTY_MESSAGE); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param SlackMessage $slackMessage |
37
|
|
|
* @return void |
38
|
|
|
* @throws SlackbotException |
39
|
|
|
*/ |
40
|
1 |
|
public function validateIconEmoji(SlackMessage $slackMessage) |
41
|
|
|
{ |
42
|
1 |
|
if (!empty($slackMessage->getIconEmoji()) && !preg_match('/^\:(.*?)\:$/i', $slackMessage->getIconEmoji())) { |
43
|
1 |
|
throw new SlackbotException(SlackbotException::E_INCORRECT_ICON_EMOJI); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param SlackMessage $slackMessage |
49
|
|
|
* @param array $config |
50
|
|
|
* @return SlackMessage |
51
|
|
|
*/ |
52
|
|
|
public function setDefaultsForEmptyFields(SlackMessage $slackMessage, array $config): SlackMessage |
53
|
|
|
{ |
54
|
|
|
if (!$slackMessage->getIcon()) { |
|
|
|
|
55
|
|
|
$slackMessage->setIcon($config['default_icon']); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (!$slackMessage->getRecipient()) { |
59
|
|
|
$slackMessage->setRecipient($config['default_channel']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (!$slackMessage->getSender()) { |
63
|
|
|
$slackMessage->setSender('SlackBot'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $slackMessage; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
This method has been deprecated.