|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Box\Spout\Writer\XLSX\Manager\Style; |
|
4
|
|
|
|
|
5
|
|
|
use Box\Spout\Writer\Common\Entity\Style\Style; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class StyleRegistry |
|
9
|
|
|
* Registry for all used styles |
|
10
|
|
|
* |
|
11
|
|
|
* @package Box\Spout\Writer\XLSX\Manager\Style |
|
12
|
|
|
*/ |
|
13
|
|
|
class StyleRegistry extends \Box\Spout\Writer\Common\Manager\Style\StyleRegistry |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $registeredFills = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array [STYLE_ID] => [FILL_ID] maps a style to a fill declaration |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $styleIdToFillMappingTable = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Excel preserves two default fills with index 0 and 1 |
|
27
|
|
|
* Since Excel is the dominant vendor - we play along here |
|
28
|
|
|
* |
|
29
|
|
|
* @var int The fill index counter for custom fills. |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $fillIndex = 2; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $registeredBorders = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array [STYLE_ID] => [BORDER_ID] maps a style to a border declaration |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $styleIdToBorderMappingTable = []; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* XLSX specific operations on the registered styles |
|
45
|
|
|
* |
|
46
|
|
|
* @param Style $style |
|
47
|
|
|
* @return Style |
|
48
|
|
|
*/ |
|
49
|
48 |
|
public function registerStyle(Style $style) |
|
50
|
|
|
{ |
|
51
|
48 |
|
$registeredStyle = parent::registerStyle($style); |
|
52
|
48 |
|
$this->registerFill($registeredStyle); |
|
53
|
48 |
|
$this->registerBorder($registeredStyle); |
|
54
|
48 |
|
return $registeredStyle; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Register a fill definition |
|
59
|
|
|
* |
|
60
|
|
|
* @param Style $style |
|
61
|
|
|
*/ |
|
62
|
48 |
|
private function registerFill(Style $style) |
|
63
|
|
|
{ |
|
64
|
48 |
|
$styleId = $style->getId(); |
|
65
|
|
|
|
|
66
|
|
|
// Currently - only solid backgrounds are supported |
|
67
|
|
|
// so $backgroundColor is a scalar value (RGB Color) |
|
68
|
48 |
|
$backgroundColor = $style->getBackgroundColor(); |
|
69
|
|
|
|
|
70
|
48 |
|
if ($backgroundColor) { |
|
71
|
4 |
|
$isBackgroundColorRegistered = isset($this->registeredFills[$backgroundColor]); |
|
72
|
|
|
|
|
73
|
|
|
// We need to track the already registered background definitions |
|
74
|
4 |
|
if ($isBackgroundColorRegistered) { |
|
75
|
2 |
|
$registeredStyleId = $this->registeredFills[$backgroundColor]; |
|
76
|
2 |
|
$registeredFillId = $this->styleIdToFillMappingTable[$registeredStyleId]; |
|
77
|
2 |
|
$this->styleIdToFillMappingTable[$styleId] = $registeredFillId; |
|
78
|
|
|
} else { |
|
79
|
4 |
|
$this->registeredFills[$backgroundColor] = $styleId; |
|
80
|
4 |
|
$this->styleIdToFillMappingTable[$styleId] = $this->fillIndex++; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
} else { |
|
84
|
|
|
// The fillId maps a style to a fill declaration |
|
85
|
|
|
// When there is no background color definition - we default to 0 |
|
86
|
48 |
|
$this->styleIdToFillMappingTable[$styleId] = 0; |
|
87
|
|
|
} |
|
88
|
48 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param int $styleId |
|
92
|
|
|
* @return int|null Fill ID associated to the given style ID |
|
93
|
|
|
*/ |
|
94
|
37 |
|
public function getFillIdForStyleId($styleId) |
|
95
|
|
|
{ |
|
96
|
37 |
|
return (isset($this->styleIdToFillMappingTable[$styleId])) ? |
|
97
|
37 |
|
$this->styleIdToFillMappingTable[$styleId] : |
|
98
|
37 |
|
null; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Register a border definition |
|
103
|
|
|
* |
|
104
|
|
|
* @param Style $style |
|
105
|
|
|
*/ |
|
106
|
48 |
|
private function registerBorder(Style $style) |
|
107
|
|
|
{ |
|
108
|
48 |
|
$styleId = $style->getId(); |
|
109
|
|
|
|
|
110
|
48 |
|
if ($style->shouldApplyBorder()) { |
|
111
|
5 |
|
$border = $style->getBorder(); |
|
112
|
5 |
|
$serializedBorder = serialize($border); |
|
113
|
|
|
|
|
114
|
5 |
|
$isBorderAlreadyRegistered = isset($this->registeredBorders[$serializedBorder]); |
|
115
|
|
|
|
|
116
|
5 |
|
if ($isBorderAlreadyRegistered) { |
|
117
|
2 |
|
$registeredStyleId = $this->registeredBorders[$serializedBorder]; |
|
118
|
2 |
|
$registeredBorderId = $this->styleIdToBorderMappingTable[$registeredStyleId]; |
|
119
|
2 |
|
$this->styleIdToBorderMappingTable[$styleId] = $registeredBorderId; |
|
120
|
|
|
} else { |
|
121
|
5 |
|
$this->registeredBorders[$serializedBorder] = $styleId; |
|
122
|
5 |
|
$this->styleIdToBorderMappingTable[$styleId] = count($this->registeredBorders); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
} else { |
|
126
|
|
|
// If no border should be applied - the mapping is the default border: 0 |
|
127
|
48 |
|
$this->styleIdToBorderMappingTable[$styleId] = 0; |
|
128
|
|
|
} |
|
129
|
48 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param int $styleId |
|
133
|
|
|
* @return int|null Fill ID associated to the given style ID |
|
134
|
|
|
*/ |
|
135
|
37 |
|
public function getBorderIdForStyleId($styleId) |
|
136
|
|
|
{ |
|
137
|
37 |
|
return (isset($this->styleIdToBorderMappingTable[$styleId])) ? |
|
138
|
37 |
|
$this->styleIdToBorderMappingTable[$styleId] : |
|
139
|
37 |
|
null; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return array |
|
144
|
|
|
*/ |
|
145
|
37 |
|
public function getRegisteredFills() |
|
146
|
|
|
{ |
|
147
|
37 |
|
return $this->registeredFills; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return array |
|
152
|
|
|
*/ |
|
153
|
37 |
|
public function getRegisteredBorders() |
|
154
|
|
|
{ |
|
155
|
37 |
|
return $this->registeredBorders; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|