Skip to content

feat: add encodeFile() and encodeDocumentFile() convenience methods#4

Merged
dereuromark merged 1 commit intomasterfrom
feature/file-convenience-apis
Mar 25, 2026
Merged

feat: add encodeFile() and encodeDocumentFile() convenience methods#4
dereuromark merged 1 commit intomasterfrom
feature/file-convenience-apis

Conversation

@dereuromark
Copy link
Contributor

Summary

Add file writing convenience methods to the Toml facade:

  • encodeFile(path, data, options): Encode PHP array to TOML and write to file
  • encodeDocumentFile(path, doc, options): Encode AST document to TOML and write to file

Usage

// Encode array directly to file
Toml::encodeFile('/path/to/config.toml', $data);

// With options
Toml::encodeFile('/path/to/config.toml', $data, new EncoderOptions(sortKeys: true));

// Encode document to file (e.g., after AST editing)
$doc = Toml::parse($input, true);
// ... modify $doc ...
Toml::encodeDocumentFile('/path/to/output.toml', $doc, new EncoderOptions(
    documentFormatting: DocumentFormattingMode::SourceAware
));

Details

  • Both methods throw EncodeException on write failure
  • Consistent with existing decodeFile() pattern
  • All encoder options supported
  • Full test coverage including error cases

Add file writing convenience methods to the Toml facade:

- encodeFile(path, data, options): Encode PHP array to TOML and write to file
- encodeDocumentFile(path, doc, options): Encode AST document to TOML and write to file

Both methods throw EncodeException on write failure, consistent with
existing error handling patterns in the library.

Includes tests for successful writes, option passing, and error cases.
Updates README with examples.
@dereuromark dereuromark merged commit 9ad7d6d into master Mar 25, 2026
3 checks passed
@dereuromark dereuromark deleted the feature/file-convenience-apis branch March 25, 2026 04:38
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.

1 participant