miquido/csv-file-reader

v1.0.0 2018-10-05 10:09 UTC

This package is not auto-updated.

Last update: 2024-12-22 07:17:59 UTC


README

Build Maintainability Test Coverage MIT Licence

CSV File Reader

Installation

Use Composer to install the package:

composer require miquido/csv-file-reader

Code Samples

Simple read a csv file example

<?php

use Miquido\CsvFileReader\CsvFile;
use Miquido\CsvFileReader\Line\CsvLineInterface;

// open a file
$csv = new CsvFile('./examples/users.csv');

$count = $csv->countLines(); // 101

// don't worry about memory, it reads a file line-by-line 
foreach ($csv->readLines() as $line) {
    /** @var CsvLineInterface $line */
    $line->getLineNumber(); // 2 ... 101
    $line->getData(); // ['id' => '1', 'name' => 'Miriam', 'surname' => 'Mccoy', 'age' => '79'] ...
}

Process a file as a stream

Miquido\CsvFileReader\CsvFileReader class uses miquido/observable library for data processing.

<?php

use Miquido\CsvFileReader\CsvFile;
use Miquido\CsvFileReader\CsvFileReader;
use Miquido\CsvFileReader\Line\CsvLineInterface;

$reader = new CsvFileReader(new CsvFile('./examples/users.csv'));
$reader->lines()->subscribe(function (CsvLineInterface $line) {
    // do something with a line
    $line->getLineNumber(); // 2 ... 101
    $line->getData(); // ['id' => '1', 'name' => 'Miriam', 'surname' => 'Mccoy', 'age' => '79'] ...
});

$reader->loop(); // start reading a file

Using data transformer and error handler

Please check miquido/data-structure library for more details about classes used in examples below.

<?php

use Miquido\CsvFileReader\CsvFile;
use Miquido\CsvFileReader\CsvFileReader;
use Miquido\CsvFileReader\Line\CsvLineInterface;
use Miquido\DataStructure\Map\MapInterface;
use Miquido\DataStructure\Map\Map;

// change data to Map object
$transformer = function (array $data, int $line): MapInterface {
    return new Map($data);
};

$reader = new CsvFileReader(new CsvFile('./examples/users.csv'), $transformer);
$reader->lines()->subscribe(function (CsvLineInterface $line): void {
    $line->getData(); // getData() now returns Map() object
});

$reader->loop(); // start reading a file

If transformer throws an error, it will appear in $reader->errors() stream

<?php

use Miquido\CsvFileReader\CsvFile;
use Miquido\CsvFileReader\CsvFileReader;
use Miquido\CsvFileReader\Exception\InvalidCsvLineException;

// check user's age
$transformer = function (array $data, int $line): array {
    $age = (int) $data['age'];
    if ($age < 18) {
        throw new \Exception('Invalid age');
    }
    
    return $data;
};

$reader = new CsvFileReader(new CsvFile('./examples/users.csv'), $transformer);
$reader->data()->subscribe(function (array $lineData): void {
    // do something with data
});
$reader->errors()->subscribe(function (InvalidCsvLineException $e): void {
    // do something with an error
    $e->getMessage();
    $e->getCsvLine();
});

$reader->loop(); // start reading a file

Batch processing

Simply use Miquido\Observable\Operator:

<?php

use Miquido\CsvFileReader\CsvFile;
use Miquido\CsvFileReader\CsvFileReader;
use Miquido\Observable\Operator;

$batchSize = 10;

$reader = new CsvFileReader(new CsvFile('./examples/users.csv'));
$reader->lines()->pipe(new Operator\BufferCount($batchSize))->subscribe(function (array $lines): void {
    // do something with 10 lines
});

$reader->loop(); // start reading a file

See miquido/observable library for more operators.

Contributing

Pull requests, bug fixes and issue reports are welcome. Before proposing a change, please discuss your change by raising an issue.