|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the WoW-Apps/Symfony-Slack-Bot bundle for Symfony. |
|
5
|
|
|
* https://github.com/wow-apps/symfony-slack-bot |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* https://github.com/wow-apps/symfony-slack-bot/blob/master/LICENSE |
|
10
|
|
|
* |
|
11
|
|
|
* For technical documentation. |
|
12
|
|
|
* https://wow-apps.github.io/symfony-slack-bot/docs/ |
|
13
|
|
|
* |
|
14
|
|
|
* Author Alexey Samara <[email protected]> |
|
15
|
|
|
* |
|
16
|
|
|
* Copyright 2016 WoW-Apps. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace WowApps\SlackBundle\Service; |
|
20
|
|
|
|
|
21
|
|
|
use WowApps\SlackBundle\Exception\SlackbotException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Trait SlackMarkdownTrait. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Alexey Samara <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class SlackMarkdown |
|
29
|
|
|
{ |
|
30
|
|
|
const LIST_MARKER = 'marker'; |
|
31
|
|
|
const LIST_NUMERIC = 'numeric'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param int $linesNumber Minimum 1, maximum 5 |
|
35
|
|
|
* |
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function newLine(int $linesNumber = 1): string |
|
39
|
|
|
{ |
|
40
|
|
|
if ($linesNumber < 1 || $linesNumber > 5) { |
|
41
|
|
|
throw new SlackbotException( |
|
42
|
|
|
SlackbotException::E_WRONG_LINES_NUMBER, |
|
43
|
|
|
['requested_lines_number: ' . $linesNumber, 'allowed_lines_number: from 1 to 5'] |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$result = ''; |
|
48
|
|
|
|
|
49
|
|
|
for ($lineNumber = 1; $lineNumber <= $linesNumber; ++$lineNumber) { |
|
50
|
|
|
$result .= "\n"; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $result; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $string |
|
58
|
|
|
* |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function bold(string $string): string |
|
62
|
|
|
{ |
|
63
|
|
|
return sprintf('*%s*', $string); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $string |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function italic(string $string): string |
|
72
|
|
|
{ |
|
73
|
|
|
return sprintf('_%s_', $string); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $string |
|
78
|
|
|
* |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
public static function strike(string $string): string |
|
82
|
|
|
{ |
|
83
|
|
|
return sprintf('~%s~', $string); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $title |
|
88
|
|
|
* @param string $url |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
public static function link(string $title, string $url): string |
|
93
|
|
|
{ |
|
94
|
|
|
return sprintf('<%s|%s>', $url, $title); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param array $lines |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
public static function multilines(array $lines): string |
|
103
|
|
|
{ |
|
104
|
|
|
return implode("\n", $lines); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param array $list |
|
109
|
|
|
* @param string $listType |
|
110
|
|
|
* |
|
111
|
|
|
* @return string |
|
112
|
|
|
* |
|
113
|
|
|
* @throws SlackbotException |
|
114
|
|
|
*/ |
|
115
|
|
|
public static function list(array $list, string $listType = self::LIST_MARKER): string |
|
116
|
|
|
{ |
|
117
|
|
|
if (empty($list)) { |
|
118
|
|
|
throw new SlackbotException(SlackbotException::E_EMPTY_LIST); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if (!in_array($listType, [self::LIST_MARKER, self::LIST_NUMERIC])) { |
|
122
|
|
|
throw new SlackbotException( |
|
123
|
|
|
SlackbotException::E_INCORRECT_LIST_TYPE, |
|
124
|
|
|
[ |
|
125
|
|
|
'actual_type: ' . $listType, |
|
126
|
|
|
'expected_type: ' . self::LIST_MARKER . ' or ' . self::LIST_NUMERIC, |
|
127
|
|
|
] |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$elementNumber = 0; |
|
132
|
|
|
|
|
133
|
|
|
foreach ($list as $key => $value) { |
|
134
|
|
|
if (self::LIST_MARKER == $listType) { |
|
135
|
|
|
$list[$key] = sprintf('• %s', $value); |
|
136
|
|
|
continue; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$list[$key] = sprintf("%s. %s\n", ++$elementNumber, $value); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return sprintf("\n%s\n", implode("\n", $list)); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param string $string |
|
147
|
|
|
* |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
|
|
public static function inlineCode(string $string): string |
|
151
|
|
|
{ |
|
152
|
|
|
return sprintf('`%s`', $string); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param array $lines |
|
157
|
|
|
* |
|
158
|
|
|
* @return string |
|
159
|
|
|
*/ |
|
160
|
|
|
public static function code(array $lines): string |
|
161
|
|
|
{ |
|
162
|
|
|
$output = "\n```\n"; |
|
163
|
|
|
$output .= self::multilines($lines); |
|
164
|
|
|
$output .= "\n```\n"; |
|
165
|
|
|
|
|
166
|
|
|
return $output; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|