CliColor::setStyle()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 3
rs 10
1
<?php
2
/**
3
 * Console colors
4
 *
5
 * @file      CliColor.php
6
 *
7
 * PHP version 8.0+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2021 Alexander Yancharuk
11
 * @date      Птн Фев 15 21:54:29 2013
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>.
14
 */
15
16
namespace Veles\Tools;
17
18
use Exception;
19
20
/**
21
 * Class CliColor
22
 * @author  Alexander Yancharuk <alex at itvault dot info>
23
 */
24
class CliColor
25
{
26
	private $color;
27
28
	private $style;
29
30
	private $string;
31
32
	private static $colors = [
33
		'black'  => '0',
34
		'red'    => '1',
35
		'green'  => '2',
36
		'yellow' => '3',
37
		'blue'   => '4',
38
		'purple' => '5',
39
		'cyan'   => '6',
40
		'white'  => '7'
41
	];
42
43
	private static $styles = [
44
		'0' => 'default',
45
		'1' => 'bold',
46
		'2' => 'dark',
47
		'4' => 'underline',
48
		'7' => 'invert',
49
		'9' => 'strike'
50
	];
51
52
	/**
53
	 * Encapsulate string in color esc-sequences
54
	 *
55
	 * @param string $string Строка
56
	 *
57
	 * @return string
58
	 */
59 5
	public function __invoke($string = null)
60
	{
61 5
		if (null === $string) {
62 2
			if (null === $this->string) {
63 1
				return '';
64
			}
65
66 1
			$string =& $this->string;
67
		}
68
69 4
		return "\033[{$this->getStyle()};3{$this->getColor()}m$string\033[0m";
70
	}
71
72
	/**
73
	 * Constructor
74
	 *
75
	 * @param string $color Color
76
	 * @param array  $style Styles array
77
	 *
78
	 * @throws Exception
79
	 */
80 18
	public function __construct($color = 'green', array $style = ['default'])
81
	{
82 18
		$this->setColor($color);
83 18
		$this->setStyle($style);
84
	}
85
86
	/**
87
	 * Output object as string
88
	 *
89
	 * @return mixed
90
	 */
91 4
	public function __toString()
92
	{
93 4
		if (null === $this->string) {
94 1
			return '';
95
		}
96
97 3
		$style = $this->getStyle();
98 3
		$color = $this->getColor();
99
100 3
		return "\033[$style;3{$color}m$this->string\033[0m";
101
	}
102
103
	/**
104
	 * Add string
105
	 *
106
	 * @param string $string String that should be colorized
107
	 *
108
	 * @throws Exception
109
	 * @return CliColor
110
	 */
111 2
	public function setString($string = null)
112
	{
113 2
		if (null === $string) {
114 1
			throw new Exception('Not valid string!');
115
		}
116
117 1
		$this->string = (string) $string;
118
119 1
		return $this;
120
	}
121
122
	/**
123
	 * Set style
124
	 *
125
	 * @param array $style Style
126
	 *
127
	 * @throws Exception
128
	 * @return CliColor
129
	 */
130 18
	public function setStyle(array $style = [])
131
	{
132 18
		$styles = array_flip(self::$styles);
133
134 18
		foreach ($style as $value) {
135 18
			if (!isset($styles[$value])) {
136 1
				throw new Exception("Not valid style: '$value'!");
137
			}
138
		}
139
140 18
		$this->style = $style;
141
142 18
		return $this;
143
	}
144
145
	/**
146
	 * Set color
147
	 *
148
	 * @param string $color Цвет
149
	 *
150
	 * @throws Exception
151
	 * @return CliColor
152
	 */
153 18
	public function setColor($color = null)
154
	{
155 18
		if (null === $color || !is_string($color)) {
156 2
			throw new Exception('Not valid color!');
157
		}
158
159 18
		if (!isset(self::$colors[$color])) {
160 1
			throw new Exception("Not valid color: '$color'!");
161
		}
162
163 18
		$this->color = $color;
164
165 18
		return $this;
166
	}
167
168
	/**
169
	 * Get style
170
	 *
171
	 * @return string
172
	 */
173 7
	private function getStyle()
174
	{
175 7
		$styles = array_keys(array_intersect(self::$styles, $this->style));
176
177 7
		return implode(';', $styles);
178
	}
179
180
	/**
181
	 * Get color
182
	 *
183
	 * @return string
184
	 */
185 7
	private function getColor()
186
	{
187 7
		return self::$colors[$this->color];
188
	}
189
}
190