1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Box\Spout\Writer; |
4
|
|
|
|
5
|
|
|
use Box\Spout\Common\Creator\HelperFactory; |
6
|
|
|
use Box\Spout\Common\Entity\Row; |
7
|
|
|
use Box\Spout\Common\Helper\GlobalFunctionsHelper; |
8
|
|
|
use Box\Spout\Common\Manager\OptionsManagerInterface; |
9
|
|
|
use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface; |
10
|
|
|
use Box\Spout\Writer\Common\Entity\Options; |
11
|
|
|
use Box\Spout\Writer\Common\Entity\Sheet; |
12
|
|
|
use Box\Spout\Writer\Common\Entity\Worksheet; |
13
|
|
|
use Box\Spout\Writer\Common\Manager\WorkbookManagerInterface; |
14
|
|
|
use Box\Spout\Writer\Exception\SheetNotFoundException; |
15
|
|
|
use Box\Spout\Writer\Exception\WriterAlreadyOpenedException; |
16
|
|
|
use Box\Spout\Writer\Exception\WriterNotOpenedException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class WriterMultiSheetsAbstract |
20
|
|
|
* |
21
|
|
|
* @abstract |
22
|
|
|
*/ |
23
|
|
|
abstract class WriterMultiSheetsAbstract extends WriterAbstract |
24
|
|
|
{ |
25
|
|
|
/** @var ManagerFactoryInterface */ |
26
|
|
|
private $managerFactory; |
27
|
|
|
|
28
|
|
|
/** @var WorkbookManagerInterface */ |
29
|
|
|
private $workbookManager; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param OptionsManagerInterface $optionsManager |
33
|
|
|
* @param GlobalFunctionsHelper $globalFunctionsHelper |
34
|
|
|
* @param HelperFactory $helperFactory |
35
|
|
|
* @param ManagerFactoryInterface $managerFactory |
36
|
|
|
*/ |
37
|
87 |
|
public function __construct( |
38
|
|
|
OptionsManagerInterface $optionsManager, |
39
|
|
|
GlobalFunctionsHelper $globalFunctionsHelper, |
40
|
|
|
HelperFactory $helperFactory, |
41
|
|
|
ManagerFactoryInterface $managerFactory |
42
|
|
|
) { |
43
|
87 |
|
parent::__construct($optionsManager, $globalFunctionsHelper, $helperFactory); |
44
|
87 |
|
$this->managerFactory = $managerFactory; |
45
|
87 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Sets whether new sheets should be automatically created when the max rows limit per sheet is reached. |
49
|
|
|
* This must be set before opening the writer. |
50
|
|
|
* |
51
|
|
|
* @param bool $shouldCreateNewSheetsAutomatically Whether new sheets should be automatically created when the max rows limit per sheet is reached |
52
|
|
|
* @throws WriterAlreadyOpenedException If the writer was already opened |
53
|
|
|
* @return WriterMultiSheetsAbstract |
54
|
|
|
*/ |
55
|
31 |
|
public function setShouldCreateNewSheetsAutomatically($shouldCreateNewSheetsAutomatically) |
56
|
|
|
{ |
57
|
31 |
|
$this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); |
58
|
|
|
|
59
|
29 |
|
$this->optionsManager->setOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, $shouldCreateNewSheetsAutomatically); |
60
|
|
|
|
61
|
29 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
77 |
|
protected function openWriter() |
68
|
|
|
{ |
69
|
77 |
|
if (!$this->workbookManager) { |
70
|
77 |
|
$this->workbookManager = $this->managerFactory->createWorkbookManager($this->optionsManager); |
71
|
77 |
|
$this->workbookManager->addNewSheetAndMakeItCurrent(); |
72
|
|
|
} |
73
|
77 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns all the workbook's sheets |
77
|
|
|
* |
78
|
|
|
* @throws WriterNotOpenedException If the writer has not been opened yet |
79
|
|
|
* @return Sheet[] All the workbook's sheets |
80
|
|
|
*/ |
81
|
14 |
|
public function getSheets() |
82
|
|
|
{ |
83
|
14 |
|
$this->throwIfWorkbookIsNotAvailable(); |
84
|
|
|
|
85
|
14 |
|
$externalSheets = []; |
86
|
14 |
|
$worksheets = $this->workbookManager->getWorksheets(); |
87
|
|
|
|
88
|
|
|
/** @var Worksheet $worksheet */ |
89
|
14 |
|
foreach ($worksheets as $worksheet) { |
90
|
14 |
|
$externalSheets[] = $worksheet->getExternalSheet(); |
91
|
|
|
} |
92
|
|
|
|
93
|
14 |
|
return $externalSheets; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Creates a new sheet and make it the current sheet. The data will now be written to this sheet. |
98
|
|
|
* |
99
|
|
|
* @throws WriterNotOpenedException If the writer has not been opened yet |
100
|
|
|
* @return Sheet The created sheet |
101
|
|
|
*/ |
102
|
15 |
|
public function addNewSheetAndMakeItCurrent() |
103
|
|
|
{ |
104
|
15 |
|
$this->throwIfWorkbookIsNotAvailable(); |
105
|
15 |
|
$worksheet = $this->workbookManager->addNewSheetAndMakeItCurrent(); |
106
|
|
|
|
107
|
15 |
|
return $worksheet->getExternalSheet(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns the current sheet |
112
|
|
|
* |
113
|
|
|
* @throws WriterNotOpenedException If the writer has not been opened yet |
114
|
|
|
* @return Sheet The current sheet |
115
|
|
|
*/ |
116
|
10 |
|
public function getCurrentSheet() |
117
|
|
|
{ |
118
|
10 |
|
$this->throwIfWorkbookIsNotAvailable(); |
119
|
|
|
|
120
|
10 |
|
return $this->workbookManager->getCurrentWorksheet()->getExternalSheet(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Sets the given sheet as the current one. New data will be written to this sheet. |
125
|
|
|
* The writing will resume where it stopped (i.e. data won't be truncated). |
126
|
|
|
* |
127
|
|
|
* @param Sheet $sheet The sheet to set as current |
128
|
|
|
* @throws WriterNotOpenedException If the writer has not been opened yet |
129
|
|
|
* @throws SheetNotFoundException If the given sheet does not exist in the workbook |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
4 |
|
public function setCurrentSheet($sheet) |
133
|
|
|
{ |
134
|
4 |
|
$this->throwIfWorkbookIsNotAvailable(); |
135
|
4 |
|
$this->workbookManager->setCurrentSheet($sheet); |
136
|
4 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Checks if the workbook has been created. Throws an exception if not created yet. |
140
|
|
|
* |
141
|
|
|
* @throws WriterNotOpenedException If the workbook is not created yet |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
69 |
|
protected function throwIfWorkbookIsNotAvailable() |
145
|
|
|
{ |
146
|
69 |
|
if (!$this->workbookManager->getWorkbook()) { |
147
|
|
|
throw new WriterNotOpenedException('The writer must be opened before performing this action.'); |
148
|
|
|
} |
149
|
69 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
63 |
|
protected function addRowToWriter(Row $row) |
155
|
|
|
{ |
156
|
63 |
|
$this->throwIfWorkbookIsNotAvailable(); |
157
|
63 |
|
$this->workbookManager->addRowToCurrentWorksheet($row); |
158
|
61 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
*/ |
163
|
70 |
|
protected function closeWriter() |
164
|
|
|
{ |
165
|
70 |
|
if ($this->workbookManager) { |
166
|
70 |
|
$this->workbookManager->close($this->filePointer); |
167
|
|
|
} |
168
|
70 |
|
} |
169
|
|
|
} |
170
|
|
|
|