The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started

Connections

Inserting Records

Selecting Records

Converting Records

Upgrading Guide

This is the documentation for the upcoming version 9.0. This is a work in progress

Records conversion

Converter Interface

The package provides classes which convert any collection of CSV records into:

All theses classes implements the Converter interface.

<?php

public Converter::convert(iterable $records): iterable

The $records argument can be:

Warning: A League\Csv\Writer object can not be converted.

Converters are immutable

Before convertion, you may want to configure your converter. Each provided converter exposes additional methods to correctly convert your records.

When building a converter object, the methods do not need to be called in any particular order, and may be called multiple times. Because all provided converters are immutable, each time their setter methods are called they will return a new object without modifying the current one.

Records encoding

<?php

public Encoder::inputEncoding(string $input_encoding): self
public Encoder::outputEncoding(string $output_encoding): self
public Encoder::encodeOne(array $record): array
public Encoder::encodeAll(iterable $records): iterable

Out of the box, all converters assume that your are submitting records on a valid encoding charset. For instance, if your data is not UTF-8 encoded some unexpected results or exception may be thrown when trying to convert your data in JSON format.

You can use the Encoder class to encode your records prior to converting it

<?php

use League\Csv\Encoder;
use League\Csv\JsonConverter;

$csv = new SplFileObject('/path/to/french.csv', 'r');
$csv->setFlags(SplFileObject::READ_CSV | SplFileObject::SKIP_EMPTY);

$encoder = (new Encoder())->inputEncoding('iso-8859-15');
$converter = new JsonConverter();

$json = $converter->convert($encoder->encodeAll($csv));

Tips: If your records come from a Reader object which supports PHP stream filters then it's recommended to use the library stream filtering mechanism to first encode your data in UTF-8.

Tips: The Encoder object can also be use to format records prior to insertion using Writer::addFormater.