Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/miquido/csv-file-reader
The project was made by Miquido. https://www.miquido.com/
https://github.com/miquido/csv-file-reader
Last synced: 3 months ago
JSON representation
The project was made by Miquido. https://www.miquido.com/
- Host: GitHub
- URL: https://github.com/miquido/csv-file-reader
- Owner: miquido
- License: mit
- Created: 2018-08-13T14:09:15.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-10-05T10:11:25.000Z (over 6 years ago)
- Last Synced: 2024-04-23T03:34:30.777Z (9 months ago)
- Language: PHP
- Homepage:
- Size: 46.9 KB
- Stars: 5
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build](https://travis-ci.org/miquido/csv-file-reader.svg?branch=master)](https://travis-ci.org/miquido/csv-file-reader)
[![Maintainability](https://api.codeclimate.com/v1/badges/d7b1addb4a14eab9cb48/maintainability)](https://codeclimate.com/github/miquido/csv-file-reader/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/d7b1addb4a14eab9cb48/test_coverage)](https://codeclimate.com/github/miquido/csv-file-reader/test_coverage)
[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php)# CSV File Reader
- [Installation guide](#installation)
- [Code Samples](#code-samples)
- [Contributing](#contributing)## Installation
Use [Composer](https://getcomposer.org) to install the package:```shell
composer require miquido/csv-file-reader
```## Code Samples
- [Simple read a csv file example](#simple-read-a-csv-file-example)
- [Process a file as a stream](#process-a-file-as-a-stream)
- [Using data transformer and error handler](#using-data-transformer)
- [Batch processing](#batch-processing)### Simple read a csv file example
```php
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](https://github.com/miquido/observable) library for data processing.
```php
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](https://github.com/miquido/data-structure) library for more details about classes used in examples below.
```php
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
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
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*](https://github.com/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.