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

BufferLogger::log()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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