1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Matks\Vivian\Border; |
4
|
|
|
|
5
|
|
|
use Matks\Vivian\Output\BorderedElement; |
6
|
|
|
use Matks\Vivian\Util; |
7
|
|
|
use Exception; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Border manager |
11
|
|
|
*/ |
12
|
|
|
class BorderManager |
13
|
|
|
{ |
14
|
|
|
const STYLE_UNDERLINE = 'underlineBorder'; |
15
|
|
|
const STYLE_DOUBLE_UNDERLINE = 'doubleUnderlineBorder'; |
16
|
|
|
const STYLE_BORDER = 'border'; |
17
|
|
|
const STYLE_DOUBLE_BORDER = 'doubleBorder'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Static calls interface |
21
|
|
|
*/ |
22
|
|
|
public static function __callstatic($name, $params) |
23
|
|
|
{ |
24
|
1 |
|
if (!in_array($name, static::getKnownBorders())) { |
25
|
1 |
|
throw new Exception("Unknown border function name $name"); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
// target string is expected to be: |
29
|
1 |
|
$targetString = $params[0][0]; |
30
|
1 |
|
$functionName = '__' . $name; |
31
|
|
|
|
32
|
1 |
|
return static::$functionName($targetString); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get known borders |
37
|
|
|
* |
38
|
|
|
* @return string[] |
39
|
|
|
*/ |
40
|
|
|
public static function getKnownBorders() |
41
|
|
|
{ |
42
|
|
|
$styles = array( |
43
|
1 |
|
static::STYLE_UNDERLINE, |
44
|
1 |
|
static::STYLE_DOUBLE_UNDERLINE, |
45
|
1 |
|
static::STYLE_BORDER, |
46
|
1 |
|
static::STYLE_DOUBLE_BORDER |
47
|
1 |
|
); |
48
|
|
|
|
49
|
1 |
|
return $styles; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Render bordered string |
54
|
|
|
* |
55
|
|
|
* @param string $string |
56
|
|
|
* @param Border $border |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
* @throws Exception |
60
|
|
|
*/ |
61
|
1 |
|
public static function buildBorder($string, Border $border) |
62
|
|
|
{ |
63
|
1 |
|
switch ($border->getType()) { |
64
|
1 |
|
case Border::TYPE_UNDERLINE: |
65
|
1 |
|
$result = self::buildUnderline($string, $border); |
66
|
1 |
|
break; |
67
|
1 |
|
case Border::TYPE_FRAME: |
68
|
1 |
|
$result = self::buildFrame($string, $border); |
69
|
1 |
|
break; |
70
|
|
|
default: |
71
|
|
|
throw new Exception('Unknown border type ' . $border->getType()); |
72
|
1 |
|
} |
73
|
|
|
|
74
|
1 |
|
return $result; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Underline with the given Border the given string |
79
|
|
|
* |
80
|
|
|
* @param string $string |
81
|
|
|
* @param Border $border |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
private static function buildUnderline($string, Border $border) |
86
|
|
|
{ |
87
|
1 |
|
$stringLength = Util::getVisibleStringLength($string); |
88
|
1 |
|
$underline = Util::buildPatternLine($border->getLineCharacter(), $stringLength); |
89
|
|
|
|
90
|
1 |
|
$result = $string . PHP_EOL . $underline . PHP_EOL; |
91
|
1 |
|
|
92
|
1 |
|
return $result; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Frame with the given Border the given string |
97
|
|
|
* |
98
|
|
|
* @param string $string |
99
|
|
|
* @param Border $border |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
private static function buildFrame($string, Border $border) |
104
|
|
|
{ |
105
|
1 |
|
$stringLength = Util::getVisibleStringLength($string); |
106
|
|
|
|
107
|
1 |
|
$line = Util::buildPatternLine($border->getLineCharacter(), $stringLength + 2); |
108
|
|
|
|
109
|
1 |
|
$firstLine = $border->getCrossCharacter() . $line . $border->getCrossCharacter(); |
110
|
1 |
|
$mainLine = $border->getColumnCharacter() . ' ' . $string . ' ' . $border->getColumnCharacter(); |
111
|
1 |
|
$lastLine = $firstLine; |
112
|
|
|
|
113
|
1 |
|
$result = $firstLine . PHP_EOL . $mainLine . PHP_EOL . $lastLine . PHP_EOL; |
114
|
|
|
|
115
|
1 |
|
return $result; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Underline a string with '-' |
120
|
|
|
* |
121
|
|
|
* Be careful, this adds two end-of-line |
122
|
|
|
* |
123
|
|
|
* @param string $string |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
private static function __underlineBorder($string) |
128
|
|
|
{ |
129
|
1 |
|
$border = new Border(Border::TYPE_UNDERLINE); |
130
|
1 |
|
$borderedElement = new BorderedElement($string, $border); |
131
|
|
|
|
132
|
1 |
|
return $borderedElement->render(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Underline a string with '=' |
137
|
|
|
* |
138
|
|
|
* Be careful, this adds two end-of-line |
139
|
|
|
* |
140
|
|
|
* @param string $string |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
private static function __doubleUnderlineBorder($string) |
145
|
|
|
{ |
146
|
|
|
$border = new Border(Border::TYPE_UNDERLINE, '='); |
147
|
|
|
$borderedElement = new BorderedElement($string, $border); |
148
|
|
|
|
149
|
|
|
return $borderedElement->render(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Draw a border around a string |
154
|
|
|
* |
155
|
|
|
* @param string $string |
156
|
|
|
* |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
private static function __border($string) |
160
|
|
|
{ |
161
|
|
|
$border = new Border(Border::TYPE_FRAME); |
162
|
|
|
$borderedElement = new BorderedElement($string, $border); |
163
|
|
|
|
164
|
|
|
return $borderedElement->render(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Draw a double border around a string |
169
|
|
|
* |
170
|
|
|
* @param string $string |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
private static function __doubleBorder($string) |
175
|
|
|
{ |
176
|
|
|
$border = new Border(Border::TYPE_FRAME, '=', '#', '*'); |
177
|
|
|
$borderedElement = new BorderedElement($string, $border); |
178
|
|
|
|
179
|
|
|
return $borderedElement->render(); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|