1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pim\Bundle\ExcelConnectorBundle\Writer; |
4
|
|
|
|
5
|
|
|
use Pim\Bundle\BaseConnectorBundle\Writer\File\FileWriter; |
6
|
|
|
use Symfony\Component\Serializer\Encoder\EncoderInterface; |
7
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Excel 2003 XML writer |
11
|
|
|
* |
12
|
|
|
* @author Antoine Guigan <[email protected]> |
13
|
|
|
* @copyright 2013 Akeneo SAS (http://www.akeneo.com) |
14
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
15
|
|
|
*/ |
16
|
|
|
class Excel2003XmlWriter extends FileWriter |
17
|
|
|
{ |
18
|
|
|
/** @const string */ |
19
|
|
|
const COLUMN_XML = '<Column ss:Span="1" ss:Width="64"/>'; |
20
|
|
|
|
21
|
|
|
/** @var EncoderInterface */ |
22
|
|
|
protected $encoder; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
protected $format; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
protected $headerTemplate; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
protected $footerTemplate; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
* @Assert\NotBlank(groups={"Execution"}) |
36
|
|
|
*/ |
37
|
|
|
protected $filePath = '/tmp/export_%datetime%.xml'; |
38
|
|
|
|
39
|
|
|
/** @var array */ |
40
|
|
|
protected $labels; |
41
|
|
|
|
42
|
|
|
/** @var int */ |
43
|
|
|
protected $tempHandler; |
44
|
|
|
|
45
|
|
|
/** @var int */ |
46
|
|
|
protected $handler; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param EncoderInterface $encoder |
50
|
|
|
* @param string $format |
51
|
|
|
* @param string $headerTemplate |
52
|
|
|
* @param string $footerTemplate |
53
|
|
|
*/ |
54
|
|
|
public function __construct(EncoderInterface $encoder, $format, $headerTemplate, $footerTemplate) |
55
|
|
|
{ |
56
|
|
|
$this->encoder = $encoder; |
57
|
|
|
$this->format = $format; |
58
|
|
|
$this->headerTemplate = $headerTemplate; |
59
|
|
|
$this->footerTemplate = $footerTemplate; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function write(array $items) |
66
|
|
|
{ |
67
|
|
|
foreach ($items as $item) { |
68
|
|
|
$this->writeItem($item); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function initialize() |
76
|
|
|
{ |
77
|
|
|
$this->labels = []; |
78
|
|
|
$this->tempHandler = fopen('php://temp', 'rw'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function flush() |
85
|
|
|
{ |
86
|
|
|
$this->handler = fopen($this->getPath(), 'w'); |
87
|
|
|
$this->writeHeader(); |
88
|
|
|
$this->writeColumns(); |
89
|
|
|
$this->writeLabels(); |
90
|
|
|
fseek($this->tempHandler, 0); |
91
|
|
|
stream_copy_to_stream($this->tempHandler, $this->handler); |
92
|
|
|
$this->writeFooter(); |
93
|
|
|
fclose($this->tempHandler); |
94
|
|
|
fclose($this->handler); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Writes an item |
99
|
|
|
* |
100
|
|
|
* @param array $item |
101
|
|
|
*/ |
102
|
|
|
protected function writeItem(array $item) |
103
|
|
|
{ |
104
|
|
|
$context = $this->getContext($item); |
105
|
|
|
$row = []; |
106
|
|
|
foreach ($this->labels as $header) { |
107
|
|
|
if (array_key_exists($header, $item)) { |
108
|
|
|
$row[] = $item[$header]; |
109
|
|
|
unset($item[$header]); |
110
|
|
|
} else { |
111
|
|
|
$row[] = ''; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
foreach ($item as $header => $value) { |
116
|
|
|
$this->labels[] = $header; |
117
|
|
|
$row[] = $value; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
fwrite($this->tempHandler, $this->encoder->encode($row, $this->format, $context)); |
121
|
|
|
|
122
|
|
|
if ($this->stepExecution) { |
123
|
|
|
$this->stepExecution->incrementSummaryInfo('write'); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns the context for an encoder |
129
|
|
|
* |
130
|
|
|
* @param array $item |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
protected function getContext(array $item) |
135
|
|
|
{ |
136
|
|
|
return []; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Writes the header of the XML file |
141
|
|
|
*/ |
142
|
|
|
protected function writeHeader() |
143
|
|
|
{ |
144
|
|
|
$this->appendFile($this->headerTemplate); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Writes the footer of the XML file |
149
|
|
|
*/ |
150
|
|
|
protected function writeFooter() |
151
|
|
|
{ |
152
|
|
|
$this->appendFile($this->footerTemplate); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Writes the Columns section of the XML file |
157
|
|
|
* |
158
|
|
|
* @return null |
159
|
|
|
*/ |
160
|
|
|
protected function writeColumns() |
161
|
|
|
{ |
162
|
|
|
if (!count($this->labels)) { |
163
|
|
|
return; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
fwrite($this->handler, implode('', array_fill(0, count($this->labels), static::COLUMN_XML))); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Writes the labels row of the XML file |
171
|
|
|
*/ |
172
|
|
|
protected function writeLabels() |
173
|
|
|
{ |
174
|
|
|
fwrite($this->handler, $this->encoder->encode($this->labels, $this->format)); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Appends the contents of a file to the XML file |
179
|
|
|
* |
180
|
|
|
* @param string $fileName |
181
|
|
|
*/ |
182
|
|
|
protected function appendFile($fileName) |
183
|
|
|
{ |
184
|
|
|
$fd = fopen($fileName, 'r'); |
185
|
|
|
stream_copy_to_stream($fd, $this->handler); |
186
|
|
|
fclose($fd); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|