1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pim\Bundle\ExcelConnectorBundle\Writer; |
4
|
|
|
|
5
|
|
|
use Pim\Bundle\BaseConnectorBundle\Writer\File\FileWriter; |
6
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
7
|
|
|
use Pim\Bundle\ExcelConnectorBundle\Excel\Builder\ExcelBuilderInterface; |
8
|
|
|
use Pim\Bundle\ExcelConnectorBundle\Excel\Builder\ExcelBuilderFactory; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Xlsx file writer |
12
|
|
|
* |
13
|
|
|
* @author Antoine Guigan <[email protected]> |
14
|
|
|
* @copyright 2013 Akeneo SAS (http://www.akeneo.com) |
15
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
16
|
|
|
*/ |
17
|
|
|
class XlsxWriter extends FileWriter |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
* @Assert\NotBlank(groups={"Execution"}) |
22
|
|
|
*/ |
23
|
|
|
protected $filePath = '/tmp/export_%datetime%.xlsx'; |
24
|
|
|
|
25
|
|
|
/** @var ExcelBuilderFactory */ |
26
|
|
|
protected $builderFactory; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
protected $builderClass; |
30
|
|
|
|
31
|
|
|
/** @var array */ |
32
|
|
|
protected $builderOptions; |
33
|
|
|
|
34
|
|
|
/** @var ExcelBuilderInterface */ |
35
|
|
|
protected $builder; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param ExcelBuilderFactory $builderFactory |
39
|
|
|
* @param string $builderClass |
40
|
|
|
* @param array $builderOptions |
41
|
|
|
*/ |
42
|
|
|
public function __construct(ExcelBuilderFactory $builderFactory, $builderClass, array $builderOptions = array()) |
43
|
|
|
{ |
44
|
|
|
$this->builderFactory = $builderFactory; |
45
|
|
|
$this->builderClass = $builderClass; |
46
|
|
|
$this->builderOptions = $builderOptions; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function initialize() |
53
|
|
|
{ |
54
|
|
|
$this->builder = $this->builderFactory->create($this->builderClass, $this->builderOptions); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
|
|
public function write(array $data) |
61
|
|
|
{ |
62
|
|
|
foreach ($data as $item) { |
63
|
|
|
$this->builder->add($item); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function flush() |
71
|
|
|
{ |
72
|
|
|
$writer = new \PHPExcel_Writer_Excel2007($this->builder->getExcelObject()); |
73
|
|
|
$writer->save($this->getPath()); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|