8
8
9
9
namespace Fastbolt \EntityImporter \Reader ;
10
10
11
- use Port \ Spreadsheet \ SpreadsheetReader ;
11
+ use PhpOffice \ PhpSpreadsheet \ IOFactory ;
12
12
use SplFileObject ;
13
13
14
14
/**
15
15
* @psalm-suppress PropertyNotSetInConstructor
16
16
*/
17
- class XlsxReader extends SpreadsheetReader implements ReaderInterface
17
+ class XlsxReader implements ReaderInterface
18
18
{
19
19
/**
20
20
* Faulty rows
@@ -23,21 +23,58 @@ class XlsxReader extends SpreadsheetReader implements ReaderInterface
23
23
*/
24
24
protected ?array $ errors = null ;
25
25
26
+ /**
27
+ * @var array
28
+ */
29
+ protected array $ columnHeaders ;
30
+
31
+ /**
32
+ * Total number of rows
33
+ *
34
+ * @var int
35
+ */
36
+ protected int $ count ;
37
+
38
+ /**
39
+ * @var int|null
40
+ */
41
+ protected ?int $ headerRowNumber = null ;
42
+
43
+ /**
44
+ * @var int
45
+ */
46
+ protected int $ pointer = 0 ;
47
+
48
+ /**
49
+ * @var array
50
+ */
51
+ protected array $ worksheet ;
52
+
26
53
/**
27
54
* @param SplFileObject $file
28
55
* @param array<int,string> $columnHeaders
29
56
* @param ?int $headerRowNumber
30
57
*/
31
58
public function __construct (SplFileObject $ file , array $ columnHeaders , ?int $ headerRowNumber )
32
59
{
33
- parent ::__construct ($ file , $ headerRowNumber );
60
+ $ reader = IOFactory::createReaderForFile ($ file ->getPathName ());
61
+ $ reader ->setReadDataOnly (true );
62
+
63
+ $ spreadsheet = $ reader ->load ($ file ->getPathname ());
64
+
65
+ $ this ->worksheet = $ spreadsheet ->getActiveSheet ()->toArray ();
34
66
35
67
if (null !== $ headerRowNumber ) {
36
68
$ this ->setHeaderRowNumber ($ headerRowNumber );
37
69
}
38
70
$ this ->setColumnHeaders ($ columnHeaders );
39
71
}
40
72
73
+ public function setColumnHeaders (array $ columnHeaders ): void
74
+ {
75
+ $ this ->columnHeaders = $ columnHeaders ;
76
+ }
77
+
41
78
/**
42
79
* @inheritDoc
43
80
*/
@@ -61,4 +98,53 @@ public function getErrors(): array
61
98
/** @psalm-var array<int,array<int,mixed>> */
62
99
return $ this ->errors ;
63
100
}
101
+
102
+ public function current ()
103
+ {
104
+ $ row = $ this ->worksheet [$ this ->pointer ];
105
+
106
+ // If the spreadsheet file has column headers, use them to construct an associative
107
+ // array for the columns in this line
108
+ if (!empty ($ this ->columnHeaders ) && count ($ this ->columnHeaders ) === count ($ row )) {
109
+ return array_combine (array_values ($ this ->columnHeaders ), $ row );
110
+ }
111
+
112
+ // Else just return the column values
113
+ return $ row ;
114
+ }
115
+
116
+ public function setHeaderRowNumber (int $ rowNumber ): void
117
+ {
118
+ $ this ->headerRowNumber = $ rowNumber ;
119
+ $ this ->columnHeaders = $ this ->worksheet [$ rowNumber ];
120
+ }
121
+
122
+ public function getColumnHeaders (): array
123
+ {
124
+ return $ this ->columnHeaders ;
125
+ }
126
+
127
+ public function next (): void
128
+ {
129
+ $ this ->pointer ++;
130
+ }
131
+
132
+ public function key (): mixed
133
+ {
134
+ return $ this ->pointer ;
135
+ }
136
+
137
+ public function valid (): bool
138
+ {
139
+ return isset ($ this ->worksheet [$ this ->pointer ]);
140
+ }
141
+
142
+ public function rewind (): void
143
+ {
144
+ if (null === $ this ->headerRowNumber ) {
145
+ $ this ->pointer = 0 ;
146
+ } else {
147
+ $ this ->pointer = $ this ->headerRowNumber + 1 ;
148
+ }
149
+ }
64
150
}
0 commit comments