1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MewesK\TwigSpreadsheetBundle\Wrapper; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Exception; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Settings; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\BaseWriter; |
10
|
|
|
use Symfony\Bridge\Twig\AppVariable; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class XlsDocumentWrapper |
14
|
|
|
* |
15
|
|
|
* @package MewesK\TwigSpreadsheetBundle\Wrapper |
16
|
|
|
*/ |
17
|
|
|
class XlsDocumentWrapper extends AbstractWrapper |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $context; |
23
|
|
|
/** |
24
|
|
|
* @var \Twig_Environment |
25
|
|
|
*/ |
26
|
|
|
protected $environment; |
27
|
|
|
/** |
28
|
|
|
* @var Spreadsheet|null |
29
|
|
|
*/ |
30
|
|
|
protected $object; |
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $attributes; |
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $mappings; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* XlsDocumentWrapper constructor. |
42
|
|
|
* |
43
|
|
|
* @param array $context |
44
|
|
|
* @param \Twig_Environment $environment |
45
|
|
|
*/ |
46
|
|
|
public function __construct(array $context, \Twig_Environment $environment) |
47
|
|
|
{ |
48
|
|
|
$this->context = $context; |
49
|
|
|
$this->environment = $environment; |
50
|
|
|
|
51
|
|
|
$this->object = null; |
52
|
|
|
$this->attributes = []; |
53
|
|
|
$this->mappings = []; |
54
|
|
|
|
55
|
|
|
$this->initializeMappings(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function initializeMappings() |
59
|
|
|
{ |
60
|
|
|
$this->mappings['category'] = function ($value) { |
61
|
|
|
$this->object->getProperties()->setCategory($value); |
62
|
|
|
}; |
63
|
|
|
$this->mappings['company'] = function ($value) { |
64
|
|
|
$this->object->getProperties()->setCompany($value); |
65
|
|
|
}; |
66
|
|
|
$this->mappings['created'] = function ($value) { |
67
|
|
|
$this->object->getProperties()->setCreated($value); |
68
|
|
|
}; |
69
|
|
|
$this->mappings['creator'] = function ($value) { |
70
|
|
|
$this->object->getProperties()->setCreator($value); |
71
|
|
|
}; |
72
|
|
|
$this->mappings['defaultStyle'] = function ($value) { |
73
|
|
|
$this->object->getDefaultStyle()->applyFromArray($value); |
74
|
|
|
}; |
75
|
|
|
$this->mappings['description'] = function ($value) { |
76
|
|
|
$this->object->getProperties()->setDescription($value); |
77
|
|
|
}; |
78
|
|
|
$this->mappings['format'] = function ($value) { |
79
|
|
|
$this->attributes['format'] = $value; |
80
|
|
|
}; |
81
|
|
|
$this->mappings['keywords'] = function ($value) { |
82
|
|
|
$this->object->getProperties()->setKeywords($value); |
83
|
|
|
}; |
84
|
|
|
$this->mappings['lastModifiedBy'] = function ($value) { |
85
|
|
|
$this->object->getProperties()->setLastModifiedBy($value); |
86
|
|
|
}; |
87
|
|
|
$this->mappings['manager'] = function ($value) { |
88
|
|
|
$this->object->getProperties()->setManager($value); |
89
|
|
|
}; |
90
|
|
|
$this->mappings['modified'] = function ($value) { |
91
|
|
|
$this->object->getProperties()->setModified($value); |
92
|
|
|
}; |
93
|
|
|
$this->mappings['security']['lockRevision'] = function ($value) { |
94
|
|
|
$this->object->getSecurity()->setLockRevision($value); |
95
|
|
|
}; |
96
|
|
|
$this->mappings['security']['lockStructure'] = function ($value) { |
97
|
|
|
$this->object->getSecurity()->setLockStructure($value); |
98
|
|
|
}; |
99
|
|
|
$this->mappings['security']['lockWindows'] = function ($value) { |
100
|
|
|
$this->object->getSecurity()->setLockWindows($value); |
101
|
|
|
}; |
102
|
|
|
$this->mappings['security']['revisionsPassword'] = function ($value) { |
103
|
|
|
$this->object->getSecurity()->setRevisionsPassword($value); |
104
|
|
|
}; |
105
|
|
|
$this->mappings['security']['workbookPassword'] = function ($value) { |
106
|
|
|
$this->object->getSecurity()->setWorkbookPassword($value); |
107
|
|
|
}; |
108
|
|
|
$this->mappings['subject'] = function ($value) { |
109
|
|
|
$this->object->getProperties()->setSubject($value); |
110
|
|
|
}; |
111
|
|
|
$this->mappings['template'] = function ($value) { |
112
|
|
|
$this->attributes['template'] = $value; |
113
|
|
|
}; |
114
|
|
|
$this->mappings['title'] = function ($value) { |
115
|
|
|
$this->object->getProperties()->setTitle($value); |
116
|
|
|
}; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param array $properties |
121
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
122
|
|
|
*/ |
123
|
|
|
public function start(array $properties = []) |
124
|
|
|
{ |
125
|
|
|
// load template |
126
|
|
|
if (isset($properties['template'])) { |
127
|
|
|
$templatePath = $this->expandPath($properties['template']); |
128
|
|
|
$reader = IOFactory::createReaderForFile($templatePath); |
129
|
|
|
$this->object = $reader->load($templatePath); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// create new |
133
|
|
|
else { |
134
|
|
|
$this->object = new Spreadsheet(); |
135
|
|
|
$this->object->removeSheetByIndex(0); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->attributes['properties'] = $properties; |
139
|
|
|
|
140
|
|
|
$this->setProperties($properties, $this->mappings); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param bool $preCalculateFormulas |
145
|
|
|
* @param null|string $diskCachingDirectory |
146
|
|
|
* @throws \InvalidArgumentException |
147
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
148
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
149
|
|
|
*/ |
150
|
|
|
public function end(bool $preCalculateFormulas = true, string $diskCachingDirectory = null) |
151
|
|
|
{ |
152
|
|
|
$format = null; |
153
|
|
|
|
154
|
|
|
// try document property |
155
|
|
|
if (isset($this->attributes['format'])) { |
156
|
|
|
$format = $this->attributes['format']; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// try Symfony request |
160
|
|
|
elseif (isset($this->context['app'])) { |
161
|
|
|
/** |
162
|
|
|
* @var AppVariable $appVariable |
163
|
|
|
*/ |
164
|
|
|
$appVariable = $this->context['app']; |
165
|
|
|
if ($appVariable instanceof AppVariable && $appVariable->getRequest() !== null) { |
166
|
|
|
$format = $appVariable->getRequest()->getRequestFormat(); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// set default |
171
|
|
|
if ($format === null || !is_string($format)) { |
172
|
|
|
$format = 'xlsx'; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
switch (strtolower($format)) { |
176
|
|
|
case 'csv': |
177
|
|
|
$writerType = 'Csv'; |
178
|
|
|
break; |
179
|
|
|
case 'ods': |
180
|
|
|
$writerType = 'Ods'; |
181
|
|
|
break; |
182
|
|
|
case 'pdf': |
183
|
|
|
$writerType = 'Pdf'; |
184
|
|
|
if (!class_exists('mPDF')) { |
185
|
|
|
throw new Exception('Error loading mPDF. Is mPDF correctly installed?'); |
186
|
|
|
} |
187
|
|
|
Settings::setPdfRendererName(Settings::PDF_RENDERER_MPDF); |
188
|
|
|
break; |
189
|
|
|
case 'xls': |
190
|
|
|
$writerType = 'Xls'; |
191
|
|
|
break; |
192
|
|
|
case 'xlsx': |
193
|
|
|
$writerType = 'Xlsx'; |
194
|
|
|
break; |
195
|
|
|
default: |
196
|
|
|
throw new \InvalidArgumentException(sprintf('Unknown format "%s"', $format)); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @var BaseWriter $writer |
201
|
|
|
*/ |
202
|
|
|
$writer = IOFactory::createWriter($this->object, $writerType); |
|
|
|
|
203
|
|
|
$writer->setPreCalculateFormulas($preCalculateFormulas); |
204
|
|
|
$writer->setUseDiskCaching($diskCachingDirectory !== null, $diskCachingDirectory); |
205
|
|
|
$writer->save('php://output'); |
206
|
|
|
|
207
|
|
|
$this->object = null; |
208
|
|
|
$this->attributes = []; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
// |
212
|
|
|
// Helpers |
213
|
|
|
// |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Resolves paths using Twig namespaces. |
217
|
|
|
* The path must start with the namespace. |
218
|
|
|
* Namespaces are case sensitive. |
219
|
|
|
* |
220
|
|
|
* @param string $path |
221
|
|
|
* @return string |
222
|
|
|
*/ |
223
|
|
|
private function expandPath(string $path): string |
224
|
|
|
{ |
225
|
|
|
$loader = $this->environment->getLoader(); |
226
|
|
|
|
227
|
|
|
if ($loader instanceof \Twig_Loader_Filesystem && mb_strpos($path, '@') === 0) { |
228
|
|
|
/** |
229
|
|
|
* @var \Twig_Loader_Filesystem $loader |
230
|
|
|
*/ |
231
|
|
|
foreach ($loader->getNamespaces() as $namespace) { |
232
|
|
|
if (mb_strpos($path, $namespace) === 1) { |
233
|
|
|
foreach ($loader->getPaths($namespace) as $namespacePath) { |
234
|
|
|
$expandedPathAttribute = str_replace('@' . $namespace, $namespacePath, $path); |
235
|
|
|
if (file_exists($expandedPathAttribute)) { |
236
|
|
|
return $expandedPathAttribute; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return $path; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
// |
247
|
|
|
// Getters/Setters |
248
|
|
|
// |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @return Spreadsheet|null |
252
|
|
|
*/ |
253
|
|
|
public function getObject() |
254
|
|
|
{ |
255
|
|
|
return $this->object; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param Spreadsheet|null $object |
260
|
|
|
*/ |
261
|
|
|
public function setObject(Spreadsheet $object = null) |
262
|
|
|
{ |
263
|
|
|
$this->object = $object; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @return array |
268
|
|
|
*/ |
269
|
|
|
public function getAttributes(): array |
270
|
|
|
{ |
271
|
|
|
return $this->attributes; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @param array $attributes |
276
|
|
|
*/ |
277
|
|
|
public function setAttributes(array $attributes) |
278
|
|
|
{ |
279
|
|
|
$this->attributes = $attributes; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @return array |
284
|
|
|
*/ |
285
|
|
|
public function getMappings(): array |
286
|
|
|
{ |
287
|
|
|
return $this->mappings; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @param array $mappings |
292
|
|
|
*/ |
293
|
|
|
public function setMappings(array $mappings) |
294
|
|
|
{ |
295
|
|
|
$this->mappings = $mappings; |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..