Color   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 67
c 1
b 0
f 0
dl 0
loc 108
ccs 49
cts 49
cp 1
rs 10
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getBackgroundColors() 0 10 1
A getForegroundColors() 0 18 1
A getColoredString() 0 21 6
A red() 0 2 1
A yellow() 0 2 1
A green() 0 2 1
A echo() 0 2 1
A purple() 0 2 1
A blue() 0 2 1
1
<?php
2
3
4
namespace Stefaminator\Cli;
5
6
7
class Color {
8
9
    public const FOREGROUND_COLOR_BLACK = '0;30';
10
    public const FOREGROUND_COLOR_DARK_GRAY = '1;30';
11
    public const FOREGROUND_COLOR_BLUE = '0;34';
12
    public const FOREGROUND_COLOR_LIGHT_BLUE = '1;34';
13
    public const FOREGROUND_COLOR_GREEN = '0;32';
14
    public const FOREGROUND_COLOR_LIGHT_GREEN = '1;32';
15
    public const FOREGROUND_COLOR_CYAN = '0;36';
16
    public const FOREGROUND_COLOR_LIGHT_CYAN = '1;36';
17
    public const FOREGROUND_COLOR_RED = '0;31';
18
    public const FOREGROUND_COLOR_LIGHT_RED = '1;31';
19
    public const FOREGROUND_COLOR_PURPLE = '0;35';
20
    public const FOREGROUND_COLOR_LIGHT_PURPLE = '1;35';
21
    public const FOREGROUND_COLOR_BROWN = '0;33';
22
    public const FOREGROUND_COLOR_YELLOW = '1;33';
23
    public const FOREGROUND_COLOR_LIGHT_GRAY = '0;37';
24
    public const FOREGROUND_COLOR_WHITE = '1;37';
25
26
    public const BACKGROUND_COLOR_BLACK = '40';
27
    public const BACKGROUND_COLOR_RED = '41';
28
    public const BACKGROUND_COLOR_GREEN = '42';
29
    public const BACKGROUND_COLOR_YELLOW = '43';
30
    public const BACKGROUND_COLOR_BLUE = '44';
31
    public const BACKGROUND_COLOR_PURPLE = '45';
32
    public const BACKGROUND_COLOR_CYAN = '46';
33
    public const BACKGROUND_COLOR_LIGHT_GRAY = '47';
34
35 7
    public static function getForegroundColors(): array {
36
        return [
37 7
            self::FOREGROUND_COLOR_BLACK => 'BLACK',
38 7
            self::FOREGROUND_COLOR_DARK_GRAY => 'DARK_GRAY',
39 7
            self::FOREGROUND_COLOR_BLUE => 'BLUE',
40 7
            self::FOREGROUND_COLOR_LIGHT_BLUE => 'LIGHT_BLUE',
41 7
            self::FOREGROUND_COLOR_GREEN => 'GREEN',
42 7
            self::FOREGROUND_COLOR_LIGHT_GREEN => 'LIGHT_GREEN',
43 7
            self::FOREGROUND_COLOR_CYAN => 'CYAN',
44 7
            self::FOREGROUND_COLOR_LIGHT_CYAN => 'LIGHT_CYAN',
45 7
            self::FOREGROUND_COLOR_RED => 'RED',
46 7
            self::FOREGROUND_COLOR_LIGHT_RED => 'LIGHT_RED',
47 7
            self::FOREGROUND_COLOR_PURPLE => 'PURPLE',
48 7
            self::FOREGROUND_COLOR_LIGHT_PURPLE => 'LIGHT_PURPLE',
49 7
            self::FOREGROUND_COLOR_BROWN => 'BROWN',
50 7
            self::FOREGROUND_COLOR_YELLOW => 'YELLOW',
51 7
            self::FOREGROUND_COLOR_LIGHT_GRAY => 'LIGHT_GRAY',
52 7
            self::FOREGROUND_COLOR_WHITE => 'WHITE'
53
        ];
54
    }
55
56 2
    public static function getBackgroundColors(): array {
57
        return [
58 2
            self::BACKGROUND_COLOR_BLACK => 'BLACK',
59 2
            self::BACKGROUND_COLOR_RED => 'RED',
60 2
            self::BACKGROUND_COLOR_GREEN => 'GREEN',
61 2
            self::BACKGROUND_COLOR_YELLOW => 'YELLOW',
62 2
            self::BACKGROUND_COLOR_BLUE => 'BLUE',
63 2
            self::BACKGROUND_COLOR_PURPLE => 'PURPLE',
64 2
            self::BACKGROUND_COLOR_CYAN => 'CYAN',
65 2
            self::BACKGROUND_COLOR_LIGHT_GRAY => 'LIGHT_GRAY'
66
        ];
67
    }
68
69
    // Returns colored string
70 7
    public static function getColoredString($string, $foreground_color = null, $background_color = null): string {
71
72 7
        if (empty($string)) {
73 4
            return '';
74
        }
75
76 7
        $colored_string = '';
77
78
        // Check if given foreground color found
79 7
        if ($foreground_color !== null && array_key_exists($foreground_color, self::getForegroundColors())) {
80 7
            $colored_string .= "\e[" . $foreground_color . 'm';
81
        }
82
        // Check if given background color found
83 7
        if ($background_color !== null && array_key_exists($background_color, self::getBackgroundColors())) {
84 2
            $colored_string .= "\e[" . $background_color . 'm';
85
        }
86
87
        // Add string and end coloring
88 7
        $colored_string .= $string . "\e[0m";
89
90 7
        return $colored_string;
91
    }
92
93 1
    public static function echo($string, $foreground_color = null, $background_color = null): void {
94 1
        echo self::getColoredString($string, $foreground_color, $background_color);
95 1
    }
96
97 1
    public static function red($string): string {
98 1
        return self::getColoredString($string, self::FOREGROUND_COLOR_RED);
99
    }
100
101 1
    public static function green($string): string {
102 1
        return self::getColoredString($string, self::FOREGROUND_COLOR_GREEN);
103
    }
104
105 1
    public static function blue($string): string {
106 1
        return self::getColoredString($string, self::FOREGROUND_COLOR_BLUE);
107
    }
108
109 1
    public static function yellow($string): string {
110 1
        return self::getColoredString($string, self::FOREGROUND_COLOR_YELLOW);
111
    }
112
113 1
    public static function purple($string): string {
114 1
        return self::getColoredString($string, self::FOREGROUND_COLOR_PURPLE);
115
    }
116
117
}