|
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
|
1 |
|
public function emphasis($text) |
|
38
|
|
|
{ |
|
39
|
1 |
|
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
|
1 |
|
public function indent($text) |
|
100
|
|
|
{ |
|
101
|
1 |
|
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
|
1 |
|
public function arrayToNumberedList(array $listItems) |
|
112
|
|
|
{ |
|
113
|
1 |
|
$formattedList = ''; |
|
114
|
1 |
|
$number = 1; |
|
115
|
|
|
|
|
116
|
1 |
|
foreach ($listItems as $listItem) { |
|
117
|
1 |
|
$formattedList .= sprintf('%d. %s\n', $number++, (string) $listItem); |
|
118
|
1 |
|
} |
|
119
|
|
|
|
|
120
|
1 |
|
return $formattedList; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* This will generate a bullet list from an array. |
|
125
|
|
|
* |
|
126
|
|
|
* @param array $listItems |
|
127
|
|
|
* @param string $bulletCharacter The character used as bullet |
|
128
|
|
|
* |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function arrayToBulletList(array $listItems, $bulletCharacter = '•') |
|
132
|
|
|
{ |
|
133
|
1 |
|
$formattedList = ''; |
|
134
|
|
|
|
|
135
|
1 |
|
foreach ($listItems as $listItem) { |
|
136
|
1 |
|
$formattedList .= sprintf('%s %s\n', $bulletCharacter, (string) $listItem); |
|
137
|
1 |
|
} |
|
138
|
|
|
|
|
139
|
1 |
|
return $formattedList; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Wraps a string with a wrapper string. |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $text |
|
146
|
|
|
* @param string $wrapper |
|
147
|
|
|
* |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
5 |
|
private function wrapStringWith($text, $wrapper) |
|
151
|
|
|
{ |
|
152
|
5 |
|
return sprintf('%1$s%2$s%1$s', $wrapper, $text); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|