Completed
Pull Request — master (#14)
by Simon
01:56
created

LogFormatter::entryToUl()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 19
nc 12
nop 2
1
<?php
2
3
namespace SilverLeague\LogViewer\Helper;
4
5
/**
6
 * Class LogFormatter formats the array to a readable unordered list
7
 *
8
 * @package SilverLeague\LogViewer\Helper
9
 *
10
 * @author Simon Erkelens <[email protected]>
11
 */
12
class LogFormatter
13
{
14
15
    /**
16
     * Recursive method, will call itself if a sub element is an array
17
     * @param array $data
18
     * @param bool $first start with an opening bracket? if true, skip (counterintuitive, but naming)
19
     * @return string formatted <ul><li> of the array;
20
     */
21
    public static function entryToUl($data, $first = true)
22
    {
23
        $out = $first ? '<span style="color: #007700">[</span>' : '';
24
        $out .= '<ul style="margin-bottom: 0">';
25
        foreach ($data as $key => $arrayItem) {
26
            if (!is_array($arrayItem)) {
27
                $out .= '<li class="list-unstyled">' .
28
                    '<span style="color: #0000BB">' . $key . '</span>' .
29
                    '<span style="color: #007700">: </span>' .
30
                    '<span style="color: #DD0000">' . $arrayItem . '</span>'.
31
                    '</li>';
32
            } else {
33
                $out .= '<li class="list-unstyled">' .
34
                    '<span style="color: #0000BB">' . $key . '</span>' .
35
                    '<span style="color: #007700">: </span>' .
36
                    self::entryToUl($arrayItem) .
37
                    '</li>';
38
            }
39
        }
40
        $out .= '</ul>';
41
        $out .= $first ? '<span style="color: #007700">]</span>' : '';
42
43
        return $out;
44
    }
45
46
}