Skip to content

Commit a2c4195

Browse files
authored
Simplify a little bit the ExcelLoader code (#2103)
* Simplify a little bit the `ExcelLoader` code Core review changes * Fixing bug in `ExcelExtractor` skipping first row when no headers needed * Core review changes
1 parent 9f585f5 commit a2c4195

File tree

6 files changed

+32
-77
lines changed

6 files changed

+32
-77
lines changed

src/adapter/etl-adapter-excel/src/Flow/ETL/Adapter/Excel/ExcelExtractor.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ public function extract(FlowContext $context) : \Generator
5252
// Offset must be a positive number
5353
$offset = $this->offset ?? 1;
5454

55-
if (!$this->withHeader) {
56-
$offset++;
57-
}
58-
5955
foreach ($context->streams()->list($this->path, $this->filter()) as $stream) {
6056
foreach ($this->extractRows($stream, $headers, $offset) as $row) {
6157
// Ensure $row is an array before passing to array_to_rows

src/adapter/etl-adapter-excel/src/Flow/ETL/Adapter/Excel/ExcelLoader.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,12 @@ public function load(Rows $rows, FlowContext $context) : void
7474
timeFormat: $this->timeFormat,
7575
);
7676

77-
$streams = $context->streams();
77+
$stream = $context->streams()->writeTo($this->path, $rows->partitions()->toArray());
7878

79-
if ($rows->partitions()->count()) {
80-
$stream = $streams->writeTo($this->path, $rows->partitions()->toArray());
81-
} else {
82-
$stream = $streams->writeTo($this->path);
83-
}
84-
85-
$filePath = $stream->path()->path();
8679
$manager = $this->getWorkbookManager();
87-
$manager->open($filePath);
88-
89-
$rowIndex = 0;
80+
$manager->open($stream->path()->path());
9081

91-
foreach ($rows as $row) {
82+
foreach ($rows as $rowIndex => $row) {
9283
$sheetName = $this->resolveSheetName($row);
9384

9485
$rowForExcel = $this->sheetNameEntryName !== null && $row->has($this->sheetNameEntryName)
@@ -103,8 +94,6 @@ public function load(Rows $rows, FlowContext $context) : void
10394
$values = $normalizer->normalize($rowForExcel);
10495
$styles = $this->resolveCellStyles($rowForExcel, $rowIndex, $sheetName);
10596
$manager->writeRow($sheetName, $values, $styles);
106-
107-
$rowIndex++;
10897
}
10998
}
11099

@@ -243,9 +232,7 @@ private function resolveWriterType() : ExcelWriter
243232
return $this->writerType;
244233
}
245234

246-
$extension = $this->path->extension();
247-
248-
return match (\strtolower((string) $extension)) {
235+
return match ((string) $this->path->extension()) {
249236
'ods' => ExcelWriter::ODS,
250237
default => ExcelWriter::XLSX,
251238
};

src/adapter/etl-adapter-excel/src/Flow/ETL/Adapter/Excel/RowsNormalizer/ExcelRowsNormalizer.php

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Flow\ETL\Adapter\Excel\RowsNormalizer;
66

7+
use Flow\ETL\Exception\InvalidArgumentException;
78
use Flow\ETL\Row;
89
use Flow\ETL\Row\Entry;
910
use Flow\ETL\Row\Entry\{BooleanEntry,
@@ -66,68 +67,37 @@ public function normalize(Row $row) : array
6667
*/
6768
private function normalizeEntry(Entry $entry) : bool|float|int|string|null
6869
{
69-
$value = match ($entry::class) {
70+
return match ($entry::class) {
7071
BooleanEntry::class,
7172
IntegerEntry::class,
7273
FloatEntry::class,
7374
StringEntry::class => $entry->value(),
7475
DateTimeEntry::class => $entry->value()?->format($this->dateTimeFormat),
7576
DateEntry::class => $entry->value()?->format($this->dateFormat),
7677
TimeEntry::class => $entry->value()?->format($this->timeFormat),
77-
UuidEntry::class => $entry->toString(),
7878
EnumEntry::class => $this->normalizeEnumEntry($entry),
7979
JsonEntry::class,
8080
ListEntry::class,
8181
MapEntry::class,
8282
StructureEntry::class => $this->normalizeToJson($entry->value()),
83+
UuidEntry::class,
8384
XMLEntry::class,
8485
XMLElementEntry::class,
8586
HTMLEntry::class,
8687
HTMLElementEntry::class => $entry->toString(),
87-
default => $entry->toString(),
88+
default => throw new InvalidArgumentException('Unknown entry type: ' . $entry::class),
8889
};
89-
90-
if ($value === null) {
91-
return null;
92-
}
93-
94-
if (\is_bool($value)) {
95-
return $value;
96-
}
97-
98-
if (\is_int($value)) {
99-
return $value;
100-
}
101-
102-
if (\is_float($value)) {
103-
return $value;
104-
}
105-
106-
if (\is_string($value)) {
107-
return $value;
108-
}
109-
110-
return $entry->toString();
111-
}
112-
113-
private function normalizeEnum(\UnitEnum $enum) : string
114-
{
115-
if ($enum instanceof \BackedEnum) {
116-
return (string) $enum->value;
117-
}
118-
119-
return $enum->name;
12090
}
12191

12292
private function normalizeEnumEntry(EnumEntry $entry) : ?string
12393
{
12494
$value = $entry->value();
12595

126-
if ($value === null) {
127-
return null;
96+
if ($value instanceof \BackedEnum) {
97+
return (string) $value->value;
12898
}
12999

130-
return $this->normalizeEnum($value);
100+
return $value?->name;
131101
}
132102

133103
private function normalizeToJson(mixed $value) : ?string

src/adapter/etl-adapter-excel/src/Flow/ETL/Adapter/Excel/WorkbookManager.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,18 @@ public function open(string $filePath) : void
7777
*/
7878
public function writeHeader(string $sheetName, array $headers, ?Style $style = null) : void
7979
{
80-
$sheet = $this->getOrCreateSheet($sheetName);
8180
$writer = $this->writers[$this->currentFilePath];
8281

83-
$row = $style !== null
84-
? OpenSpoutRow::fromValuesWithStyle($headers, $style)
85-
: OpenSpoutRow::fromValues($headers);
82+
if ($sheetName !== $writer->getCurrentSheet()->getName()) {
83+
$sheet = $this->getOrCreateSheet($sheetName);
84+
$writer->setCurrentSheet($sheet);
85+
}
8686

87-
$writer->setCurrentSheet($sheet);
88-
$writer->addRow($row);
87+
$writer->addRow(
88+
$style !== null
89+
? OpenSpoutRow::fromValuesWithStyle($headers, $style)
90+
: OpenSpoutRow::fromValues($headers)
91+
);
8992

9093
$key = $this->sheetKey($sheetName);
9194
$this->headersWritten[$key] = true;
@@ -97,21 +100,19 @@ public function writeHeader(string $sheetName, array $headers, ?Style $style = n
97100
*/
98101
public function writeRow(string $sheetName, array $values, ?array $styles = null) : void
99102
{
100-
$sheet = $this->getOrCreateSheet($sheetName);
101103
$writer = $this->writers[$this->currentFilePath];
102104

103-
if ($styles !== null) {
104-
$filteredStyles = \array_filter($styles, static fn (?Style $style) : bool => $style !== null);
105-
106-
$row = \count($filteredStyles) > 0
107-
? OpenSpoutRow::fromValuesWithStyles($values, $filteredStyles)
108-
: OpenSpoutRow::fromValues($values);
109-
} else {
110-
$row = OpenSpoutRow::fromValues($values);
105+
if ($sheetName !== $writer->getCurrentSheet()->getName()) {
106+
$sheet = $this->getOrCreateSheet($sheetName);
107+
$writer->setCurrentSheet($sheet);
111108
}
112109

113-
$writer->setCurrentSheet($sheet);
114-
$writer->addRow($row);
110+
$writer->addRow(
111+
OpenSpoutRow::fromValuesWithStyles(
112+
$values,
113+
$styles ? \array_filter($styles, static fn (?Style $style) : bool => $style !== null) : []
114+
)
115+
);
115116
}
116117

117118
private function countSheetsForCurrentFile() : int

src/adapter/etl-adapter-excel/tests/Flow/ETL/Adapter/Excel/Tests/Integration/ExcelExtractorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function test_extract_excel_file_with_offset_without_header(string $fixtu
113113
->fetch()
114114
->toArray();
115115

116-
self::assertCount(6, $rows);
116+
self::assertCount(7, $rows);
117117

118118
foreach ($rows as $row) {
119119
self::assertSame(['e00', 'e01', 'e02'], \array_keys($row));
@@ -166,7 +166,7 @@ public function test_extract_excel_file_without_header(string $fixtureName) : vo
166166
->fetch()
167167
->toArray();
168168

169-
self::assertCount(10, $rows);
169+
self::assertCount(11, $rows);
170170

171171
foreach ($rows as $row) {
172172
self::assertSame(['e00', 'e01', 'e02'], \array_keys($row));

src/adapter/etl-adapter-excel/tests/Flow/ETL/Adapter/Excel/Tests/Integration/ExcelLoaderTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ public function test_round_trip_without_header(ExcelWriter $writer, string $exte
386386

387387
self::assertEquals(
388388
[
389+
['e00' => 1, 'e01' => 'Alice'],
389390
['e00' => 2, 'e01' => 'Bob'],
390391
],
391392
$rows

0 commit comments

Comments
 (0)