1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Tools\Reports\Formatters\Colour; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\ConsoleColour; |
6
|
|
|
use AlecRabbit\Exception\ColorException; |
7
|
|
|
use AlecRabbit\Exception\InvalidStyleException; |
8
|
|
|
use AlecRabbit\Tools\Reports\Formatters\Contracts\Themes; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @method dark(string $text) |
12
|
|
|
* @method comment(string $text) |
13
|
|
|
* @method yellow(string $text) |
14
|
|
|
* @method error(string $text) |
15
|
|
|
* @method red(string $text) |
16
|
|
|
* @method green(string $text) |
17
|
|
|
* @method info(string $text) |
18
|
|
|
*/ |
19
|
|
|
class Theme implements Themes |
20
|
|
|
{ |
21
|
|
|
/** @var bool */ |
22
|
|
|
protected $doColorize; |
23
|
|
|
|
24
|
|
|
/** @var ConsoleColour */ |
25
|
|
|
protected $color; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Themed constructor. |
29
|
|
|
* @param bool $colorize |
30
|
|
|
* @throws InvalidStyleException |
31
|
|
|
*/ |
32
|
|
|
public function __construct(bool $colorize = true) |
33
|
|
|
{ |
34
|
|
|
$this->doColorize = $colorize; |
35
|
|
|
$this->color = new ConsoleColour(); |
36
|
|
|
$this->setThemes(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @throws InvalidStyleException |
41
|
|
|
* |
42
|
|
|
*/ |
43
|
|
|
protected function setThemes(): void |
44
|
|
|
{ |
45
|
|
|
foreach (static::THEMES as $name => $styles) { |
46
|
|
|
$this->color->addTheme($name, $styles); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $name |
52
|
|
|
* @param array $arguments |
53
|
|
|
* @return string |
54
|
|
|
* @throws \Throwable |
55
|
|
|
*/ |
56
|
|
|
public function __call(string $name, array $arguments): string |
57
|
|
|
{ |
58
|
|
|
$this->assertMethodName($name); |
59
|
|
|
$this->assertArgs($name, $arguments); |
60
|
|
|
|
61
|
|
|
return |
62
|
|
|
$this->apply(static::THEMES[$name], $arguments[0]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array|string $style |
67
|
|
|
* @param string $text |
68
|
|
|
* @return string |
69
|
|
|
* @throws \Throwable |
70
|
|
|
*/ |
71
|
|
|
protected function apply($style, $text): string |
72
|
|
|
{ |
73
|
|
|
try { |
74
|
|
|
return |
75
|
|
|
$this->doColorize ? $this->color->apply($style, $text) : $text; |
76
|
|
|
} catch (ColorException $e) { |
77
|
|
|
// do nothing |
78
|
|
|
} |
79
|
|
|
return $text; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $name |
84
|
|
|
* @param array $arguments |
85
|
|
|
*/ |
86
|
|
|
protected function assertArgs(string $name, array $arguments): void |
87
|
|
|
{ |
88
|
|
|
if (1 !== \count($arguments)) { |
89
|
|
|
throw new \ArgumentCountError('Method [' . $name . '] accepts only one argument.'); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $name |
95
|
|
|
*/ |
96
|
|
|
protected function assertMethodName(string $name): void |
97
|
|
|
{ |
98
|
|
|
if (!\array_key_exists($name, static::THEMES)) { |
99
|
|
|
throw new \BadMethodCallException('Unknown method call [' . $name . '] in [' . static::class . '].'); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|