kuborgh/csv-bundle

Converting an array to CSV and vice versa.

Installs: 6 312

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 3

Forks: 0

Open Issues: 2

Type:symfony-bundle

v1.0.1 2016-09-07 11:47 UTC

This package is not auto-updated.

Last update: 2024-11-09 19:28:52 UTC


README

[![Build Status][travis-image]][travis-url] [travis-url]: https://travis-ci.org/kuborgh/csv-bundle [travis-image]: https://secure.travis-ci.org/kuborgh/csv-bundle.svg?branch=master

This bundle is intended to be simple, but rfc4180 conform csv parser and writer. It will be extended in the future to fit more and more needs of custom implementations.

Installation

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

$ composer require kuborgh/csv-bundle

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the app/AppKernel.php file of your project:

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new Kuborgh\CsvBundle\KuborghCsvBundle(),
        );

        // ...
    }

    // ...
}

Configuration

Following configuration variables exist an can be inserted into the config.yml of your project

kuborgh_csv:
    parser:
        # Add here as many parser as you need. Each will get it's own service kuborgh_csv.parser.my_parser
        my_parser:
            # Delimiter (default: ",")
            delimiter: ","
            
            # Line Ending (default: "\r\n")
            line_ending: "\r\n"
            
            # Implementation for the parser. Possible values are "character" (default) oder "simple" (not recommended).
            # You can add your own parser implementation by registering the class name in parameters like  
            # kuborgh_csv.parser.<my_implementation>.class 
            implementation: character
    
    generator:
        # Add here as many generators as you need. Each will get it's own service kuborgh_csv.generator.my_generator
        my_generator:
            # Delimiter (default: ",")
            delimiter: ","
            
            # Line Ending (default: "\r\n")
            line_ending: "\r\n"
            
            # Implementation for the generator. Possible values are "string" (default) oder "php" (not recommended).
            # You can add your own generatr implementation by registering the class name in parameters like  
            # kuborgh_csv.generator.<my_implementation>.class 
            implementation: string

Usage

To parse CSV you simply call

$parser = $container->get('kuborgh_csv.parser.<my_parser>');
$array = $parser->parse($csv);

To generate CSV you simply call

$generator = $container->get('kuborgh_csv.generator.<my_generator>');
$csv = $generator->generate($array);

Testing

The whole parser should be unittested. You can run the tests with

$ composer install
$ bin/phpunit

The coverage report is saved in the coverage folder and should always cover 100%