StringFormat::code()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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