1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OneSheet; |
4
|
|
|
|
5
|
|
|
use OneSheet\Xml\WorkbookXml; |
6
|
|
|
|
7
|
|
|
class Workbook |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var int[] |
11
|
|
|
*/ |
12
|
|
|
private $printTitleStarts; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var int[] |
16
|
|
|
*/ |
17
|
|
|
private $printTitleEnds; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Set the range of rows to repeat when printing the excel file. |
21
|
|
|
* $startRow=1 and $endRow=1 will repeat the first row only. |
22
|
|
|
* |
23
|
|
|
* @param int $startRow |
24
|
|
|
* @param int $endRow |
25
|
|
|
* @param string $sheetName |
26
|
|
|
*/ |
27
|
3 |
|
public function setPrintTitleRange($startRow, $endRow, $sheetName) |
28
|
|
|
{ |
29
|
3 |
|
if ($startRow <= $endRow && is_numeric($startRow) && is_numeric($endRow)) { |
30
|
3 |
|
$this->printTitleStarts[$sheetName] = (int)$startRow; |
31
|
3 |
|
$this->printTitleEnds[$sheetName] = (int)$endRow; |
32
|
|
|
} |
33
|
3 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get final/combined XML for workbook.xml file. |
37
|
|
|
* |
38
|
|
|
* @param string[] $sheetNames |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
7 |
|
public function getWorkbookXml(array $sheetNames) |
42
|
|
|
{ |
43
|
7 |
|
$sheets = $this->getWorkbookSheetsXml($sheetNames); |
44
|
7 |
|
$definedNames = $this->getDefinedNamesXml($sheetNames); |
45
|
|
|
|
46
|
7 |
|
return sprintf(WorkbookXml::WORKBOOK_XML, $sheets, $definedNames); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get XML for sheet relations of the workbook. |
51
|
|
|
* |
52
|
|
|
* @param string[] $sheetNames |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
4 |
View Code Duplication |
public function getWorkbookRelsXml($sheetNames) |
|
|
|
|
56
|
|
|
{ |
57
|
4 |
|
$relations = ''; |
58
|
4 |
|
foreach ($sheetNames as $key => $sheetName) { |
59
|
4 |
|
$relations .= sprintf(WorkbookXml::WORKBOOK_REL_XML, $key + 1, $sheetName); |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
return sprintf(WorkbookXml::WORKBOOK_RELS_XML, $relations); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Generate xml with all sheet names that should be linked to the workbook. |
67
|
|
|
* |
68
|
|
|
* @param string[] $sheetNames |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
7 |
View Code Duplication |
private function getWorkbookSheetsXml($sheetNames) |
|
|
|
|
72
|
|
|
{ |
73
|
7 |
|
$sheets = ''; |
74
|
7 |
|
foreach ($sheetNames as $key => $sheetName) { |
75
|
7 |
|
$sheets .= sprintf(WorkbookXml::WORKBOOK_SHEETS_XML, $sheetName, $key + 1, $key + 1); |
76
|
|
|
} |
77
|
|
|
|
78
|
7 |
|
return $sheets; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Generate xml used for repeatable headers when printing of exporting to PDF, |
83
|
|
|
* or empty string if none are set. |
84
|
|
|
* |
85
|
|
|
* @param string[] $sheetNames |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
7 |
|
private function getDefinedNamesXml(array $sheetNames) |
89
|
|
|
{ |
90
|
7 |
|
$definedNames = ''; |
91
|
7 |
|
if ($this->printTitleStarts && $this->printTitleEnds) { |
|
|
|
|
92
|
2 |
|
$definedNameTags = ''; |
93
|
2 |
|
foreach ($sheetNames as $key => $sheetName) { |
94
|
2 |
|
$definedNameTags .= $this->createSingleDefinedNameXml($sheetName, $key); |
95
|
|
|
} |
96
|
2 |
|
$definedNames = sprintf(WorkbookXml::DEFINED_NAMES_XML, $definedNameTags); |
97
|
|
|
} |
98
|
|
|
|
99
|
7 |
|
return $definedNames; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* $localSheetId is the only one that has to start from 0 instead of 1. |
104
|
|
|
* |
105
|
|
|
* @param string $sheetName |
106
|
|
|
* @param int $localSheetId |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
2 |
|
private function createSingleDefinedNameXml($sheetName, $localSheetId) |
110
|
|
|
{ |
111
|
2 |
|
return isset($this->printTitleEnds[$sheetName]) |
112
|
2 |
|
? sprintf( |
113
|
2 |
|
WorkbookXml::DEFINED_NAME_XML, |
114
|
|
|
$localSheetId, |
115
|
|
|
$sheetName, |
116
|
2 |
|
$this->printTitleStarts[$sheetName], |
117
|
2 |
|
$this->printTitleEnds[$sheetName]) |
118
|
2 |
|
: ''; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.