Colorizer   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 0
dl 0
loc 205
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A color() 0 13 2
A setForegroundCode() 0 19 3
A setBackgroundColor() 0 22 5
A getForegroundColors() 0 4 1
A getBackgroundColors() 0 4 1
A doColorize() 0 20 3
A getForegroundText() 0 4 1
A getBackgroundText() 0 4 1
1
<?php
2
3
namespace CliColorizer;
4
5
class Colorizer implements ColorizerInterface
6
{
7
8
    public const FOREGROUND_BLACK     = '0;30';
9
    public const FOREGROUND_RED       = '0;31';
10
    public const FOREGROUND_GREEN     = '0;32';
11
    public const FOREGROUND_BROWN     = '0;33';
12
    public const FOREGROUND_BLUE      = '0;34';
13
    public const FOREGROUND_MAGENTA   = '0;35';
14
    public const FOREGROUND_CYAN      = '0;36';
15
    public const FOREGROUND_LIGHTGREY = '0;37';
16
    public const FOREGROUND_DARKGREY  = '1;30';
17
    public const FOREGROUND_LIGHTRED  = '1;31';
18
    public const FOREGROUND_LIGHTGREEN = '1;32';
19
    public const FOREGROUND_YELLOW     = '1;33';
20
    public const FOREGROUND_LIGHTBLUE  = '1;34';
21
    public const FOREGROUND_PURPLE     = '1;35';
22
    public const FOREGROUND_LIGHTCYAN  = '1;36';
23
    public const FOREGROUND_WHITE      = '1;37';
24
25
    public const BACKGROUND_BLACK     = 40;
26
    public const BACKGROUND_RED       = 41;
27
    public const BACKGROUND_GREEN     = 42;
28
    public const BACKGROUND_YELLOW    = 43;
29
    public const BACKGROUND_BLUE      = 44;
30
    public const BACKGROUND_MAGENTA   = 45;
31
    public const BACKGROUND_CYAN      = 46;
32
    public const BACKGROUND_LIGHTGREY = 47;
33
34
    /** @var string */
35
    protected $foregroundColor;
36
37
    /** @var string */
38
    protected $foregroundText;
39
40
    /** @var string */
41
    protected $backgroundColor;
42
43
    /** @var string */
44
    protected $backgroundText;
45
46
    /** @var array  */
47
    public static $foregroundTexts = [
48
        '0;30' => 'black',
49
        '0;31' => 'red',
50
        '0;32' => 'green',
51
        '0;33' => 'brown',
52
        '0;34' => 'blue',
53
        '0;35' => 'magenta',
54
        '0;36' => 'cyan',
55
        '0;37' => 'lightGrey',
56
        '1;30' => 'darkGrey',
57
        '1;31' => 'lightRed',
58
        '1;32' => 'lightGreen',
59
        '1;33' => 'yellow',
60
        '1;34' => 'lightBlue',
61
        '1;35' => 'lightPurple',
62
        '1;36' => 'lightCyan',
63
        '1;37' => 'white',
64
    ];
65
66
    /** @var array  */
67
    public static $backgroundTexts = [
68
        40 => 'black',
69
        41 => 'red',
70
        42 => 'green',
71
        43 => 'yellow',
72
        44 => 'blue',
73
        45 => 'magenta',
74
        46 => 'cyan',
75
        47 => 'lightgrey'
76
    ];
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function color(
82
        string $sentence,
83
        string $foreground = null,
84
        int $background = null,
85
        bool $endOfLine = false
86
    ) {
87
        $this->setForegroundCode($foreground);
88
        $this->setBackgroundColor($background);
89
90
        $sentence = $endOfLine ? $sentence . PHP_EOL : $sentence;
91
92
        return $this->doColorize($sentence, [$foreground, $background]);
93
    }
94
95
    /**
96
     * @param string $foregroundCode
97
     *
98
     * @param null   $text
99
     *
100
     * @return bool|Colorizer
101
     */
102
    public function setForegroundCode(string $foregroundCode, $text = null)
103
    {
104
        $this->foregroundColor = $foregroundCode;
105
106
        if (!array_key_exists($this->foregroundColor, self::$foregroundTexts)) {
107
            // todo: throw a custom exception
108
            return false;
109
        }
110
111
        if (null === $text) {
112
            $this->foregroundText = self::$foregroundTexts[$foregroundCode] ?? 'unknown foreground color';
113
114
            return $this;
115
        }
116
117
        $this->foregroundText = $text;
118
119
        return $this;
120
    }
121
122
    /**
123
     * @param string|int $backgroundColor
124
     * @param null   $text
125
     *
126
     * @return bool|Colorizer
127
     */
128
    public function setBackgroundColor($backgroundColor, $text = null)
129
    {
130
        if (is_int($backgroundColor)) {
131
            $backgroundColor = (string) $backgroundColor;
132
        }
133
        $this->backgroundColor = $backgroundColor;
134
135
        if ($this->backgroundColor < self::BACKGROUND_BLACK || $this->backgroundColor > self::BACKGROUND_LIGHTGREY) {
136
            // todo: throw a custom exception
137
            return false;
138
        }
139
140
        if (null === $text) {
141
            $this->backgroundText = self::$backgroundTexts[$backgroundColor] ?? 'unknown background color';
142
143
            return $this;
144
        }
145
146
        $this->backgroundText = $text;
147
148
        return $this;
149
    }
150
151
    /**
152
     * @return array
153
     */
154
    public function getForegroundColors(): array
155
    {
156
        return array_keys(self::$foregroundTexts);
157
    }
158
159
    /**
160
     * @return array
161
     */
162
    public function getBackgroundColors(): array
163
    {
164
        return array_keys(self::$backgroundTexts);
165
    }
166
167
    /**
168
     * @param string $sentence
169
     * @param array  $arguments
170
     *
171
     * @return string
172
     */
173
    private function doColorize(string $sentence, array $arguments): string
174
    {
175
        $sentence = str_pad($sentence, 15, ' ', STR_PAD_BOTH);
176
        [$foreground, $background] = $arguments;
0 ignored issues
show
Bug introduced by
The variable $foreground does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $background does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
177
        $coloredOutput = '';
178
179
        if (array_key_exists($foreground, self::$foregroundTexts)) {
180
            $coloredOutput .= "\033[" . $foreground . ';';
181
        } else{
182
            // todo: throw an exception or set default value
183
        }
184
185
        if (array_key_exists($background, self::$backgroundTexts)) {
186
            $coloredOutput .= $background . 'm';
187
        }
188
189
        $coloredOutput .= $sentence . "\033[0m";
190
191
        return $coloredOutput . PHP_EOL;
192
    }
193
194
    /**
195
     * @return string
196
     */
197
    public function getForegroundText(): string
198
    {
199
        return $this->foregroundText;
200
    }
201
202
    /**
203
     * @return string
204
     */
205
    public function getBackgroundText(): string
206
    {
207
        return $this->backgroundText;
208
    }
209
}
210