Completed
Push — master ( adc8b1...5898c8 )
by Bjørn
34:12
created

BufferLogger::getHtml()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 5
nop 0
dl 0
loc 19
ccs 0
cts 16
cp 0
crap 30
rs 9.4888
c 0
b 0
f 0
1
<?php
2
3
namespace WebPConvert\Loggers;
4
5
use WebPConvert\Loggers\BaseLogger;
6
7
class BufferLogger extends BaseLogger
8
{
9
    public $entries = array();
10
11 3
    public function log($msg, $style = '')
12
    {
13 3
        $this->entries[] = [$msg, $style];
14 3
    }
15
16 3
    public function ln()
17
    {
18 3
        $this->entries[] = '';
19 3
    }
20
21
    public function getHtml()
22
    {
23
        $html = '';
24
        foreach ($this->entries as $entry) {
25
            if ($entry == '') {
26
                $html .= '<br>';
27
            } else {
28
                list($msg, $style) = $entry;
29
                $msg = htmlspecialchars($msg);
30
                if ($style == 'bold') {
31
                    $html .= '<b>' . $msg . '</b>';
32
                } elseif ($style == 'italic') {
33
                    $html .= '<i>' . $msg . '</i>';
34
                } else {
35
                    $html .= $msg;
36
                }
37
            }
38
        }
39
        return $html;
40
    }
41
42
    public function getText($newLineChar = ' ')
43
    {
44
        $text = '';
45
        foreach ($this->entries as $entry) {
46
            if ($entry == '') {  // empty string means new line
47
                if (substr($text, -2) != '.' . $newLineChar) {
48
                    $text .= '.' . $newLineChar;
49
                }
50
            } else {
51
                list($msg, $style) = $entry;
52
                $text .= $msg;
53
            }
54
        }
55
56
        return $text;
57
    }
58
}
59