PHP-ETL - Operations
Load - CSV File(csv-write)

The csv-write operation writes data to a CSV file. It expects DataItem objects with associative arrays; keys become CSV headers (if has_headers is true). All DataItem objects should have consistent keys for a uniform CSV structure.

Options

  • file: The path to the CSV file to write to.
  • delimiter: (Optional) The character used to separate fields in the CSV file. Defaults to ;.
  • enclosure: (Optional) The character used to enclose fields in the CSV file. Defaults to ".
  • escape: (Optional) The character used to escape special characters in the CSV file. Defaults to \.
  • has_headers: (Optional) A boolean indicating whether to write a header row to the CSV file. Defaults to true.

Example

Here’s an example of how to use the CSV write operation to save transformed data to a new CSV file:

use Oliverde8\Component\PhpEtl\ChainConfig;
use Oliverde8\Component\PhpEtl\OperationConfig\Extract\CsvExtractConfig;
use Oliverde8\Component\PhpEtl\OperationConfig\Loader\CsvFileWriterConfig;
use Oliverde8\Component\PhpEtl\OperationConfig\Transformer\RuleTransformConfig;

$chainConfig = new ChainConfig();
$chainConfig
    ->addLink(new CsvExtractConfig())
    ->addLink((new RuleTransformConfig(add: false))
        ->addColumn('FullName', [
            ['implode' => [
                'values' => [
                    [['get' => ['field' => 'FirstName']]],
                    [['get' => ['field' => 'LastName']]],
                ],
                'with' => ' ',
            ]],
        ])
    )
    ->addLink(new CsvFileWriterConfig(
        file: '/path/to/output.csv',
        delimiter: ',',
        hasHeaders: true
    ));