-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStyle.php
372 lines (308 loc) · 10.2 KB
/
Style.php
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
namespace Videni\BetterExcel;
use PhpOption\None;
use PhpOption\Option;
use PhpOption\Some;
/**
* This class allows you to set styles for Excel cells quickly and easily.
*
* It is a simple wrapper around the \Vtiful\Kernel\Format class
*
* 为什么不直接使用Vtiful\Kernel\Format? 因为,
* 1. 可以用数组的形式创建 style。
*/
class Style
{
public const FONT_STYLES = ['italic', 'bold'];
/**
* https://xlswriter-docs.viest.me/zh-cn/yang-shi-lie-biao/xia-hua-xian
*/
public const UNDERLINE_STYLES = ['single', 'double', 'single_accounting', 'double_accounting'];
/**
* https://xlswriter-docs.viest.me/zh-cn/yang-shi-lie-biao/bian-kuang-yang-shi-chang-liang
*/
public const BORDER_STYLES = [
'thin', 'medium', 'dashed', 'dotted', 'thick', 'double', 'hair',
'medium_dashed', 'dash_dot', 'medium_dash_dot', 'dash_dot_dot',
'medium_dash_dot_dot', 'slant_dash_dot'
];
/**
* https://xlswriter-docs.viest.me/zh-cn/yang-shi-lie-biao/yan-se-chang-liang
*/
public const COLOR_STYLE = [
'black', 'blue', 'brown', 'cyan', 'gray', 'green', 'lime', 'magenta', 'navy', 'orange', 'pink', 'purple', 'red', 'silver', 'white', 'yellow'
];
public const PATH_FONT_STYLES = 'font.styles';
public const PATH_FONT_SIZE = 'font.size';
public const PATH_FONT_NAME = 'font.name';
public const PATH_FONT_COLOR = 'font.color';
public const PATH_FONT_UNDERLINE = 'font.underline';
public const PATH_STRIKEOUT = 'font.strikeout';
public const PATH_WRAP = 'wrap';
public const PATH_ALIGN = 'align';
public const PATH_BORDER = 'border';
public const PATH_BACKGROUND = 'background';
public const PATH_NUMBER = 'number';
public const PATH_WIDTH = 'width';
public const PATH_HEIGHT = 'height';
public const PATH_UNDERLINE = 'underline';
private $width;
private $height;
private $formats = [];
public function __construct()
{
$this->formats = [];
}
public function italic()
{
$this->setFormat(self::PATH_FONT_STYLES, 'italic', true);
return $this;
}
public function font($color, $size, $name = null)
{
$this->fontColor($color);
$this->fontSize($size);
$this->fontName($name);
return $this;
}
public function fontStyles($styles)
{
$styles = is_array($styles) ? $styles : [$styles];
foreach ($styles as $style) {
$this->throwIfInvalidOption($style, self::FONT_STYLES, 'font style');
$this->{$style}();
}
return $this;
}
/**
*
* 可以是, Vtiful\Kernel\Format颜色相关的常量,也可是预定义颜色名,
* 如black。
* @param string|int $color
*
* @return self
*/
public function fontColor($color)
{
if (is_string($color) ) {
$color = strtolower($color);
if (!in_array($color, self::COLOR_STYLE)) {
throw new \InvalidArgumentException(
sprintf('Invalid color: %s, predefined colors are: %s', $color, join(', ', self::COLOR_STYLE))
);
}
}
$this->setFormat(self::PATH_FONT_COLOR, $color);
return $this;
}
public function fontSize(int $size)
{
$this->setFormat(self::PATH_FONT_SIZE, $size);
return $this;
}
public function fontName(string $name = null)
{
$this->setFormat(self::PATH_FONT_NAME, $name);
return $this;
}
public function bold()
{
$this->setFormat(self::PATH_FONT_STYLES, 'bold', true);
return $this;
}
public function align($horizontal = 'left', $vertical = 'bottom')
{
$horizontalAlignments = [
'left', 'center', 'right', 'fill', 'justify', 'center_across', 'distributed'
];
$this->assetOption($horizontal, $horizontalAlignments, 'horizontal alignment');
$verticalAlignments = [
'top', 'bottom', 'center', 'justify', 'distributed'
];
$this->assetOption($vertical, $verticalAlignments, 'vertical alignment');
$this->setFormat(self::PATH_ALIGN, [$horizontal, $vertical]);
return $this;
}
public function strikeout()
{
$this->setFormat(self::PATH_STRIKEOUT, 'strikeout', true);
return $this;
}
/**
* @param string $underline default "single"
* @return $this
*/
public function underline($underline = 'single')
{
$this->assetOption($underline, self::UNDERLINE_STYLES, 'underline style');
$this->setFormat('underline', $underline);
return $this;
}
public function wrap()
{
$this->setFormat('wrap', true);
return $this;
}
public function border($border)
{
$this->assetOption($border, self::BORDER_STYLES, 'border style');
$this->setFormat(self::PATH_BORDER, $border);
return $this;
}
public function background($background)
{
$this->assetOption($background, self::COLOR_STYLE, 'background color');
$this->setFormat(self::PATH_BACKGROUND, $background);
return $this;
}
/**
*
* https://xlswriter-docs.viest.me/zh-cn/yang-shi-lie-biao/number
*
* example:
* "0.000"
* "#,##0"
* "#,##0.00"
* "0.00"
*
* @param string $format
* @return $this
*/
public function number(string $format)
{
$this->setFormat(self::PATH_NUMBER, $format);
return $this;
}
public function apply(\Closure $callback)
{
// 将格式化处理后的 style 缓存起来,避免同一个 style 每次重新
// format, 这在php-ext-xlswriter 会是严重的性能问题。
$this->formattedStyle = $callback($this->formats);
return $this->formattedStyle;
}
/**
* Set column width.
* usually, for column width, it means how many characters can be displayed
*
* @param int $width
* @return self
*/
public function width(int $width)
{
$this->width = $width;
return $this;
}
/**
* Set row height.
*
* @param int $height
* @return self
*/
public function height(int $height)
{
$this->height = $height;
return $this;
}
public function getWidth()
{
return $this->width;
}
public function getHeight()
{
return $this->height;
}
private function setFormat(string $field, $value, $multiple = false)
{
if ($multiple && !in_array($value, $this->formats[$field]??[])) {
$this->formats[$field][] = $value;
} else {
$this->formats[$field] = $value;
}
}
public static function fromArray(array $formats)
{
$self = new self();
// null is valid value, I use NONE to decide if it is set manually
$self->getFormatFromArray($formats, self::PATH_FONT_STYLES, None::create())
->forAll(function($value) use ($self){
$self->fontStyles($value);
});
$self->getFormatFromArray($formats, self::PATH_FONT_SIZE, None::create())
->forAll(function($value) use ($self){
$self->fontSize($value);
});
$self->getFormatFromArray($formats, self::PATH_FONT_NAME, None::create())
->forAll(function($value) use ($self){
$self->fontName($value);
});
$self->getFormatFromArray($formats, self::PATH_FONT_COLOR, None::create())
->forAll(function($value) use ($self){
$self->fontColor($value);
});
$self->getFormatFromArray($formats, self::PATH_FONT_UNDERLINE, None::create())
->forAll(function($value) use ($self){
$self->underline($value);
});
$self->getFormatFromArray($formats, self::PATH_STRIKEOUT, None::create())
->forAll(function($value) use ($self){
$value && $self->strikeout();
});
$self->getFormatFromArray($formats, self::PATH_WRAP, None::create())
->forAll(function($value) use ($self){
$value && $self->wrap();
});
$self->getFormatFromArray($formats, self::PATH_ALIGN, None::create())
->forAll(function($value) use ($self){
$self->align($value['horizontal']??null, $value['vertical']??null);
});
$self->getFormatFromArray($formats, self::PATH_BORDER, None::create())
->forAll(function($value) use ($self){
$self->border($value);
});
$self->getFormatFromArray($formats, self::PATH_NUMBER, None::create())
->forAll(function($value) use ($self){
$self->number($value);
});
$self->getFormatFromArray($formats, self::PATH_WIDTH, None::create())
->forAll(function($value) use ($self){
$self->width($value);
});
$self->getFormatFromArray($formats, self::PATH_HEIGHT, None::create())
->forAll(function($value) use ($self){
$self->height($value);
});
return $self;
}
private function assetOption($option, $choices, $hint)
{
//nullable option is allowed
if ($option === null) {
return;
}
$this->throwIfInvalidOption($option, $choices, $hint);
}
private function throwIfInvalidOption($option, array $choices, string $hint)
{
$option = strtolower($option);
if (!in_array($option, $choices)) {
throw new \InvalidArgumentException(
sprintf('Invalid %s: %s, valid choices are: %s', $hint, $option, join(', ', $choices))
);
}
}
private function getFormatFromArray(array $formats, string $path, $default = null): Option
{
$data = data_get($formats, $path, $default);
return $data instanceof None ? $data : Some::create($data);
}
private $formattedStyle;
public function getFormattedStyle()
{
return $this->formattedStyle;
}
public function setFormattedStyle($formattedStyle): self
{
$this->formattedStyle = $formattedStyle;
return $this;
}
}