CSV


CSV, Comma-Separated Values, is a spreadsheet-like computer file with values separated by commas.

What is it ?

The CSV plugin aims at integrating the CSV reader and writer into the Pipeline stack.

Installation

composer require php-etl/csv-plugin:'*'

Usage

Building an extractor

To build an extractor, you need to specify the path of your file.

csv:
  extractor:
    file_path: 'input.csv'

Building a loader

To build a loader, you need to specify the path of your file.

csv:
  loader:
    file_path: 'output.csv'

Additional options

To build extractors or loaders, additional options exist and can be used :

  • delimiter : sets the field separator
  • enclosure : sets the text enclosure character
  • escape : sets the escape character
  • safe_mode : enable safe mode in the Pipeline
  • columns : specify the name of the columns to retrieve or write
csv:
  loader:
    file_path: 'output.csv'
    delimiter: '/'
    enclosure: '"'
    escape: '\\'
    safe_mode: true
    columns:
      - firstname
      - lastname

Advanced Usage

Splitting into several files

To limit the number of lines to be written to your csv file, you can specify the max_lines option.

csv:
  expression_language:
    - 'Kiboko\Component\StringExpressionLanguage\StringExpressionLanguageProvider'
  loader:
    # ...
    max_lines: 20
    file_path: '@=env("OUTPUT_DIRECTORY")~format("output_%06d.csv",index)'

For the dynamic filename to work, you must install php-etl/string-expression-language:

composer require php-etl/string-expression-language

Warning : this option is only available for loaders

Using a nonstandard format

In some cases, it is possible to generate a csv file that does not correspond to the standard format (for example by removing the enclosures). For this we have added the nonstandard option which is a boolean.

csv:
  loader:
    # ...
    nonstandard: true

Notice : The nonstandard option cannot be used with the enclosure and escape options.