1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OneSheet; |
4
|
|
|
|
5
|
|
|
use OneSheet\Xml\WorkbookXml; |
6
|
|
|
|
7
|
|
|
class Workbook |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var int |
11
|
|
|
*/ |
12
|
|
|
private $printTitleStart; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var int |
16
|
|
|
*/ |
17
|
|
|
private $printTitleEnd; |
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
|
|
|
*/ |
26
|
1 |
|
public function setPrintTitleRange($startRow, $endRow) |
27
|
|
|
{ |
28
|
1 |
|
if ($startRow <= $endRow && is_numeric($startRow) && is_numeric($endRow)) { |
29
|
1 |
|
$this->printTitleStart = (int)$startRow; |
30
|
1 |
|
$this->printTitleEnd = (int)$endRow; |
31
|
|
|
} |
32
|
1 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string[] $sheetIds |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
6 |
|
public function getWorkbookXml(array $sheetIds) |
39
|
|
|
{ |
40
|
6 |
|
$sheets = $this->getSheetsXml($sheetIds); |
41
|
6 |
|
$definedNames = $this->getDefinedNamesXml($sheetIds); |
42
|
|
|
|
43
|
6 |
|
return sprintf(WorkbookXml::WORKBOOK_XML, $sheets, $definedNames); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string[] $sheetIds |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
4 |
View Code Duplication |
public function getWorkbookRelsXml($sheetIds) |
|
|
|
|
51
|
|
|
{ |
52
|
4 |
|
$sheets = ''; |
53
|
4 |
|
foreach ($sheetIds as $key => $sheetId) { |
54
|
4 |
|
$sheets .= sprintf(WorkbookXml::WORKBOOK_REL_XML, $key + 1, $sheetId); |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
return sprintf(WorkbookXml::WORKBOOK_RELS_XML, $sheets); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string[] $sheetIds |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
6 |
View Code Duplication |
private function getSheetsXml($sheetIds) |
|
|
|
|
65
|
|
|
{ |
66
|
6 |
|
$sheets = ''; |
67
|
6 |
|
foreach ($sheetIds as $key => $sheetId) { |
68
|
6 |
|
$sheets .= sprintf(WorkbookXml::WORKBOOK_SHEETS_XML, $sheetId, $key + 1, $key + 1); |
69
|
|
|
} |
70
|
|
|
|
71
|
6 |
|
return $sheets; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string[] $sheetIds |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
6 |
|
private function getDefinedNamesXml(array $sheetIds) |
79
|
|
|
{ |
80
|
6 |
|
$definedNames = ''; |
81
|
6 |
|
if ($this->printTitleStart && $this->printTitleEnd) { |
82
|
1 |
|
foreach ($sheetIds as $sheetId) { |
83
|
1 |
|
$definedNames = sprintf( |
84
|
1 |
|
WorkbookXml::DEFINED_NAMES_XML, |
85
|
|
|
$sheetId, |
86
|
1 |
|
$this->printTitleStart, |
87
|
1 |
|
$this->printTitleEnd |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
6 |
|
return $definedNames; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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.