BashColor   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 161
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 9 1
B parseAttributes() 0 29 7
A snakeCase() 0 6 1
A getForegroundColors() 0 4 1
A getBackgroundColors() 0 4 1
1
<?php
2
3
/**
4
 * BashColor.php.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @author    overtrue <[email protected]>
10
 * @copyright 2015 overtrue <[email protected]>
11
 *
12
 * @link      https://github.com/overtrue
13
 * @link      http://overtrue.me
14
 * @link      https://github.com/overtrue/colorization
15
 */
16
17
namespace Overtrue\BashColor;
18
19
/**
20
 * Class BashColor.
21
 */
22
class BashColor
23
{
24
    /**
25
     * Options.
26
     *
27
     * @var array
28
     */
29
    private static $options = array(
30
        'none' => 0, // reset/remove all option
31
        'bold' => 1, // bold/bright
32
        'bright' => 1, // bold/bright
33
        'dim' => 2, // dim
34
        'underlined' => 4, // underlined
35
        'blink' => 5, // blink
36
        'reverse' => 7, // reverse/invert
37
        'invert' => 7, // reverse/invert
38
        'hidden' => 8, // hidden
39
    );
40
41
    /**
42
     * Foreground colors.
43
     *
44
     * @var array
45
     */
46
    private static $foregroundColors = array(
47
        'default' => 39, // default (usually green, white or light gray)
48
        'black' => 30, // black
49
        'red' => 31, // red (don't use with green background)
50
        'green' => 32, // green
51
        'yellow' => 33, // yellow
52
        'blue' => 34, // blue
53
        'magenta' => 35, // magenta/purple
54
        'purple' => 35, // magenta/purple
55
        'cyan' => 36, // cyan
56
        'light_gray' => 37, // light gray
57
        'dark_gray' => 90, // dark gray
58
        'light_red' => 91, // light red
59
        'light_green' => 92, // light green
60
        'light_yellow' => 93, // light yellow
61
        'light_blue' => 94, // light blue
62
        'light_magenta' => 95, // light magenta/pink
63
        'light_pink' => 95, // light magenta/pink
64
        'light_cyan' => 96, // light cyan
65
        'white' => 97, // white
66
    );
67
68
    /**
69
     * Backgound colors.
70
     *
71
     * @var array
72
     */
73
    private static $backgroundColors = array(
74
        'default' => 49,  // Default background color (usually black or blue)
75
        'black' => 40,  // Black
76
        'red' => 41,  // Red
77
        'green' => 42,  // Green
78
        'yellow' => 43,  // Yellow
79
        'blue' => 44,  // Blue
80
        'magenta' => 45,  // Magenta/Purple
81
        'purple' => 45,  // Magenta/Purple
82
        'cyan' => 46,  // Cyan
83
        'light_gray' => 47,  // Light Gray (don't use with white foreground)
84
        'dark_gray' => 100, // Dark Gray (don't use with black foreground)
85
        'light_red' => 101, // Light Red
86
        'light_green' => 102, // Light Green (don't use with white foreground)
87
        'light_yellow' => 103, // Light Yellow (don't use with white foreground)
88
        'light_blue' => 104, // Light Blue (don't use with light yellow foreground)
89
        'light_magenta' => 105, // Light Magenta/Pink (don't use with light foreground)
90
        'light_pink' => 105, // Light Magenta/Pink (don't use with light foreground)
91
        'light_cyan' => 106, // Light Cyan (don't use with white foreground)
92
        'white' => 107, // White (don't use with light foreground)
93
    );
94
95
    /**
96
     * Return colorized string.
97
     *
98
     * @param string $string
99
     *
100
     * @return string
101
     */
102 1
    public static function render($string)
103
    {
104 1
        return preg_replace_callback('~<(?<attributes>[a-z_;A-Z=\s]+)>(?<string>.*?)</>~',
105
            function ($matches) {
106 1
                list($foreground, $background, $option, $endModifier) = BashColor::parseAttributes($matches['attributes']);
107
108 1
                return "\033[{$option};{$background};{$foreground}m{$matches['string']}\033[{$endModifier}m";
109 1
            }, $string);
110
    }
111
112
    /**
113
     * Parse attributes.
114
     *
115
     * @param string $attributesString
116
     *
117
     * @return array
118
     */
119 1
    public static function parseAttributes($attributesString)
120
    {
121
        $attributes = array(
122 1
            'fg' => 'default',
123
            'bg' => 'default',
124
            'opt' => 'none',
125
        );
126
127 1
        foreach (explode(';', $attributesString) as $attribute) {
128 1
            $temp = explode('=', $attribute);
129
130 1
            if (count($temp) < 2) {
131 1
                continue;
132
            }
133
134 1
            $attributes[trim($temp[0])] = self::snakeCase(trim($temp[1]));
135
        }
136
137 1
        !empty(self::$foregroundColors[$attributes['fg']]) || $attributes['fg'] = 'default';
138 1
        !empty(self::$backgroundColors[$attributes['bg']]) || $attributes['bg'] = 'default';
139 1
        !empty(self::$options[$attributes['opt']]) || $attributes['opt'] = 'none';
140
141 1
        $foreground = self::$foregroundColors[$attributes['fg']];
142 1
        $background = self::$backgroundColors[$attributes['bg']];
143 1
        $option = self::$options[$attributes['opt']];
144 1
        $endModifier = $option ? 20 + $option : $option;
145
146 1
        return array($foreground, $background, $option, $endModifier);
147
    }
148
149
    /**
150
     * Snake case.
151
     *
152
     * @param string $string
153
     *
154
     * @return string
155
     */
156
    public static function snakeCase($string)
157
    {
158 1
        return preg_replace_callback('/([A-Z])/', function ($matches) {
159 1
            return '_'.strtolower($matches[1]);
160 1
        }, lcfirst($string));
161
    }
162
163
    /**
164
     * Returns all foreground color names.
165
     *
166
     * @return array
167
     */
168 1
    public static function getForegroundColors()
169
    {
170 1
        return array_keys(self::$foregroundColors);
171
    }
172
173
    /**
174
     * Returns all background color names.
175
     *
176
     * @return array
177
     */
178 1
    public static function getBackgroundColors()
179
    {
180 1
        return array_keys(self::$backgroundColors);
181
    }
182
}
183