Skip to content

Configure export engines at construction and normalize encodings - #41

Open
donatj wants to merge 11 commits into
masterfrom
feature/csv-strategy-enum
Open

donatj wants to merge 11 commits into
masterfrom
feature/csv-strategy-enum

Conversation

@donatj

@donatj donatj commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Non-UTF-8 input cannot pass through DataSheet's JSON storage. Engine settings can also be changed after construction, including after export processing has begun.

This is intentionally a breaking change. DataSheet normalizes input to UTF-8. CSV output uses an encoding enum. Engine options are set at construction.

Migration Notes

CSV output encoding

  • Replace CsvEngine::UTF8, UTF16BE, UTF16LE, UTF32BE, and UTF32LE with the matching Quorum\Exporter\Engines\Csv\Encoding cases. The output encoding argument no longer accepts strings.
  • Replace generic CsvEngine::UTF16 and CsvEngine::UTF32 with an explicit big-endian or little-endian case. Output no longer depends on the host machine's byte order.
  • Other output encodings are no longer supported. Choose one of the five enum cases.

Input encoding

  • Move inputEncoding from the CsvEngine constructor to the DataSheet constructor. It accepts an Encoding case or an mbstring encoding name. UTF-8 remains the default.
  • Each sheet converts string values to UTF-8 when rows are added. Iteration and every export engine receive the converted values. Other scalar values are converted to strings. Array keys must be valid UTF-8.
  • Review positional CsvEngine constructor calls. The fourth argument is now bomEnabled, the fifth is multiSheetStrategy, and the sixth is tmpDir.
use Quorum\Exporter\DataSheet;
use Quorum\Exporter\Engines\Csv\Encoding;
use Quorum\Exporter\Engines\CsvEngine;

$sheet = new DataSheet(inputEncoding: 'ISO-8859-1');
$engine = new CsvEngine(outputEncoding: Encoding::UTF8);

Engine configuration

Set these options in the constructor. The setters and byte order mark toggle have been removed.

Previous call Constructor argument
$csv->setDelimiter($value) delimiter: $value
$csv->setEnclosure($value) enclosure: $value
$csv->setMultiSheetStrategy($value) multiSheetStrategy: $value
$csv->disableBom() bomEnabled: false
$csv->disableBom(false) bomEnabled: true
$csv->setTmpDir($path) tmpDir: $path
$spreadsheet->setCreatedTime($timestamp) createdTime: new DateTimeImmutable('@' . $timestamp)

Delimiter and enclosure values must now be a single ASCII byte. The CSV tmpDir option checks that the directory exists before ZIP export. It does not select where temporary streams are stored.

The SpreadsheetML createdTime argument accepts DateTimeInterface instead of an integer. Its time zone is preserved. Null uses the current time when exporting. Pass null for the old null-or-zero behavior. A date object at Unix timestamp zero now represents the Unix epoch. The @ constructor syntax uses UTC; set the time zone explicitly if the previous local-time representation is needed.

Subclasses must review the removed protected encoding setters and isLittleEndian() method. The CSV output encoding property now holds an Encoding case. The CSV input encoding property has been removed. SpreadsheetML's creation time property now holds a date object or null.

Validation

On PHP 8.5, make lint test and the MDDoc dry run pass. PHPUnit passes 40 tests with 94 assertions. Tests cover explicit output encodings, constructor options, date objects, Latin-1 and UTF-16 input, mixed scalar values, CSV quoting, SpreadsheetML output, and custom sheet iterators.

@donatj donatj changed the title Add enum for CSV multi-sheet strategies Use an enum for CSV multi-sheet strategies Nov 10, 2026
@donatj donatj changed the title Use an enum for CSV multi-sheet strategies Use enums for CSV strategies and encodings Nov 10, 2026
@donatj donatj changed the title Use enums for CSV strategies and encodings Rename the CSV engine and use enums for its options Nov 10, 2026
@donatj donatj changed the title Rename the CSV engine and use enums for its options Use enums for CSV strategies and encodings Nov 10, 2026
@donatj
donatj marked this pull request as ready for review November 10, 2026 19:37
Copilot AI balanced review requested due to automatic review settings November 10, 2026 19:37

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The conversion pipeline corrupts accepted non-ASCII CSV delimiters and enclosures, while the new test accepts a BOM/byte-order mismatch that breaks decoding on little-endian hosts.

2 open issues
What changed in this PR

This PR introduces typed CSV options and binary-safe sheet storage, allowing non-UTF-8 input to be converted before CSV formatting. It intentionally breaks the previous string-based API.

Changes:

  • Replace strategy and encoding constants with enums and add constructor options for strategy and byte order marks.
  • Preserve input bytes in DataSheet and convert values to UTF-8 before CSV formatting.
  • Update integration tests, the UTF-8 fixture, examples, and generated documentation.
File Description
test/​Integration/​fixtures/​basic-csv-UTF-8.csv Add the default UTF-8 byte order mark.
test/​Integration/​DataSheetTest.php Test binary-safe row round trips.
test/​Integration/​CsvIntegrationTest.php Cover enums, encodings, byte order marks, and export strategies.
src/​Engines/​CsvEngine.php Adopt typed options and per-value input conversion.
src/​Engines/​Csv/​MultiSheetStrategy.php Define concatenation and ZIP strategies.
src/​Engines/​Csv/​Encoding.php Define seven supported output encodings.
src/​DataSheet.php Replace JSON storage with binary-safe serialization.
README.md Update generated API documentation and example.
example/​simplecsv.php Configure ZIP output through the strategy enum.

Review effort: Balanced

Comment thread test/Integration/CsvIntegrationTest.php Resolved
Comment thread src/Engines/CsvEngine.php Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

UTF-16LE input decoding corrupts numeric values during CSV export because original scalar types are lost, requiring type preservation and a mixed text-and-number regression test.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Avoid decoding numeric values as UTF-16LE

src/​Engines/​CsvEngine.php:126

With inputEncoding: Encoding::UTF16LE, a row containing encoded text and the integer 42 exports the number as ㈴: DataSheet::normalizeValue() first casts it to ASCII "42", which this line then decodes as UTF-16LE. Preserve original scalar types or equivalent metadata through storage so input decoding applies only to original strings. Add a regression test mixing multibyte text with numeric values.

2 resolved since last review

Review effort: Balanced

donatj commented Nov 10, 2026

Copy link
Copy Markdown
Member Author

Fixed the mixed-type UTF-16LE input issue from Copilot's latest review in dcb0613. DataSheet retains the original scalar types for CSV conversion while its public iterator continues to return strings. The regression test covers encoded text, integers, floats, booleans, null, and an encoded numeric string.

@donatj
donatj force-pushed the feature/csv-strategy-enum branch from 2b3ddf9 to 35307af Compare November 10, 2026 22:21
@donatj donatj changed the title Use enums for CSV strategies and encodings Use an encoding enum and support non-UTF-8 CSV input Nov 10, 2026
@donatj donatj changed the title Use an encoding enum and support non-UTF-8 CSV input Configure export engines at construction and normalize encodings Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants