1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Matks\Vivian\Color; |
4
|
|
|
|
5
|
|
|
use Matks\Vivian\Output\TextElement; |
6
|
|
|
use Exception; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Background Color manager |
10
|
|
|
*/ |
11
|
|
|
class BackgroundColorManager extends AbstractColorManager |
12
|
|
|
{ |
13
|
|
|
const BLACK = 'black'; |
14
|
|
|
const RED = 'red'; |
15
|
|
|
const GREEN = 'green'; |
16
|
|
|
const YELLOW = 'yellow'; |
17
|
|
|
const BLUE = 'blue'; |
18
|
|
|
const PURPLE = 'purple'; |
19
|
|
|
const CYAN = 'cyan'; |
20
|
|
|
const WHITE = 'white'; |
21
|
|
|
|
22
|
|
|
const BASH_BACKGROUND_BLACK = 40; |
23
|
|
|
const BASH_BACKGROUND_RED = 41; |
24
|
|
|
const BASH_BACKGROUND_GREEN = 42; |
25
|
|
|
const BASH_BACKGROUND_YELLOW = 43; |
26
|
|
|
const BASH_BACKGROUND_BLUE = 44; |
27
|
|
|
const BASH_BACKGROUND_PURPLE = 45; |
28
|
|
|
const BASH_BACKGROUND_CYAN = 46; |
29
|
|
|
const BASH_BACKGROUND_WHITE = 47; |
30
|
|
|
|
31
|
|
|
const BACKGROUND_COLOR_FUNCTION_NAME_REGEX = '^(back)_([a-zA-Z]*)$'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Static calls interface to allow calls such as back_green('hello'); |
35
|
|
|
*/ |
36
|
|
|
public static function __callstatic($name, $params) |
37
|
|
|
{ |
38
|
|
|
|
39
|
1 |
|
$matches = static::isBackgroundCall($name); |
40
|
|
|
|
41
|
1 |
|
if (null === $matches) { |
42
|
|
|
throw new Exception("Unknown background color function name $name"); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
$colorName = $matches[2]; |
46
|
1 |
|
$knownColors = static::getKnownColors(); |
47
|
1 |
|
$colorID = $knownColors[$colorName]; |
48
|
|
|
|
49
|
1 |
|
if (!$colorID) { |
50
|
|
|
throw new Exception("Unknown background color name $name"); |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
$color = static::color($colorID); |
54
|
|
|
|
55
|
|
|
// target string is expected to be: |
56
|
1 |
|
$targetString = $params[0][0]; |
57
|
|
|
|
58
|
1 |
|
$element = new TextElement($targetString); |
59
|
1 |
|
$element->setBackgroundColor($color); |
60
|
|
|
|
61
|
1 |
|
return $element->render(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Analyse function name |
66
|
|
|
* |
67
|
|
|
* @param string $functionName |
68
|
|
|
* |
69
|
|
|
* @return array|bool |
70
|
|
|
*/ |
71
|
|
|
public static function isBackgroundCall($functionName) |
72
|
|
|
{ |
73
|
1 |
|
$matches = array(); |
74
|
1 |
|
$pattern = '#' . static::BACKGROUND_COLOR_FUNCTION_NAME_REGEX . '#'; |
75
|
|
|
|
76
|
1 |
|
if (!preg_match($pattern, $functionName, $matches)) { |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
return $matches; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get allowed colors |
85
|
|
|
* |
86
|
|
|
* @return string[] |
87
|
|
|
*/ |
88
|
|
|
public static function getKnownColors() |
89
|
|
|
{ |
90
|
|
|
$colors = array( |
91
|
1 |
|
static::BLACK => static::BASH_BACKGROUND_BLACK, |
92
|
1 |
|
static::RED => static::BASH_BACKGROUND_RED, |
93
|
1 |
|
static::GREEN => static::BASH_BACKGROUND_GREEN, |
94
|
1 |
|
static::YELLOW => static::BASH_BACKGROUND_YELLOW, |
95
|
1 |
|
static::BLUE => static::BASH_BACKGROUND_BLUE, |
96
|
1 |
|
static::PURPLE => static::BASH_BACKGROUND_PURPLE, |
97
|
1 |
|
static::CYAN => static::BASH_BACKGROUND_CYAN, |
98
|
1 |
|
static::WHITE => static::BASH_BACKGROUND_WHITE |
99
|
1 |
|
); |
100
|
|
|
|
101
|
1 |
|
return $colors; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get Color class |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public static function getColorClass() |
110
|
|
|
{ |
111
|
1 |
|
return '\Matks\Vivian\Color\BackgroundColor'; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|