1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\ConsoleColour; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\ConsoleColour\Contracts\DefaultThemes; |
6
|
|
|
use AlecRabbit\ConsoleColour\Exception\InvalidStyleException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @method italic(string $text) |
10
|
|
|
* @method dark(string $text) |
11
|
|
|
* @method darkItalic(string $text) |
12
|
|
|
* @method white(string $text) |
13
|
|
|
* @method whiteBold(string $text) |
14
|
|
|
* @method comment(string $text) |
15
|
|
|
* @method yellow(string $text) |
16
|
|
|
* @method error(string $text) |
17
|
|
|
* @method red(string $text) |
18
|
|
|
* @method green(string $text) |
19
|
|
|
* @method info(string $text) |
20
|
|
|
* @method underline(string $text) |
21
|
|
|
* @method underlineBold(string $text) |
22
|
|
|
* @method underlineItalic(string $text) |
23
|
|
|
*/ |
24
|
|
|
class Theme implements DefaultThemes |
25
|
|
|
{ |
26
|
|
|
/** @var array */ |
27
|
|
|
protected static $themesArr; |
28
|
|
|
|
29
|
|
|
/** @var bool */ |
30
|
|
|
protected $doColorize = false; |
31
|
|
|
|
32
|
|
|
/** @var ConsoleColour */ |
33
|
|
|
protected $color; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Themed constructor. |
37
|
|
|
* @param null|bool $colorize |
38
|
|
|
* @throws InvalidStyleException |
39
|
|
|
*/ |
40
|
6 |
|
public function __construct(?bool $colorize = null) |
41
|
|
|
{ |
42
|
6 |
|
$this->color = new ConsoleColour(); |
43
|
6 |
|
$this->doColorize = $this->refineColorize($colorize); |
44
|
6 |
|
$this->setThemes(); |
45
|
6 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param null|bool $colorize |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
6 |
|
protected function refineColorize(?bool $colorize): bool |
52
|
|
|
{ |
53
|
6 |
|
if ($supported = $this->color->isSupported()) { |
54
|
6 |
|
return $colorize ?? $supported; |
55
|
|
|
} |
56
|
|
|
// @codeCoverageIgnoreStart |
57
|
|
|
return false; |
58
|
|
|
// @codeCoverageIgnoreEnd |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @throws InvalidStyleException |
63
|
|
|
*/ |
64
|
6 |
|
protected function setThemes(): void |
65
|
|
|
{ |
66
|
6 |
|
foreach ($this->allThemes() as $name => $styles) { |
67
|
6 |
|
$this->color->addTheme($name, $styles); |
68
|
|
|
} |
69
|
6 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return array |
73
|
|
|
* |
74
|
|
|
* @psalm-suppress RedundantConditionGivenDocblockType |
75
|
|
|
*/ |
76
|
6 |
|
public function allThemes(): array |
77
|
|
|
{ |
78
|
6 |
|
if (null !== static::$themesArr) { |
|
|
|
|
79
|
6 |
|
return static::$themesArr; |
80
|
|
|
} |
81
|
|
|
return |
82
|
1 |
|
static::$themesArr = $this->prepareThemes(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
1 |
|
protected function prepareThemes(): array |
89
|
|
|
{ |
90
|
1 |
|
return static::THEMES; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $name |
95
|
|
|
* @param array $arguments |
96
|
|
|
* @return string |
97
|
|
|
* @throws \Throwable |
98
|
|
|
*/ |
99
|
6 |
|
public function __call(string $name, array $arguments): string |
100
|
|
|
{ |
101
|
6 |
|
$this->assertMethodName($name); |
102
|
4 |
|
$this->assertArgs($name, $arguments); |
103
|
|
|
|
104
|
|
|
return |
105
|
3 |
|
$this->apply(static::$themesArr[$name], $arguments[0]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $name |
110
|
|
|
*/ |
111
|
6 |
|
protected function assertMethodName(string $name): void |
112
|
|
|
{ |
113
|
6 |
|
if (!\array_key_exists($name, static::$themesArr)) { |
114
|
2 |
|
throw new \BadMethodCallException('Unknown method call [' . static::class . '::' . $name . '].'); |
115
|
|
|
} |
116
|
4 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $name |
120
|
|
|
* @param array $arguments |
121
|
|
|
*/ |
122
|
4 |
|
protected function assertArgs(string $name, array $arguments): void |
123
|
|
|
{ |
124
|
4 |
|
if (1 !== \count($arguments)) { |
125
|
2 |
|
throw new \ArgumentCountError( |
126
|
2 |
|
'Method [' . static::class . '::' . $name . '] accepts only one argument.' |
127
|
|
|
); |
128
|
|
|
} |
129
|
3 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param array|string $style |
133
|
|
|
* @param string $text |
134
|
|
|
* @return string |
135
|
|
|
* @throws \Throwable |
136
|
|
|
*/ |
137
|
3 |
|
protected function apply($style, $text): string |
138
|
|
|
{ |
139
|
|
|
return |
140
|
3 |
|
$this->doColorize ? $this->color->apply($style, $text) : $text; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|