1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pageon\SlackWebhookMonolog\Slack; |
4
|
|
|
|
5
|
|
|
use Pageon\SlackWebhookMonolog\Slack\Interfaces\ConfigInterface; |
6
|
|
|
use Pageon\SlackWebhookMonolog\Slack\Interfaces\UserInterface; |
7
|
|
|
use Pageon\SlackWebhookMonolog\Slack\Interfaces\WebhookInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Jelmer Prins <[email protected]> |
11
|
|
|
* |
12
|
|
|
* @since 0.4.0 |
13
|
|
|
*/ |
14
|
|
|
final class StringFormat |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* There are three characters you must convert to HTML entities and only three: &, <, and >. |
18
|
|
|
* Don't HTML entity-encode the entire message, Slack will take care of the rest. |
19
|
|
|
* This function will do that for you. |
20
|
|
|
* |
21
|
|
|
* @param string $unescapedText |
22
|
|
|
* |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
1 |
|
public function escape($unescapedText) |
26
|
|
|
{ |
27
|
1 |
|
return str_replace(['&', '<', '>'], ['&', '<', '>'], $unescapedText); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* This will emphasis the string by wrapping it between * |
32
|
|
|
* |
33
|
|
|
* @param string $text |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
7 |
|
public function emphasis($text) |
38
|
|
|
{ |
39
|
7 |
|
return $this->wrapStringWith($text, '*'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* This will italicize the string by wrapping it between _ |
44
|
|
|
* |
45
|
|
|
* @param string $text |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
1 |
|
public function italicize($text) |
50
|
|
|
{ |
51
|
1 |
|
return $this->wrapStringWith($text, '_'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* This will strikethrough the string by wrapping it between ~ |
56
|
|
|
* |
57
|
|
|
* @param string $text |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
1 |
|
public function strikethrough($text) |
62
|
|
|
{ |
63
|
1 |
|
return $this->wrapStringWith($text, '~'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Create a block of pre-formatted, fixed-width text |
68
|
|
|
* |
69
|
|
|
* @param string $text |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
1 |
|
public function pre($text) |
74
|
|
|
{ |
75
|
1 |
|
return $this->wrapStringWith($text, '```'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Display as inline fixed-width text. |
80
|
|
|
* |
81
|
|
|
* @param string $text |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
1 |
|
public function code($text) |
86
|
|
|
{ |
87
|
1 |
|
return $this->wrapStringWith($text, '`'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Indent the text. |
92
|
|
|
* Because multi line indenting can't be closed in slack we implemented it for you. |
93
|
|
|
* When you use a new line it will be indented as well. |
94
|
|
|
* |
95
|
|
|
* @param string $text |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
7 |
|
public function indent($text) |
100
|
|
|
{ |
101
|
7 |
|
return '>' . str_replace("\n", "\n>", $text); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* This will generate a numbered list from an array. |
106
|
|
|
* |
107
|
|
|
* @param array $listItems |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
7 |
|
public function arrayToNumberedList(array $listItems) |
112
|
|
|
{ |
113
|
7 |
|
$formattedList = ''; |
114
|
7 |
|
$number = 1; |
115
|
|
|
|
116
|
7 |
|
foreach ($listItems as $listItem) { |
117
|
7 |
|
$formattedList .= sprintf("%d. %s\n", $number++, (string) $listItem); |
118
|
7 |
|
} |
119
|
|
|
|
120
|
7 |
|
return $formattedList; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* This will generate a numbered list from an array. |
125
|
|
|
* |
126
|
|
|
* @param array $listItems |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
6 |
|
public function arrayToKeyValueList(array $listItems) |
131
|
|
|
{ |
132
|
6 |
|
$formattedList = ''; |
133
|
|
|
|
134
|
6 |
|
foreach ($listItems as $key => $value) { |
135
|
6 |
|
$formattedList .= sprintf("%s: %s\n", $this->emphasis($key), (string) $value); |
136
|
6 |
|
} |
137
|
|
|
|
138
|
6 |
|
return $formattedList; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* This will generate a bullet list from an array. |
143
|
|
|
* |
144
|
|
|
* @param array $listItems |
145
|
|
|
* @param string $bulletCharacter The character used as bullet |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
1 |
|
public function arrayToBulletList(array $listItems, $bulletCharacter = '•') |
150
|
|
|
{ |
151
|
1 |
|
$formattedList = ''; |
152
|
|
|
|
153
|
1 |
|
foreach ($listItems as $listItem) { |
154
|
1 |
|
$formattedList .= sprintf("%s %s\n", $bulletCharacter, (string) $listItem); |
155
|
1 |
|
} |
156
|
|
|
|
157
|
1 |
|
return $formattedList; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Wraps a string with a wrapper string. |
162
|
|
|
* |
163
|
|
|
* @param string $text |
164
|
|
|
* @param string $wrapper |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
11 |
|
private function wrapStringWith($text, $wrapper) |
169
|
|
|
{ |
170
|
11 |
|
return sprintf("%1\$s%2\$s%1\$s", $wrapper, $text); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|