A package with classes to manage reading and writing of csv files.
Add to your project using composer:
composer require talkingbit/csv
Reader allows us to read the contents of a given file in the file system. Contents are yielded using a Generator. Typical usage could be like this. Rows will be read as plain arrays.
use TalkingBit\Csv\Reader\Reader;
$reader = new Reader();
$filePath = '/path/to/file.csv';
$rows = $reader
->fromFile($filePath)
->readAll();
foreach ($rows as $row) {
// Do whatever you need
}
If the file has csv headers, you can use the following setup, so rows will be read as associative arrays:
use TalkingBit\Csv\Reader\Reader;
$reader = new Reader();
$filePath = '/path/to/file.csv';
$rows = $reader
->fromFile($filePath)
->withHeaders()
->readAll();
foreach ($rows as $row) {
// Do whatever you need
}
Also, you can map rows to a simple Dto, provided that all relevant fields are public:
use TalkingBit\Csv\Reader\Reader;
use TalkingBit\Csv\Reader\Mapper\DtoMapper;
$reader = new Reader();
$filePath = '/path/to/file.csv';
$rows = $reader
->fromFile($filePath)
->withHeaders()
->usingMapper(new DtoMapper(MyDto::class))
->readAll();
foreach ($rows as $row) {
// Do whatever you need
}
Writer allows us to write data to a CSV file.
A row can be a plain array:
use TalkingBit\Csv\Writer\Writer;
$writer = new Writer();
$writer
->toFile('/path/to/file.csv')
->writeRow([123, 'My name']);
A row can be an associative array. In this case, keys will be used as CSV headers.
use TalkingBit\Csv\Writer\Writer;
$writer = new Writer();
$writer
->toFile('/path/to/file.csv')
->writeRow(['id' => 123, 'name' => 'My name']);
Also, you can write Dto directly to CSV files. Dtos will be treated as if they were associative arrays.
use TalkingBit\Csv\Writer\Writer;
$writer = new Writer();
$dto = new MyDto();
$dto->id = 123;
$dto->name = 'My name';
$writer
->toFile('/path/to/file.csv')
->writeRow($dto);
You can customize delimiters and enclosure characters:
$writer
->toFile('/path/to/file.csv')
->withDelimiter(',')
->withEnclosure('"')
->writeRow($dto);
You can create custom mappers for the Reader implementing the following interface:
interface RowMapperInterface
{
public function map(array $line, ?array $headers = null);
}
The $line
parameter contains the row data from the file. The $headers
contains csv headers if found. You can return any type, so you are free to do things like:
- Build application objects.
- Make calculations with input data.
- Validate input data.
Feel free to open issues or pull requests.