|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class Style. |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Zhaqq\Xlsx\Writer; |
|
8
|
|
|
|
|
9
|
|
|
use Zhaqq\Xlsx\Support; |
|
10
|
|
|
|
|
11
|
|
|
trait Style |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* 风格设置. |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $cellStyles = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $numberFormats = []; |
|
24
|
|
|
|
|
25
|
|
|
protected $borderAllowed = [ |
|
26
|
|
|
'left', 'right', 'top', 'bottom', |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
protected $borderStyleAllowed = [ |
|
30
|
|
|
'thin', |
|
31
|
|
|
'medium', |
|
32
|
|
|
'thick', |
|
33
|
|
|
'dashDot', |
|
34
|
|
|
'dashDotDot', |
|
35
|
|
|
'dashed', |
|
36
|
|
|
'dotted', |
|
37
|
|
|
'double', |
|
38
|
|
|
'hair', |
|
39
|
|
|
'mediumDashDot', |
|
40
|
|
|
'mediumDashDotDot', |
|
41
|
|
|
'mediumDashed', |
|
42
|
|
|
'slantDashDot', |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
protected $horizontalAllowed = ['general', 'left', 'right', 'justify', 'center']; |
|
46
|
|
|
|
|
47
|
|
|
protected $verticalAllowed = ['bottom', 'center', 'distributed', 'top']; |
|
48
|
|
|
|
|
49
|
|
|
protected $defaultFont = ['size' => '10', 'name' => 'Arial', 'family' => '2']; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param $numberFormat |
|
53
|
|
|
* @param $cellStyleString |
|
54
|
|
|
* |
|
55
|
|
|
* @return false|int|string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function addCellStyle($numberFormat, $cellStyleString) |
|
58
|
|
|
{ |
|
59
|
|
|
$numberFormatIdx = Support::add2listGetIndex($this->numberFormats, $numberFormat); |
|
60
|
|
|
$lookupString = $numberFormatIdx.';'.$cellStyleString; |
|
61
|
|
|
|
|
62
|
|
|
return Support::add2listGetIndex($this->cellStyles, $lookupString); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function styleFontIndexes(array $cellStyles) |
|
66
|
|
|
{ |
|
67
|
|
|
$fills = ['', '']; //2 placeholders for static xml later |
|
68
|
|
|
$fonts = ['', '', '', '']; //4 placeholders for static xml later |
|
69
|
|
|
$borders = ['']; //1 placeholder for static xml later |
|
70
|
|
|
$styleIndexes = []; |
|
71
|
|
|
foreach ($cellStyles as $i => $cellStyleString) { |
|
72
|
|
|
$semiColonPos = strpos($cellStyleString, ';'); |
|
73
|
|
|
$numberFormatIdx = substr($cellStyleString, 0, $semiColonPos); |
|
74
|
|
|
$styleJsonString = substr($cellStyleString, $semiColonPos + 1); |
|
75
|
|
|
$style = @json_decode($styleJsonString, true); |
|
76
|
|
|
$styleIndexes[$i] = ['num_fmt_idx' => $numberFormatIdx]; //initialize entry |
|
77
|
|
|
if (isset($style['border']) && is_string($style['border'])) { //border is a comma delimited str |
|
78
|
|
|
$borderValue['side'] = array_intersect(explode(',', $style['border']), $this->borderAllowed); |
|
79
|
|
|
if (isset($style['border-style']) && in_array($style['border-style'], $this->borderStyleAllowed)) { |
|
80
|
|
|
$borderValue['style'] = $style['border-style']; |
|
81
|
|
|
} |
|
82
|
|
|
if (isset($style['border-color']) && is_string($style['border-color']) && '#' == $style['border-color'][0]) { |
|
83
|
|
|
$v = substr($style['border-color'], 1, 6); |
|
84
|
|
|
$v = 3 == strlen($v) ? $v[0].$v[0].$v[1].$v[1].$v[2].$v[2] : $v; // expand cf0 => ccff00 |
|
85
|
|
|
$border_value['color'] = 'FF'.strtoupper($v); |
|
86
|
|
|
} |
|
87
|
|
|
$styleIndexes[$i]['border_idx'] = Support::add2listGetIndex($borders, json_encode($borderValue)); |
|
88
|
|
|
} |
|
89
|
|
|
if (isset($style['fill']) && is_string($style['fill']) && '#' == $style['fill'][0]) { |
|
90
|
|
|
$v = substr($style['fill'], 1, 6); |
|
91
|
|
|
$v = 3 == strlen($v) ? $v[0].$v[0].$v[1].$v[1].$v[2].$v[2] : $v; // expand cf0 => ccff00 |
|
92
|
|
|
$styleIndexes[$i]['fill_idx'] = Support::add2listGetIndex($fills, 'FF'.strtoupper($v)); |
|
93
|
|
|
} |
|
94
|
|
|
if (isset($style['halign']) && in_array($style['halign'], $this->horizontalAllowed)) { |
|
95
|
|
|
$styleIndexes[$i]['alignment'] = true; |
|
96
|
|
|
$styleIndexes[$i]['halign'] = $style['halign']; |
|
97
|
|
|
} |
|
98
|
|
|
if (isset($style['valign']) && in_array($style['valign'], $this->verticalAllowed)) { |
|
99
|
|
|
$styleIndexes[$i]['alignment'] = true; |
|
100
|
|
|
$styleIndexes[$i]['valign'] = $style['valign']; |
|
101
|
|
|
} |
|
102
|
|
|
if (isset($style['wrap_text'])) { |
|
103
|
|
|
$styleIndexes[$i]['alignment'] = true; |
|
104
|
|
|
$styleIndexes[$i]['wrap_text'] = (bool) $style['wrap_text']; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$font = $this->defaultFont; |
|
108
|
|
|
if (isset($style['font-size'])) { |
|
109
|
|
|
$font['size'] = floatval($style['font-size']); //floatval to allow "10.5" etc |
|
110
|
|
|
} |
|
111
|
|
|
if (isset($style['font']) && is_string($style['font'])) { |
|
112
|
|
|
if ('Comic Sans MS' == $style['font']) { |
|
113
|
|
|
$font['family'] = 4; |
|
114
|
|
|
} |
|
115
|
|
|
if ('Times New Roman' == $style['font']) { |
|
116
|
|
|
$font['family'] = 1; |
|
117
|
|
|
} |
|
118
|
|
|
if ('Courier New' == $style['font']) { |
|
119
|
|
|
$font['family'] = 3; |
|
120
|
|
|
} |
|
121
|
|
|
$font['name'] = strval($style['font']); |
|
122
|
|
|
} |
|
123
|
|
|
if (isset($style['font-style']) && is_string($style['font-style'])) { |
|
124
|
|
|
if (false !== strpos($style['font-style'], 'bold')) { |
|
125
|
|
|
$font['bold'] = true; |
|
126
|
|
|
} |
|
127
|
|
|
if (false !== strpos($style['font-style'], 'italic')) { |
|
128
|
|
|
$font['italic'] = true; |
|
129
|
|
|
} |
|
130
|
|
|
if (false !== strpos($style['font-style'], 'strike')) { |
|
131
|
|
|
$font['strike'] = true; |
|
132
|
|
|
} |
|
133
|
|
|
if (false !== strpos($style['font-style'], 'underline')) { |
|
134
|
|
|
$font['underline'] = true; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
if (isset($style['color']) && is_string($style['color']) && '#' == $style['color'][0]) { |
|
138
|
|
|
$v = substr($style['color'], 1, 6); |
|
139
|
|
|
$v = 3 == strlen($v) ? $v[0].$v[0].$v[1].$v[1].$v[2].$v[2] : $v; // expand cf0 => ccff00 |
|
140
|
|
|
$font['color'] = 'FF'.strtoupper($v); |
|
141
|
|
|
} |
|
142
|
|
|
if ($font != $this->defaultFont) { |
|
143
|
|
|
$styleIndexes[$i]['font_idx'] = Support::add2listGetIndex($fonts, json_encode($font)); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return [ |
|
148
|
|
|
'fills' => $fills, |
|
149
|
|
|
'fonts' => $fonts, |
|
150
|
|
|
'borders' => $borders, |
|
151
|
|
|
'styles' => $styleIndexes, |
|
152
|
|
|
]; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|