1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OneSheet; |
4
|
|
|
|
5
|
|
|
use OneSheet\Xml\RowXml; |
6
|
|
|
use OneSheet\Style\Style; |
7
|
|
|
use OneSheet\Width\WidthCalculator; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Sheet |
11
|
|
|
* |
12
|
|
|
* @package OneSheet |
13
|
|
|
*/ |
14
|
|
|
class Sheet |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var CellBuilder |
18
|
|
|
*/ |
19
|
|
|
private $cellBuilder; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var WidthCalculator |
23
|
|
|
*/ |
24
|
|
|
private $widthCalculator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
private $useCellAutosizing = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Track next row index. |
33
|
|
|
* |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
private $rowIndex = 1; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Holds width/column count of the widest row. |
40
|
|
|
* |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
private $maxRowWidth; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Holds widths of the widest cells for column sizing. |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
private $columnWidths; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Sheet constructor. |
54
|
|
|
*/ |
55
|
5 |
|
public function __construct() |
56
|
|
|
{ |
57
|
5 |
|
$this->cellBuilder = new CellBuilder(); |
58
|
5 |
|
$this->widthCalculator = new WidthCalculator(); |
59
|
5 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Enable cell autosizing (~30% performance hit!). |
63
|
|
|
*/ |
64
|
3 |
|
public function enableCellAutosizing() |
65
|
|
|
{ |
66
|
3 |
|
$this->useCellAutosizing = true; |
67
|
3 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Disable cell autosizing (default). |
71
|
|
|
*/ |
72
|
1 |
|
public function disableCellAutosizing() |
73
|
|
|
{ |
74
|
1 |
|
$this->useCellAutosizing = false; |
75
|
1 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
2 |
|
public function isCellAutosizingEnabled() |
81
|
|
|
{ |
82
|
2 |
|
return $this->useCellAutosizing; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return array containing all column widths. |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
1 |
|
public function getColumnWidths() |
91
|
|
|
{ |
92
|
1 |
|
return $this->columnWidths; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Return cellId for dimensions. |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
1 |
|
public function getDimensionUpperBound() |
101
|
|
|
{ |
102
|
1 |
|
return $this->cellBuilder->getCellId($this->maxRowWidth, $this->rowIndex - 1); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Add single row and style to sheet. |
107
|
|
|
* |
108
|
|
|
* @param array $row |
109
|
|
|
* @param Style $style |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
2 |
|
public function addRow(array $row, Style $style) |
114
|
|
|
{ |
115
|
2 |
|
$rowWidth = count($row); |
116
|
2 |
|
$this->updateMaxRowWidth($rowWidth); |
117
|
|
|
|
118
|
2 |
|
$this->widthCalculator->setFont($style->getFont()); |
119
|
2 |
|
$cellXml = $this->getCellXml($row, $style); |
120
|
|
|
|
121
|
2 |
|
if (!$this->useCellAutosizing || $style->getFont()->getSize() < 14) { |
122
|
2 |
|
return sprintf(RowXml::DEFAULT_XML, $this->rowIndex++, $rowWidth, $cellXml); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return sprintf(RowXml::HEIGHT_XML, $this->rowIndex++, $rowWidth, |
126
|
|
|
$style->getFont()->getSize() * 1.4, $cellXml); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Track widest row for dimensions xml (e.g. A1:K123). |
131
|
|
|
* |
132
|
|
|
* @param int $rowWidth |
133
|
|
|
*/ |
134
|
2 |
|
private function updateMaxRowWidth($rowWidth) |
135
|
|
|
{ |
136
|
2 |
|
if ($this->maxRowWidth < $rowWidth) { |
137
|
2 |
|
$this->maxRowWidth = $rowWidth; |
138
|
2 |
|
} |
139
|
2 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get xml string for single cell and update cell widths. |
143
|
|
|
* |
144
|
|
|
* @param array $row |
145
|
|
|
* @param Style $style |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
2 |
|
private function getCellXml(array $row, Style $style) |
149
|
|
|
{ |
150
|
2 |
|
$cellXml = ''; |
151
|
2 |
|
foreach (array_values($row) as $cellIndex => $cellValue) { |
152
|
2 |
|
if (0 == strlen($cellValue)) continue; |
153
|
2 |
|
if ($this->useCellAutosizing) { |
154
|
2 |
|
$this->updateColumnWidths($cellValue, $cellIndex, $style); |
155
|
2 |
|
} |
156
|
2 |
|
$cellXml .= $this->cellBuilder->build($this->rowIndex, $cellIndex, $cellValue, |
157
|
2 |
|
$style->getId()); |
158
|
2 |
|
} |
159
|
|
|
|
160
|
2 |
|
return $cellXml; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Track cell width for column width sizing. |
165
|
|
|
* |
166
|
|
|
* @param mixed $value |
167
|
|
|
* @param int $cellIndex |
168
|
|
|
* @param Style $style |
169
|
|
|
*/ |
170
|
2 |
|
private function updateColumnWidths($value, $cellIndex, Style $style) |
171
|
|
|
{ |
172
|
2 |
|
$cellWidth = $this->widthCalculator->getCellWidth($value, $style->getFont()->isBold()); |
173
|
|
|
|
174
|
2 |
|
if (!isset($this->columnWidths[$cellIndex]) |
175
|
2 |
|
|| $this->columnWidths[$cellIndex] < $cellWidth |
176
|
2 |
|
) { |
177
|
2 |
|
$this->columnWidths[$cellIndex] = $cellWidth; |
178
|
2 |
|
} |
179
|
2 |
|
} |
180
|
|
|
} |
181
|
|
|
|