Passed
Push — master ( f247da...c8939d )
by Kacper
02:56
created

ConsoleHelper   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 23
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 99
ccs 54
cts 54
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A styled() 0 4 1
A open() 0 11 2
A close() 0 6 2
B _color() 0 24 2
C _style() 0 21 13
A _set() 0 8 2
A reset() 0 4 1
1
<?php
2
/**
3
 * Highlighter
4
 *
5
 * Copyright (C) 2015, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Highlighter\Utils;
17
18
class ConsoleHelper
19
{
20
    private $_stack   = [];
21
    private $_current = [];
22
23
    private $_default = [
24
        'color'      => 'default',
25
        'background' => 'default',
26
        'bold'       => false,
27
        'underlined' => false,
28
        'dim'        => false,
29
        'blink'      => false,
30
    ];
31
32 1
    public function styled($style, $text)
33
    {
34 1
        return $this->open($style).$text.$this->close();
35
    }
36
37 12
    public function open($style)
38
    {
39 12
        if (!empty($this->_current)) {
40 2
            $this->_stack[] = $this->_current;
41 2
            $style          = array_diff($style, $this->_current);
42 2
        }
43
44 12
        $this->_current = array_merge($this->_current, $style);
45
46 12
        return $this->_set(array_diff($this->_current, $this->_default));
47
    }
48
49 3
    public function close()
50
    {
51 3
        $this->_current = empty($this->_stack) ? $this->_default : array_pop($this->_stack);
52
53 3
        return "\033[0m".$this->_set(array_diff($this->_current, $this->_default));
54
    }
55
56 6
    private function _color($name, $bg = false)
57
    {
58
        $colors = [
59 6
            'default'       => 39,
60 6
            'black'         => 30,
61 6
            'red'           => 31,
62 6
            'green'         => 32,
63 6
            'yellow'        => 33,
64 6
            'blue'          => 34,
65 6
            'magenta'       => 35,
66 6
            'cyan'          => 36,
67 6
            'light gray'    => 37,
68 6
            'dark gray'     => 90,
69 6
            'light red'     => 91,
70 6
            'light green'   => 92,
71 6
            'light yellow'  => 93,
72 6
            'light blue'    => 94,
73 6
            'light magenta' => 95,
74 6
            'light cyan'    => 96,
75 6
            'white'         => 97,
76 6
        ];
77
78 6
        return $colors[strtolower($name)] + ($bg ? 10 : 0);
79
    }
80
81 12
    private function _style($name, $value)
82
    {
83
        switch ($name) {
84 12
            case 'color':
85 4
                return $this->_color($value);
86 9
            case 'background':
87 3
                return $this->_color($value, true);
88 8
            case 'bold':
89 3
                return $value ? 1 : 21;
90 5
            case 'dim':
91 1
                return $value ? 2 : 22;
92 4
            case 'underline':
93 1
                return $value ? 4 : 24;
94 3
            case 'blink':
95 1
                return $value ? 5 : 25;
96 2
            case 'invert':
97 1
                return $value ? 7 : 27;
98
        }
99
100 1
        return null;
101
    }
102
103
    private function _set($style)
104
    {
105 12
        $escape = "\e[".implode(';', array_map(function ($style, $name) {
106 12
            return $this->_style($style, $name);
107 12
        }, array_keys($style), $style)).'m';
108
109 12
        return $escape === "\e[m" ? null : $escape;
110
    }
111
112 2
    public function reset()
113
    {
114 2
        return "\e[0m";
115
    }
116
}
117