Passed
Push — master ( 1bb139...d77149 )
by Paul
03:10
created

Console::normalizeValue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 3
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\Blackbar;
4
5
use DateTime;
6
7
class Console
8
{
9
    const ERROR_CODES = [
10
        E_ERROR => 'Error', // 1
11
        E_WARNING => 'Warning', // 2
12
        E_NOTICE => 'Notice', // 8
13
        E_STRICT => 'Strict', // 2048
14
        E_DEPRECATED => 'Deprecated', // 8192
15
    ];
16
17
    const MAPPED_ERROR_CODES = [
18
        'debug' => 0,
19
        'info' => 0,
20
        'notice' => 0,
21
        'warning' => E_NOTICE, // 8
22
        'error' => E_WARNING, // 2
23
        'critical' => E_WARNING, // 2
24
        'alert' => E_WARNING, // 2
25
        'emergency' => E_WARNING, // 2
26
    ];
27
28
    public $entries = [];
29
30
    /**
31
     * @param int|string $errno
32
     * @return static
33
     */
34
    public function store(string $message, $errno = 0, string $location = '')
35
    {
36
        $errname = 'Debug';
37
        if (array_key_exists($errno, static::MAPPED_ERROR_CODES)) {
38
            $errname = ucfirst($errno);
39
            $errno = static::MAPPED_ERROR_CODES[$errno];
40
        } elseif (array_key_exists($errno, static::ERROR_CODES)) {
41
            $errname = static::ERROR_CODES[$errno];
42
        }
43
        $this->entries[] = [
44
            'errno' => $errno,
45
            'message' => $location.$this->normalizeValue($message),
46
            'name' => sprintf('<span class="glbb-info glbb-%s">%s</span>', strtolower($errname), $errname),
47
        ];
48
        return $this;
49
    }
50
51
    /**
52
     * @param mixed $value
53
     */
54
    protected function normalizeValue($value): string
55
    {
56
        if ($value instanceof DateTime) {
57
            $value = $value->format('Y-m-d H:i:s');
58
        } elseif (is_object($value) || is_array($value)) {
59
            $value = print_r(json_decode(json_encode($value)), true);
60
        }
61
        return esc_html((string) $value);
62
    }
63
}
64