Skip to content

Commit 362806a

Browse files
committed
refactor: remove redundant options
1 parent d546d7c commit 362806a

File tree

4 files changed

+79
-57
lines changed

4 files changed

+79
-57
lines changed

documentation/components/adapters/excel.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,45 @@ data_frame()
204204
->write(to_excel('path/to/output.xlsx')->withWriterOptions($options))
205205
->run();
206206
```
207+
208+
For ODS format:
209+
210+
```php
211+
<?php
212+
213+
use OpenSpout\Writer\ODS\Options as OdsOptions;
214+
215+
$options = new OdsOptions(
216+
DEFAULT_COLUMN_WIDTH: 15.0,
217+
DEFAULT_ROW_HEIGHT: 20.0,
218+
);
219+
220+
data_frame()
221+
->read($extractor)
222+
->write(to_excel('path/to/output.ods')->withWriterOptions($options))
223+
->run();
224+
```
225+
226+
### Custom Date/Time Formats
227+
228+
Control how date, datetime, and time values are formatted in the output:
229+
230+
```php
231+
<?php
232+
233+
data_frame()
234+
->read($extractor)
235+
->write(
236+
to_excel('path/to/output.xlsx')
237+
->withDateFormat('d/m/Y')
238+
->withDateTimeFormat('d/m/Y H:i')
239+
->withTimeFormat('%H:%I')
240+
)
241+
->run();
242+
```
243+
244+
Default formats:
245+
- Date: `Y-m-d`
246+
- DateTime: `Y-m-d H:i:s`
247+
- Time: `H:i:s`
248+

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

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ final class ExcelLoader implements Closure, FileLoader, Loader
3232

3333
private string $timeFormat = 'H:i:s';
3434

35-
private bool $useInlineStrings = true;
36-
3735
private bool $withHeader = true;
3836

3937
private ?WorkbookManager $workbookManager = null;
@@ -145,15 +143,12 @@ public function withHeaderStyle(Style $style) : self
145143
return $this;
146144
}
147145

148-
public function withInlineStrings(bool $useInlineStrings) : self
149-
{
150-
$this->useInlineStrings = $useInlineStrings;
151-
152-
return $this;
153-
}
154-
155146
public function withSheetName(?string $sheetName) : self
156147
{
148+
if ($sheetName !== null && $this->sheetNameEntryName !== null) {
149+
throw new InvalidArgumentException('Cannot set both sheetName and sheetNameFromEntry. These options are mutually exclusive.');
150+
}
151+
157152
if ($sheetName !== null) {
158153
SheetNameAssertion::assert($sheetName);
159154
}
@@ -165,6 +160,10 @@ public function withSheetName(?string $sheetName) : self
165160

166161
public function withSheetNameFromEntry(string $entryName) : self
167162
{
163+
if ($this->sheetName !== null) {
164+
throw new InvalidArgumentException('Cannot set both sheetName and sheetNameFromEntry. These options are mutually exclusive.');
165+
}
166+
168167
$this->sheetNameEntryName = $entryName;
169168

170169
return $this;
@@ -196,8 +195,7 @@ private function getWorkbookManager() : WorkbookManager
196195
if ($this->workbookManager === null) {
197196
$this->workbookManager = new WorkbookManager(
198197
writerType: $this->resolveWriterType(),
199-
xlsxOptions: $this->resolveXlsxOptions(),
200-
odsOptions: $this->resolveOdsOptions(),
198+
options: $this->writerOptions,
201199
);
202200
}
203201

@@ -224,15 +222,6 @@ private function resolveCellStyles(Row $row, int $rowIndex, string $sheetName) :
224222
return $styles;
225223
}
226224

227-
private function resolveOdsOptions() : ?OdsOptions
228-
{
229-
if ($this->writerOptions instanceof OdsOptions) {
230-
return $this->writerOptions;
231-
}
232-
233-
return null;
234-
}
235-
236225
private function resolveSheetName(Row $row) : string
237226
{
238227
if ($this->sheetNameEntryName !== null && $row->has($this->sheetNameEntryName)) {
@@ -261,15 +250,4 @@ private function resolveWriterType() : ExcelWriter
261250
default => ExcelWriter::XLSX,
262251
};
263252
}
264-
265-
private function resolveXlsxOptions() : ?XlsxOptions
266-
{
267-
if ($this->writerOptions instanceof XlsxOptions) {
268-
return $this->writerOptions;
269-
}
270-
271-
return $this->resolveWriterType() === ExcelWriter::XLSX
272-
? new XlsxOptions(SHOULD_USE_INLINE_STRINGS: $this->useInlineStrings)
273-
: null;
274-
}
275253
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ final class WorkbookManager
3232

3333
public function __construct(
3434
private readonly ExcelWriter $writerType = ExcelWriter::XLSX,
35-
private readonly ?XlsxOptions $xlsxOptions = null,
36-
private readonly ?OdsOptions $odsOptions = null,
35+
private readonly OdsOptions|XlsxOptions|null $options = null,
3736
) {
3837
}
3938

@@ -131,12 +130,16 @@ private function countSheetsForCurrentFile() : int
131130

132131
private function createOdsWriter() : OdsWriter
133132
{
134-
return new OdsWriter($this->odsOptions ?? new OdsOptions());
133+
$options = $this->options instanceof OdsOptions ? $this->options : new OdsOptions();
134+
135+
return new OdsWriter($options);
135136
}
136137

137138
private function createXlsxWriter() : XlsxWriter
138139
{
139-
return new XlsxWriter($this->xlsxOptions ?? new XlsxOptions());
140+
$options = $this->options instanceof XlsxOptions ? $this->options : new XlsxOptions();
141+
142+
return new XlsxWriter($options);
140143
}
141144

142145
private function getOrCreateSheet(string $sheetName) : Sheet

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

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use function Flow\ETL\Adapter\Excel\DSL\{from_excel, to_excel};
88
use function Flow\ETL\DSL\{bool_entry, date_entry, datetime_entry, df, float_entry, from_rows, int_entry, json_entry, row, rows, string_entry, time_entry, uuid_entry};
99
use Flow\ETL\Adapter\Excel\{CellStyler, ExcelWriter};
10+
use Flow\ETL\Exception\InvalidArgumentException;
1011
use Flow\ETL\Row\Entry;
1112
use Flow\ETL\Tests\FlowIntegrationTestCase;
1213
use OpenSpout\Common\Entity\Style\Style;
@@ -345,6 +346,26 @@ public function test_round_trip_without_header(ExcelWriter $writer, string $exte
345346
self::assertSame(['e00', 'e01'], \array_keys($rows[0]));
346347
}
347348

349+
public function test_sheet_name_and_sheet_name_from_entry_are_mutually_exclusive() : void
350+
{
351+
$this->expectException(InvalidArgumentException::class);
352+
$this->expectExceptionMessage('Cannot set both sheetName and sheetNameFromEntry');
353+
354+
to_excel('/tmp/test.xlsx')
355+
->withSheetName('MySheet')
356+
->withSheetNameFromEntry('category');
357+
}
358+
359+
public function test_sheet_name_from_entry_and_sheet_name_are_mutually_exclusive() : void
360+
{
361+
$this->expectException(InvalidArgumentException::class);
362+
$this->expectExceptionMessage('Cannot set both sheetName and sheetNameFromEntry');
363+
364+
to_excel('/tmp/test.xlsx')
365+
->withSheetNameFromEntry('category')
366+
->withSheetName('MySheet');
367+
}
368+
348369
public function test_with_cell_styler() : void
349370
{
350371
$outputPath = $this->cacheDir->suffix('output_cell_styler.xlsx')->path();
@@ -408,28 +429,6 @@ public function test_with_header_style() : void
408429
self::assertSame([1, 'Test'], [$rows[0]['id'], $rows[0]['name']]);
409430
}
410431

411-
public function test_with_inline_strings_disabled() : void
412-
{
413-
$outputPath = $this->cacheDir->suffix('output_no_inline_strings.xlsx')->path();
414-
415-
df()
416-
->read(from_rows(
417-
rows(
418-
row(int_entry('id', 1), string_entry('name', 'Test')),
419-
)
420-
))
421-
->write(to_excel($outputPath)->withInlineStrings(false))
422-
->run();
423-
424-
$rows = df()
425-
->read(from_excel($outputPath))
426-
->fetch()
427-
->toArray();
428-
429-
self::assertCount(1, $rows);
430-
self::assertSame([1, 'Test'], [$rows[0]['id'], $rows[0]['name']]);
431-
}
432-
433432
public function test_with_ods_options() : void
434433
{
435434
$outputPath = $this->cacheDir->suffix('output_with_ods_options.ods')->path();

0 commit comments

Comments
 (0)