Skip to content

Commit 19293ad

Browse files
author
FBGER\dwinter
committed
PHP 8.2 Update
Removed Port/Spreadsheet
1 parent 4e7e055 commit 19293ad

File tree

2 files changed

+96
-10
lines changed

2 files changed

+96
-10
lines changed

composer.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020
}
2121
],
2222
"require": {
23-
"php": "^7.4|^8.0|^8.1",
23+
"php": ">=8.2",
2424
"ext-zlib": "*",
2525
"doctrine/persistence": "^2.4|^3.0",
2626
"guzzlehttp/guzzle": "^7.5",
2727
"portphp/csv": "^2.0",
28-
"portphp/spreadsheet": "^1.0",
29-
"symfony/config": "^4.4|^5.0|^6.0",
30-
"symfony/filesystem": "^4.4|^5.0|^6.0",
31-
"symfony/framework-bundle": "^4.4|^5.0|^6.0",
32-
"symfony/string": "^4.4|^5.0|^6.0",
33-
"webmozart/assert" : "^1.11"
28+
"symfony/config": "^6.0",
29+
"symfony/filesystem": "^6.0",
30+
"symfony/framework-bundle": "^6.0",
31+
"symfony/string": "^6.0",
32+
"webmozart/assert" : "^1.11",
33+
"phpoffice/phpspreadsheet": "^2.1"
3434
},
3535
"require-dev": {
3636
"fastbolt/test-helpers": "^0.1.1",

src/Reader/XlsxReader.php

+89-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
namespace Fastbolt\EntityImporter\Reader;
1010

11-
use Port\Spreadsheet\SpreadsheetReader;
11+
use PhpOffice\PhpSpreadsheet\IOFactory;
1212
use SplFileObject;
1313

1414
/**
1515
* @psalm-suppress PropertyNotSetInConstructor
1616
*/
17-
class XlsxReader extends SpreadsheetReader implements ReaderInterface
17+
class XlsxReader implements ReaderInterface
1818
{
1919
/**
2020
* Faulty rows
@@ -23,21 +23,58 @@ class XlsxReader extends SpreadsheetReader implements ReaderInterface
2323
*/
2424
protected ?array $errors = null;
2525

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+
2653
/**
2754
* @param SplFileObject $file
2855
* @param array<int,string> $columnHeaders
2956
* @param ?int $headerRowNumber
3057
*/
3158
public function __construct(SplFileObject $file, array $columnHeaders, ?int $headerRowNumber)
3259
{
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();
3466

3567
if (null !== $headerRowNumber) {
3668
$this->setHeaderRowNumber($headerRowNumber);
3769
}
3870
$this->setColumnHeaders($columnHeaders);
3971
}
4072

73+
public function setColumnHeaders(array $columnHeaders): void
74+
{
75+
$this->columnHeaders = $columnHeaders;
76+
}
77+
4178
/**
4279
* @inheritDoc
4380
*/
@@ -61,4 +98,53 @@ public function getErrors(): array
6198
/** @psalm-var array<int,array<int,mixed>> */
6299
return $this->errors;
63100
}
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+
}
64150
}

0 commit comments

Comments
 (0)