-
Notifications
You must be signed in to change notification settings - Fork 0
/
Export.php
382 lines (324 loc) · 12.6 KB
/
Export.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
namespace SosthenG\EntityPortationBundle;
use Doctrine\Common\Annotations\AnnotationReader;
use SosthenG\EntityPortationBundle\Annotation\EntityPortationReader;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\StreamedResponse;
/**
* Converts an array of entities to excel
*
* @package SosthenG\EntityPortationBundle
*/
class Export extends AbstractPortation
{
/**
* @var array
*/
public static $replaceablePrefixes = array('get', 'is', 'has', 'my');
public static function getReplaceablePrefixes()
{
return self::$replaceablePrefixes;
}
/**
* @var string
*/
protected $_sheetTitle = '';
/**
* @var bool|null
*/
protected $_entitiesHaveSameClass = null;
/**
* @var bool
*/
protected $_replaceIfExists = true;
/**
* Be careful, if you prefer to let the PortationBundle detect if you entities are instances of the same class,
* it will parse all of them to check if they are. It can increase the export time if you have a lot of entities.
*
* @param object[] $entities The entity array
* @param null|bool $sameClass Are entities instance of the exact same class ?
* @param bool $replaceIfExists If the reader find two or more columns with same name, should it replace with the last or keep the first ?
*
* @throws \OutOfBoundsException
*/
public function setEntities(array $entities, $sameClass = null, $replaceIfExists = true)
{
if (count($entities) < 1)
throw new \InvalidArgumentException("The entity array must not be empty.");
if (!$this->_hasCommonParent($entities))
throw new \InvalidArgumentException("Entities must have a common parent.");
$this->_entities = $entities;
$this->_replaceIfExists = $replaceIfExists;
$this->_entitiesHaveSameClass = $sameClass;
$this->_setPropertiesFromEntitiesAnnotation($this->_entities[0]);
$this->_setColumnsFromReader();
if (count($this->_columns) < 1)
throw new \InvalidArgumentException("Entities have no accessible parameters.");
$this->_phpExcelObject = $this->_phpExcelFactory->createPHPExcelObject();
}
/**
* Stream the file as Response
*
* The $outputType must be one of these values : "Excel5", "Excel2007", "CSV", "HTML", "OpenDocument"
* Check the PHPExcel documentation to see other available types.
*
* @param string $outputType
* @param string $output File name (and directory if needed) to output
*
* @return StreamedResponse
*
* @throws \OutOfBoundsException
*/
public function getResponse($outputType, $output, $encoding = "UTF8")
{
$this->_createPHPExcelObject();
// If there is no extension, add the good one
if (preg_match('/\.[a-zA-Z]+$/', $output) === 0) {
$output .= '.'.$this->_getExtension($outputType);
}
$writer = $this->_phpExcelFactory->createWriter($this->_phpExcelObject, $outputType);
if ($outputType == 'CSV' && !empty($this->_csvDelimiter)) {
/** @var $writer \PHPExcel_Writer_CSV */
$writer->setDelimiter($this->_csvDelimiter);
}
$response = $this->_phpExcelFactory->createStreamedResponse($writer);
// adding headers
$dispositionHeader = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
$output
);
$response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
$response->headers->set('Pragma', 'public');
$response->headers->set('Cache-Control', 'maxage=1');
$response->headers->set('Content-Disposition', $dispositionHeader);
return $response;
}
/**
* Save the file to $output
*
* The $outputType must be one of these values : "Excel5", "Excel2007", "CSV", "HTML", "XML", "OpenDocument"
* "PDF" can also be available, check the PHPExcel documentation.
*
* @param string $outputType
* @param string $output File name (and directory if needed) to output
*
* @return string $output
*
* @throws \OutOfBoundsException
* @throws \PHPExcel_Writer_Exception
*/
public function saveAsFile($outputType, $output)
{
$this->_createPHPExcelObject();
// If there is no extension, add the good one
if (preg_match('/\.[a-zA-Z]+$/', $output) === 0) {
$output .= '.'.$this->_getExtension($outputType);
}
$writer = $this->_phpExcelFactory->createWriter($this->_phpExcelObject, $outputType);
if ($outputType == 'CSV' && !empty($this->_csvDelimiter)) {
/** @var $writer \PHPExcel_Writer_CSV */
$writer->setDelimiter($this->_csvDelimiter);
}
$writer->save($output);
return $output;
}
/**
* @return string
*/
public function getSheetTitle()
{
return $this->_sheetTitle;
}
/**
* @param string $title
*
* @return $this
*/
public function setSheetTitle($title)
{
$this->_sheetTitle = $title;
return $this;
}
/**
* @param $outputType
*
* @return string The extension that match the type or an empty string
*/
protected function _getExtension($outputType)
{
$extensions = self::$extensions;
if (empty($extensions[$outputType]))
throw new \InvalidArgumentException("This filename has no extension and the output type is invalid.");
return $extensions[$outputType];
}
/**
* Generate the PHPExcel for the given entities
* Automatically called when getResponse or saveAsfile are called.
* Translate the columns names, if a translation exists
*/
protected function _createPHPExcelObject()
{
// Deletes the sheet if it has already been defined
$this->_phpExcelObject->disconnectWorksheets();
$this->_phpExcelObject->createSheet();
$sheet = $this->_phpExcelObject->setActiveSheetIndex(0);
$linePos = '1';
$autoColumns = array();
foreach ($this->_columns as $column => $options) {
if ($options['position'] == 'auto') {
$autoColumns[$column] = $options;
}
elseif ($options['visible']) {
$label = !empty($options['label']) ? $this->_translator->trans($options['label']) : $column;
$columnPos = \PHPExcel_Cell::stringFromColumnIndex($options['position']);
if (!empty($sheet->getCell($columnPos . $linePos)->getValue()))
throw new \OutOfBoundsException("There is a position conflict, two columns asked for the same index.");
$this->_columns[$column]['cell'] = $columnPos;
$sheet->setCellValue($columnPos . $linePos, $label);
}
}
$autoPos = 'A';
foreach ($autoColumns as $column => $options) {
if ($options['visible']) {
$label = !empty($options['label']) ? $options['label'] : $column;
// Get first available position
do {
$columnPos = $autoPos++;
} while (!empty($sheet->getCell($columnPos . $linePos)
->getValue()));
$this->_columns[$column]['cell'] = $columnPos;
$sheet->setCellValue($columnPos . $linePos, $label);
}
}
foreach ($this->_entities as $entity) {
$linePos++;
foreach ($this->_columns as $columnName => $options) {
if ($options['visible']) {
$getter = $options['getter'];
$value = $this->_fallbackValue;
if (is_callable(array($entity, $getter)))
$value = $this->_convertValue($entity->$getter());
elseif (!empty($entity->$columnName))
$value = $this->_convertValue($entity->$columnName);
elseif ($result = $this->_checkPossibleGetters(new \ReflectionObject($entity), $columnName, $entity))
$value = $this->_convertValue($result);
$sheet->setCellValue($options['cell'] . $linePos, $value);
}
}
}
foreach ($this->_columns as $column => $options) {
$sheet->getColumnDimension($options['cell'])->setAutoSize(true);
$sheet->getStyle($options['cell'].'1')->getFont()->setBold(true);
}
if (!empty($this->_sheetTitle)) {
$sheet->setTitle($this->_sheetTitle);
}
elseif (!empty($title = $this->_phpExcelObject->getProperties()
->getTitle())
) {
$sheet->setTitle($title);
}
}
/**
* Recursive method that convert a given entity property value to string
*
* If the value is an object, the __toString method will be called if defined, or the object will be converted to array =>
* If the value is an array, implode it to a string with a comma separator
*
* @param mixed $value
*
* @return string
*/
protected function _convertValue($value)
{
while (is_object($value)) {
$refl = new \ReflectionObject($value);
if ($refl->hasMethod('__toString')) $value = $refl->getMethod('__toString')->invoke($value);
else $value = (array)$value;
}
if (is_array($value)) {
$value = array_map(array($this, '_convertValue'), $value);
$value = implode(', ', $value);
}
return $value;
}
/**
* @param \ReflectionObject $refl
* @param string $property
* @param object $object
*
* @return mixed
*/
protected function _checkPossibleGetters(\ReflectionObject $refl, $property, $object)
{
foreach (self::$replaceablePrefixes as $prefix) {
if ($refl->hasMethod($prefix.ucfirst($property)) && $refl->getMethod($prefix.ucfirst($property))->isPublic()) {
return $refl->getMethod($prefix.ucfirst($property))->invoke($object);
}
}
return null;
}
/**
* Detect the entities possible columns
* In case of different objects (but inheriting from the same class), properties of all the objects will be used
*
* @throws \OutOfBoundsException
*/
protected function _setColumnsFromReader()
{
$reader = new EntityPortationReader(new AnnotationReader());
// If all entities are from the exact same class, just add the columns of the first one which are the sames for the others
if ($this->_entitiesAreInstanceOfSameClass()) {
$columns = $reader->extractColumnsFromEntity($this->_entities[0], $this->_replaceIfExists, $this->_annotate);
$this->_addColumns($columns);
}
// Else, merge all classes columns
else {
$classParsed = array();
foreach ($this->_entities as $entity) {
if (!in_array(($class = get_class($entity)), $classParsed)) {
$classParsed[] = $class;
$columns = $reader->extractColumnsFromEntity($entity, $this->_replaceIfExists, $this->_annotate);
$this->_addColumns($columns);
}
}
}
}
/**
* @return bool
*/
protected function _entitiesAreInstanceOfSameClass()
{
if ($this->_entitiesHaveSameClass !== null) return $this->_entitiesHaveSameClass;
$class = get_class($this->_entities[0]);
for ($i = 1; $i < count($this->_entities); $i++) {
if ($class != get_class($this->_entities[$i]))
return $this->_entitiesHaveSameClass = false;
}
return $this->_entitiesHaveSameClass = true;
}
/**
* @param array $entities
*
* @return bool
*/
protected function _hasCommonParent(array $entities)
{
$higherParent = $this->_getObjectHigherParent($entities[0]);
for ($i = 1; $i < count($entities); $i++) {
if ($higherParent != $this->_getObjectHigherParent($entities[$i]))
return false;
}
return true;
}
/**
* @param object $object
*
* @return string The classname of the higher parent for this object
*/
protected function _getObjectHigherParent($object)
{
for ($higher = ($class = get_class($object)); $class = get_parent_class($class); $higher = $class) ;
return $higher;
}
}