-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractPortation.php
366 lines (310 loc) · 8.72 KB
/
AbstractPortation.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
<?php
namespace SosthenG\EntityPortationBundle;
use Doctrine\Common\Annotations\AnnotationReader;
use Liuggio\ExcelBundle\Factory;
use SosthenG\EntityPortationBundle\Annotation\EntityPortationReader;
use Symfony\Component\Translation\TranslatorInterface;
/**
* Class AbstractPortation
*
* @package SosthenG\EntityPortationBundle
* @author Sosthèn Gaillard <sosthen.gaillard@gmail.com>
*/
abstract class AbstractPortation
{
/* STATIC PART */
/**
* @var array
*/
protected static $defaultOptions = array('label' => '', 'visible' => true, 'position' => 'auto', 'getter' => '', 'setter' => '');
/**
* @var array
*/
protected static $replaceablePrefixes = array('get', 'is', 'has', 'my', 'set', 'add');
/**
* @var array
*/
protected static $extensions = array('CSV' => 'csv',
'Excel5' => 'xls',
'Excel2007' => 'xlsx',
'XML' => 'xml',
'HTML' => 'html',
'OpenDocument' => 'ods',
'PDF' => 'pdf');
/**
* Get a well formatted column name (without prefix or useless chars)
*
* @param string $name
* @param bool $replacePrefix
*
* @return string
*/
public static function getColumnName($name, $replacePrefix = true)
{
if ($replacePrefix) $name = preg_replace('/^(?>'.implode('|', self::$replaceablePrefixes).')/', '', $name);
$name = trim($name, '_');
return $name;
}
/**
* Format an array to match a valid options array
*
* @param array $options
*
* @return array
*/
public static function formatOptionsArray(array $options)
{
return array_merge(self::$defaultOptions, array_intersect_key($options, self::$defaultOptions));
}
/* END OF STATIC PART */
/**
* @var Factory
*/
protected $_phpExcelFactory;
/**
* @var \PHPExcel
*/
protected $_phpExcelObject;
/**
* @var array
*/
protected $_entities = array();
/**
* @var array
*/
protected $_columns = array();
/**
* @var bool
*/
protected $_annotate = false;
/**
* @var string
*/
protected $_fallbackValue = '';
/**
* @var string
*/
protected $_csvDelimiter = '';
/**
* @var TranslatorInterface
*/
protected $_translator = null;
/**
* Exportation constructor.
*
* @param Factory $phpExcelFactory PhpExcel Factory service
* @param TranslatorInterface $translator
*/
public function __construct(Factory $phpExcelFactory, TranslatorInterface $translator)
{
$this->_phpExcelFactory = $phpExcelFactory;
$this->_translator = $translator;
}
/**
* @return \PHPExcel
*/
public function getPhpExcelObject()
{
return $this->_phpExcelObject;
}
/**
* Set the PHPExcel Properties such as Creator, Title, Description, ...
*
* @param \PHPExcel_DocumentProperties $properties
*
* @return $this
*/
public function setProperties(\PHPExcel_DocumentProperties $properties)
{
$this->_phpExcelObject->setProperties($properties);
return $this;
}
/**
* Get the PHPExcel Properties defined
* You can define each properties directly from this result
*
* @return \PHPExcel_DocumentProperties
*/
public function getProperties()
{
return $this->_phpExcelObject->getProperties();
}
/**
* @return array The entities registered in the Portation object
*/
public function getEntities()
{
return $this->_entities;
}
/**
* @return array The columns detected
*/
public function getColumns()
{
return $this->_columns;
}
/**
* @param string $column
*
* @return $this
*
* @throws \OutOfBoundsException
*/
public function resetColumnDefaultOptions($column)
{
if (!is_string($column) || !array_key_exists($column, $this->_columns))
throw new \OutOfBoundsException("This column does not exist.");
$this->_columns[$column] = self::$defaultOptions;
return $this;
}
/**
* @param $column
*
* @return array
*
* @throws \OutOfBoundsException
*/
public function getColumnOptions($column)
{
if (!is_string($column) || !array_key_exists($column, $this->_columns))
throw new \OutOfBoundsException("This column does not exist.");
return $this->_columns[$column];
}
/**
* @param string $column
* @param array $options
*
* @return $this
*
* @throws \OutOfBoundsException
*/
public function setColumnOptions($column, array $options)
{
if (!is_string($column) || !array_key_exists($column, $this->_columns))
throw new \OutOfBoundsException("This column does not exist.");
$this->_columns[$column] = self::formatOptionsArray($options);
return $this;
}
/**
* @param string $column
* @param string $option
* @param mixed $value
*
* @return $this
*
* @throws \OutOfBoundsException
*/
public function setColumnOption($column, $option, $value)
{
if (!is_string($column) || !array_key_exists($column, $this->_columns))
throw new \OutOfBoundsException("This column does not exist.");
if (!is_string($option) || !array_key_exists($option, self::$defaultOptions))
throw new \OutOfBoundsException("This option does not exist.");
$this->_columns[$column][$option] = $value;
return $this;
}
/**
* @param bool $visible
*
* @return $this
*/
public function setAllVisible($visible = true)
{
foreach ($this->_columns as $column => $options) {
$options['visible'] = $visible;
}
return $this;
}
/**
* @return $this
*/
public function setAllPositionAuto()
{
foreach ($this->_columns as $column => $options) {
$options['position'] = 'auto';
}
return $this;
}
/**
* @param array $columns
*
* @return $this
*
* @throws \OutOfBoundsException
*/
protected function _addColumns(array $columns)
{
foreach ($columns as $column => $options) {
if (!array_key_exists($column, $this->_columns))
$this->_addColumn($column, $options);
else
$this->setColumnOptions($column, $options);
}
return $this;
}
/**
* @param string $column
* @param array $options
*
* @return $this
*/
protected function _addColumn($column, array $options)
{
if (!is_string($column))
throw new \InvalidArgumentException("This is not a valid column name.");
$this->_columns[$column] = self::formatOptionsArray($options);
return $this;
}
/**
* @return string
*/
public function getFallbackValue()
{
return $this->_fallbackValue;
}
/**
* Set the fallback value if a cell value is empty for an entity.
* Default is to put an empty string in the cell.
*
* @param string $fallbackValue
*
* @return $this
*/
public function setFallbackValue($fallbackValue = '')
{
$this->_fallbackValue = $fallbackValue;
return $this;
}
/**
* @return string
*/
public function getCsvDelimiter()
{
return $this->_csvDelimiter;
}
/**
* @param $delimiter
*
* @return $this
*/
public function setCsvDelimiter($delimiter)
{
$this->_csvDelimiter = $delimiter;
return $this;
}
/**
* Please ensure your class annotation is on the parent class if you have differents entities.
* If not, this method will only takes the first entity parameters
*
* @param object|string $entity
*/
protected function _setPropertiesFromEntitiesAnnotation($entity)
{
$reader = new EntityPortationReader(new AnnotationReader());
$properties = $reader->extractEntityParameters($entity);
if (isset($this->_sheetTitle) && !empty($properties->sheetTitle)) $this->_sheetTitle = $properties->sheetTitle;
if (!empty($properties->fallBackValue)) $this->_fallbackValue = $properties->fallBackValue;
if (!empty($properties->csvDelimiter)) $this->_csvDelimiter = $properties->csvDelimiter;
$this->_annotate = !empty($properties);
}
}