This repository was archived by the owner on Feb 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFont.php
More file actions
221 lines (191 loc) · 4.19 KB
/
Font.php
File metadata and controls
221 lines (191 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
namespace Crayon;
use const Crayon\{RED, GREEN, BLUE, YELLOW, MAGENTA, CYAN};
/**
* Class Font
*
* A string representation class which contains all formatting information
* that'll be used when rendering the string.
*
* @package Crayon
*/
class Font
{
/**
* @var string
*/
const BOLD = "\e[1m";
/**
* @var string
*/
const ITALIC = "\e[3m";
/**
* @var string
*/
const UNDERLINE = "\e[4m";
/**
* @var string
*/
const RESET = "\e[0m";
/**
* the text to be formatted
* @var string|null
*/
private $text;
/**
* boolean value to indicate if the text shall be rendered bold
* @var bool
*/
private $bold = false;
/**
* boolean value to indicate if the text shall be rendered italic
* @var bool
*/
private $italic = false;
/**
* boolean value to indicate if the text shall be rendered underlined
* @var bool
*/
private $underline = false;
/**
* may contain the color format string if available
* @var string|null
*/
private $color;
/**
* sets the information that the color should be applied as background
* @var bool
*/
private $background = false;
/**
* Font constructor.
* @param string $text
*/
public function __construct(string $text)
{
$this->text = $text;
}
/**
* function to set text bold
* @return Font
*/
public function bold(): Font
{
$this->bold = true;
return $this;
}
/**
* function to set text italic
* @return Font
*/
public function italic(): Font
{
$this->italic = true;
return $this;
}
/**
* function to set text underlined
* @return Font
*/
public function underline(): Font
{
$this->underline = true;
return $this;
}
/**
* function to color the text red
* @return Font
*/
public function red(): Font
{
return $this->color(RED);
}
/**
* function to color the text green
* @return Font
*/
public function green(): Font
{
return $this->color(GREEN);
}
/**
* function to color the text yellow
* @return Font
*/
public function yellow(): Font
{
return $this->color(YELLOW);
}
/**
* function to color the text blue
* @return Font
*/
public function blue(): Font
{
return $this->color(BLUE);
}
/**
* function to color the magenta
* @return Font
*/
public function magenta(): Font
{
return $this->color(MAGENTA);
}
/**
* function to color the text cyan
* @return Font
*/
public function cyan(): Font
{
return $this->color(CYAN);
}
/**
* function to set color as background
* @return Font
*/
public function background(): Font
{
if ($this->color !== null) {
$this->turnToBackground();
}
$this->background = true;
return $this;
}
/**
* function to prepare and render the text with set font options
* @return string
*/
public function __toString()
{
$reset = self::RESET;
$font_color = $this->color ?? '';
$is_bold = $this->bold ? self::BOLD : '';
$is_italic = $this->italic ? self::ITALIC : '';
$is_underline = $this->underline ? self::UNDERLINE : '';
return "{$font_color}{$is_bold}{$is_italic}{$is_underline}{$this->text}{$reset}";
}
/**
* function to set color to a string
* @param string $color
* @return Font
*/
private function color(string $color): Font
{
$this->color = $color;
if ($this->background) {
$this->turnToBackground();
}
return $this;
}
/**
* function to convert color to background color
*/
private function turnToBackground()
{
$this->color = preg_replace_callback('/\\e\[(?<color>\d+)m/', function ($matches) {
$new_color = $matches['color'] + 10;
return "\e[{$new_color}m";
}, $this->color);
}
}