Passed
Push — master ( 0771d8...cccc01 )
by Peter
02:29
created

StringHelper::plainToHtml()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 17
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Helper;
6
7
class StringHelper
8
{
9
    /**
10
     * @param string $plainText
11
     * @param string $prefix
12
     * @param string $postfix
13
     *
14
     * @return string
15
     */
16
    public static function plainToHtml(string $plainText, string $prefix = '', string $postfix = ''): string
17
    {
18
        if (empty($plainText)) {
19
            return '';
20
        }
21
22
        $paragraphs = explode("\n", $plainText);
23
24
        $lines = [];
25
        foreach ($paragraphs as $paragraph) {
26
            if (empty($paragraph)) {
27
                continue;
28
            }
29
            $lines[] = "$prefix$paragraph$postfix";
30
        }
31
32
        return implode("\n", $lines);
33
    }
34
}
35