1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Akeneo\Component\SpreadsheetParser\Xlsx; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Row iterator for an Excel worksheet |
7
|
|
|
* |
8
|
|
|
* The iterator returns arrays of results. |
9
|
|
|
* |
10
|
|
|
* Empty values are trimmed from the right of the rows, and empty rows are skipped. |
11
|
|
|
* |
12
|
|
|
* @author Antoine Guigan <[email protected]> |
13
|
|
|
* @copyright 2014 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 RowIterator implements \Iterator |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var RowBuilderFactory |
20
|
|
|
*/ |
21
|
|
|
protected $rowBuilderFactory; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ColumnIndexTransformer |
25
|
|
|
*/ |
26
|
|
|
protected $columnIndexTransformer; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ValueTransformer |
30
|
|
|
*/ |
31
|
|
|
protected $valueTransformer; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $path; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $options; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var \XMLReader |
45
|
|
|
*/ |
46
|
|
|
protected $xml; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
protected $currentKey; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected $currentValue; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var boolean |
60
|
|
|
*/ |
61
|
|
|
protected $valid; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The Archive from which the path was extracted. |
65
|
|
|
* |
66
|
|
|
* A reference to the object is kept here to ensure that it is not deleted |
67
|
|
|
* before the RowIterator, as this would remove the extraction folder. |
68
|
|
|
* |
69
|
|
|
* @var Archive |
70
|
|
|
*/ |
71
|
|
|
private $archive; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Constructor |
75
|
|
|
* |
76
|
|
|
* @param RowBuilderFactory $rowBuilderFactory |
77
|
|
|
* @param ColumnIndexTransformer $columnIndexTransformer |
78
|
|
|
* @param ValueTransformer $valueTransformer |
79
|
|
|
* @param string $path |
80
|
|
|
* @param array $options |
81
|
|
|
* @param Archive $archive The Archive from which the path was extracted |
82
|
|
|
*/ |
83
|
|
|
public function __construct( |
84
|
|
|
RowBuilderFactory $rowBuilderFactory, |
85
|
|
|
ColumnIndexTransformer $columnIndexTransformer, |
86
|
|
|
ValueTransformer $valueTransformer, |
87
|
|
|
$path, |
88
|
|
|
array $options, |
89
|
|
|
Archive $archive |
90
|
|
|
) { |
91
|
|
|
$this->rowBuilderFactory = $rowBuilderFactory; |
92
|
|
|
$this->columnIndexTransformer = $columnIndexTransformer; |
93
|
|
|
$this->valueTransformer = $valueTransformer; |
94
|
|
|
$this->path = $path; |
95
|
|
|
$this->options = $options; |
96
|
|
|
$this->archive = $archive; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
|
|
public function current() |
103
|
|
|
{ |
104
|
|
|
return $this->currentValue; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
public function key() |
111
|
|
|
{ |
112
|
|
|
return $this->currentKey; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
public function next() |
119
|
|
|
{ |
120
|
|
|
$this->valid = false; |
121
|
|
|
|
122
|
|
|
$style = null; |
123
|
|
|
$type = null; |
124
|
|
|
$columnIndex = null; |
125
|
|
|
$rowBuilder = null; |
126
|
|
|
$currentKey = 0; |
127
|
|
|
|
128
|
|
|
while ($this->xml->read()) { |
129
|
|
|
if (\XMLReader::ELEMENT === $this->xml->nodeType) { |
130
|
|
|
switch ($this->xml->name) { |
131
|
|
|
case 'row': |
132
|
|
|
$currentKey = (int)$this->xml->getAttribute('r'); |
133
|
|
|
$rowBuilder = $this->rowBuilderFactory->create(); |
134
|
|
|
break; |
135
|
|
|
case 'c': |
136
|
|
|
$columnIndex = $this->columnIndexTransformer->transform($this->xml->getAttribute('r')); |
137
|
|
|
$style = $this->getValue($this->xml->getAttribute('s')); |
138
|
|
|
$type = $this->getValue($this->xml->getAttribute('t')); |
139
|
|
|
break; |
140
|
|
|
case 'v': |
141
|
|
|
$rowBuilder->addValue( |
142
|
|
|
$columnIndex, |
143
|
|
|
$this->valueTransformer->transform($this->xml->readString(), $type, $style) |
144
|
|
|
); |
145
|
|
|
break; |
146
|
|
|
case 'is': |
147
|
|
|
$rowBuilder->addValue($columnIndex, $this->xml->readString()); |
148
|
|
|
break; |
149
|
|
|
} |
150
|
|
|
} elseif (\XMLReader::END_ELEMENT === $this->xml->nodeType) { |
151
|
|
|
switch ($this->xml->name) { |
152
|
|
|
case 'row': |
153
|
|
|
$currentValue = $rowBuilder->getData(); |
154
|
|
|
if (count($currentValue)) { |
155
|
|
|
$this->currentKey = $currentKey; |
156
|
|
|
$this->currentValue = $currentValue; |
157
|
|
|
$this->valid = true; |
158
|
|
|
|
159
|
|
|
return; |
160
|
|
|
} |
161
|
|
|
break; |
162
|
|
|
case 'sheetData': |
163
|
|
|
break 2; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
|
public function rewind() |
173
|
|
|
{ |
174
|
|
|
if ($this->xml) { |
175
|
|
|
$this->xml->close(); |
176
|
|
|
} |
177
|
|
|
$this->xml = new \XMLReader(); |
178
|
|
|
$this->xml->open($this->path); |
179
|
|
|
$this->next(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritdoc} |
184
|
|
|
*/ |
185
|
|
|
public function valid() |
186
|
|
|
{ |
187
|
|
|
return $this->valid; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Returns a normalized attribute value |
192
|
|
|
* |
193
|
|
|
* @param string $value |
194
|
|
|
* |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
|
|
protected function getValue($value) |
198
|
|
|
{ |
199
|
|
|
return null === $value ? '' : $value; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|