1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Eclipxe\XlsxExporter; |
6
|
|
|
|
7
|
|
|
use Eclipxe\XlsxExporter\Exceptions\InvalidPropertyNameException; |
8
|
|
|
use Eclipxe\XlsxExporter\Exceptions\InvalidWorkSheetNameException; |
9
|
|
|
use Eclipxe\XlsxExporter\Providers\NullProvider; |
10
|
|
|
use Eclipxe\XlsxExporter\Utils\TemporaryFile; |
11
|
|
|
use EngineWorks\ProgressStatus\NullProgress; |
12
|
|
|
use EngineWorks\ProgressStatus\ProgressInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* WorkSheet class |
16
|
|
|
* |
17
|
|
|
* @property ProviderInterface $provider Provider object |
18
|
|
|
* @property Columns|Column[] $columns Columns object |
19
|
|
|
* @property string $name Name of the worksheet |
20
|
|
|
* @property Style $headerStyle Style of the header columns |
21
|
|
|
*/ |
22
|
|
|
class WorkSheet |
23
|
|
|
{ |
24
|
|
|
/** @var string The name of the worksheet **/ |
25
|
|
|
protected string $name; |
26
|
|
|
|
27
|
|
|
/** @var Columns|Column[] Columns collection */ |
28
|
|
|
protected $columns; |
29
|
|
|
|
30
|
|
|
protected ProviderInterface $provider; |
31
|
|
|
|
32
|
|
|
protected Style $headerStyle; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* WorkSheet constructor. |
36
|
|
|
* |
37
|
|
|
* @param string $name |
38
|
|
|
* @param ProviderInterface|null $provider |
39
|
|
|
* @param Columns|null $columns |
40
|
|
|
* @param Style|null $headerStyle |
41
|
|
|
*/ |
42
|
20 |
|
public function __construct( |
43
|
|
|
string $name, |
44
|
|
|
ProviderInterface $provider = null, |
45
|
|
|
Columns $columns = null, |
46
|
|
|
Style $headerStyle = null |
47
|
|
|
) { |
48
|
20 |
|
$this->setName($name); |
49
|
20 |
|
$this->setProvider($provider ?? new NullProvider()); |
50
|
20 |
|
$this->setColumns($columns ?? new Columns()); |
51
|
20 |
|
$this->setHeaderStyle($headerStyle ?? BasicStyles::defaultHeader()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Magic method, this allows to access methods as getters |
56
|
|
|
* |
57
|
|
|
* @param string $name |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
1 |
|
public function __get(string $name) |
61
|
|
|
{ |
62
|
1 |
|
$props = ['name', 'provider', 'columns', 'headerStyle']; |
63
|
1 |
|
if (! in_array($name, $props)) { |
64
|
|
|
throw new InvalidPropertyNameException($name); |
65
|
|
|
} |
66
|
1 |
|
$method = 'get' . ucfirst($name); |
67
|
1 |
|
return $this->$method(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string Worksheet name |
72
|
|
|
*/ |
73
|
6 |
|
public function getName(): string |
74
|
|
|
{ |
75
|
6 |
|
return $this->name; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set the name of the sheet, be aware that the name has several constraints: |
80
|
|
|
* - Must be a string |
81
|
|
|
* - Cannot contains: : \ / ? * [ ] ' tab nl cr null |
82
|
|
|
* - Cannot be more than 31 chars |
83
|
|
|
* - Cannot be an empty string |
84
|
|
|
* |
85
|
|
|
* @param string $name |
86
|
|
|
*/ |
87
|
20 |
|
public function setName(string $name): void |
88
|
|
|
{ |
89
|
20 |
|
if ('' === $name) { |
90
|
1 |
|
throw new InvalidWorkSheetNameException($name, 'the name is empty'); |
91
|
|
|
} |
92
|
20 |
|
if (preg_match('/[\'\/\\\\:?*\[\]\n\r\t\0]/', $name)) { |
93
|
12 |
|
throw new InvalidWorkSheetNameException($name, 'the name contains invalid chars'); |
94
|
|
|
} |
95
|
20 |
|
if (strlen($name) > 31) { |
96
|
1 |
|
throw new InvalidWorkSheetNameException($name, 'the name length is more than 31 chars length'); |
97
|
|
|
} |
98
|
20 |
|
$this->name = $name; |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
public function getColumns(): Columns |
102
|
|
|
{ |
103
|
2 |
|
return $this->columns; |
104
|
|
|
} |
105
|
|
|
|
106
|
20 |
|
public function setColumns(Columns $columns): void |
107
|
|
|
{ |
108
|
20 |
|
$this->columns = $columns; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Retrieve the header style |
113
|
|
|
*/ |
114
|
2 |
|
public function getHeaderStyle(): Style |
115
|
|
|
{ |
116
|
2 |
|
return $this->headerStyle; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set the header style |
121
|
|
|
* @param Style $headerStyle |
122
|
|
|
*/ |
123
|
20 |
|
public function setHeaderStyle(Style $headerStyle): void |
124
|
|
|
{ |
125
|
20 |
|
$this->headerStyle = $headerStyle; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Set the provider object |
130
|
|
|
* |
131
|
|
|
* @param ProviderInterface $provider |
132
|
|
|
*/ |
133
|
20 |
|
public function setProvider(ProviderInterface $provider): void |
134
|
|
|
{ |
135
|
20 |
|
$this->provider = $provider; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Retrieve the provider object, null if the object has not been set |
140
|
|
|
*/ |
141
|
|
|
public function getProvider(): ProviderInterface |
142
|
|
|
{ |
143
|
|
|
return $this->provider; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Write the contents of the worksheet, it requires a SharedStrings object |
148
|
|
|
*/ |
149
|
2 |
|
public function write(TemporaryFile $file, SharedStrings $strings, ProgressInterface $progress = null): void |
150
|
|
|
{ |
151
|
2 |
|
$writer = new WorkSheetWriter(); |
152
|
2 |
|
$rowsCount = $this->provider->count(); |
153
|
2 |
|
$progress = $progress ?: new NullProgress(); |
154
|
2 |
|
$progress->update('', 0, $rowsCount + 1); |
155
|
2 |
|
$writer->createSheet($file->getPath(), $this->columns->count(), $rowsCount); |
156
|
2 |
|
$writer->openSheet(); |
157
|
|
|
// -- write headers contents |
158
|
2 |
|
$writer->openRow(); |
159
|
2 |
|
$styleIndex = $this->getHeaderStyle()->getStyleIndex(); |
160
|
2 |
|
foreach ($this->columns as $column) { |
161
|
|
|
// write cell |
162
|
2 |
|
$writer->writeCell(CellTypes::TEXT, $strings->add($column->getTitle()), $styleIndex); |
163
|
|
|
} |
164
|
2 |
|
$writer->closeRow(); |
165
|
2 |
|
$progress->increase(); |
166
|
|
|
// -- write cell contents |
167
|
2 |
|
while ($this->provider->valid()) { |
168
|
|
|
// write new row |
169
|
2 |
|
$writer->openRow(); |
170
|
2 |
|
foreach ($this->columns as $column) { |
171
|
|
|
// write cell |
172
|
2 |
|
$value = $this->provider->get($column->getId()); |
173
|
2 |
|
$type = $column->getType(); |
174
|
2 |
|
if (CellTypes::TEXT === $type) { |
175
|
2 |
|
$value = $strings->add((string) $value); |
176
|
|
|
} |
177
|
2 |
|
$writer->writeCell($type, $value, $column->style->getStyleIndex()); |
178
|
|
|
} |
179
|
2 |
|
$writer->closeRow(); |
180
|
|
|
// move to the next record |
181
|
2 |
|
$this->provider->next(); |
182
|
2 |
|
$progress->increase(); |
183
|
|
|
} |
184
|
2 |
|
$writer->closeSheet(); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|