Bundled insertion helpers
Column consistency validator
The League\Csv\ColumnConsistency
class validates the inserted record column count consistency.
This class constructor accepts a single argument $column_count
which sets the column count value and validates each record length against the given value. If the value differs, a CannotInsertRecord
exception will be thrown.
If $column_count
equals -1
, the object will lazy set the column count value according to the next inserted record and therefore will also validate it. On the next insert, if the given value differs, a CannotInsertRecord
exception is triggered.
At any given time you can retrieve the column count value using the ColumnConsistency::getColumnCount
method.
use League\Csv\Writer;
use League\Csv\ColumnConsistency;
$validator = new ColumnConsistency();
$writer = Writer::createFromPath('/path/to/your/csv/file.csv');
$writer->addValidator($validator, 'column_consistency');
$validator->getColumnCount(); //returns -1
$writer->insertOne(["foo", "bar", "baz"]);
$validator->getColumnCount(); //returns 3
$writer->insertOne(["foo", "bar"]); //will trigger a CannotInsertRecord exception
Charset formatter
League\Csv\CharsetConverter will help you encode your records depending on your settings.