Giter VIP home page Giter VIP logo

csv's Introduction

csv

A package with classes to manage reading and writing of csv files.

Installation

Add to your project using composer:

composer require talkingbit/csv

Basic usage

Reader

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

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);

Configuration

You can customize delimiters and enclosure characters:

$writer
    ->toFile('/path/to/file.csv')
    ->withDelimiter(',')
    ->withEnclosure('"')
    ->writeRow($dto);

Custom Mappers for Reader

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.

Contribute

Feel free to open issues or pull requests.

csv's People

Contributors

franiglesias avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

csv's Issues

New Generic mappers

Add new generic mappers to the package:

  • Mapper able to detect numeric/date/array values
  • Mapper with validation rules

Is it possible some sort of composable adapters?

Use Adapters for file access

talkingbit/csv read and write to local file system. It could be great if we can avoid this coupling allowing to use different adapters even for separated reading and writing.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.