Passed
Push — master ( 589611...468061 )
by jelmer
02:22
created

StringFormat::arrayToBulletList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2
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(['&', '<', '>'], ['&amp;', '&lt;', '&gt;'], $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