Formatters
Formatters are used to output dataset content in specific formats. The AnyDataset-Text package provides two formatters:
CSVFormatter- Outputs the content as a CSV file (field delimited)FixedSizeColumnFormatter- Outputs the content with columns defined by length
CSVFormatter
The CSVFormatter class allows you to output dataset content as a CSV file with customizable delimiter, quote character, and quoting behavior.
Basic Usage
Using CSVFormatter
<?php
$iterator = $anydataset->getIterator();
$formatter = new \ByJG\AnyDataset\Text\Formatter\CSVFormatter($iterator);
$csvContent = $formatter->toText();
Customization Options
$formatter = new \ByJG\AnyDataset\Text\Formatter\CSVFormatter($iterator);
// Set the delimiter character (default: ,)
$formatter->setDelimiter(';');
// Set the quote character (default: ")
$formatter->setQuote("'");
// Set the quoting behavior
$formatter->setApplyQuote(\ByJG\AnyDataset\Text\Formatter\CSVFormatter::APPLY_QUOTE_ALWAYS);
// Control whether to output header row (default: true)
$formatter->setOutputHeader(false);
// Get the formatted text
$csvContent = $formatter->toText();
Quoting Behaviors
The CSVFormatter supports several quoting behaviors:
CSVFormatter::APPLY_QUOTE_ALWAYS- Apply quotes to all fieldsCSVFormatter::APPLY_QUOTE_WHEN_REQUIRED- Apply quotes only when required (default)CSVFormatter::APPLY_QUOTE_ALL_STRINGS- Apply quotes to all string fieldsCSVFormatter::NEVER_APPLY_QUOTE- Never apply quotes
FixedSizeColumnFormatter
The FixedSizeColumnFormatter class allows you to output dataset content with fixed-width columns based on field definitions.
Basic Usage
Using FixedSizeColumnFormatter
<?php
$fieldDefinition = [
new \ByJG\AnyDataset\Text\Definition\FixedTextDefinition('id', 0, 3, \ByJG\AnyDataset\Text\Definition\TextTypeEnum::NUMBER),
new \ByJG\AnyDataset\Text\Definition\FixedTextDefinition('name', 3, 7, \ByJG\AnyDataset\Text\Definition\TextTypeEnum::STRING),
// ... more field definitions
];
$iterator = $anydataset->getIterator();
$formatter = new \ByJG\AnyDataset\Text\Formatter\FixedSizeColumnFormatter($iterator, $fieldDefinition);
$formattedContent = $formatter->toText();
Customization Options
$formatter = new \ByJG\AnyDataset\Text\Formatter\FixedSizeColumnFormatter($iterator, $fieldDefinition);
// Set the padding character for number fields (default: 0)
$formatter->setPadNumber('0');
// Set the padding character for string fields (default: space)
$formatter->setPadString(' ');
// Get the formatted text
$formattedContent = $formatter->toText();
Methods
CSVFormatter
__construct($anydataset, $delimiter = ",", $quote = '"', $applyQuote = 1)- Constructor with customizable optionstoText()- Returns the formatted text as a stringsetDelimiter($value)- Sets the delimiter charactergetDelimiter()- Gets the delimiter charactersetQuote($value)- Sets the quote charactergetQuote()- Gets the quote charactersetApplyQuote($value)- Sets the quoting behaviorgetApplyQuote()- Gets the quoting behaviorsetOutputHeader($outputHeader)- Sets whether to output the header rowgetOutputHeader()- Gets whether to output the header row
FixedSizeColumnFormatter
__construct(GenericIterator|Row $anydataset, array $fieldDefinition)- Constructor with required dataset and field definitionstoText()- Returns the formatted text as a stringsetPadNumber(string $padNumber)- Sets the padding character for number fieldsgetPadNumber()- Gets the padding character for number fieldssetPadString(string $padString)- Sets the padding character for string fieldsgetPadString()- Gets the padding character for string fields