2017 © Pedro Peláez
 

library csvme

An opinionated library to make exporting data in CSV format super simple.

image

netsells/csvme

An opinionated library to make exporting data in CSV format super simple.

  • Sunday, August 6, 2017
  • by craigsansam
  • Repository
  • 1 Watchers
  • 2 Stars
  • 216 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 123 % Grown

The README.md

Csvme

Latest Version Software License Total Downloads, (*1)

Csvme is an opinionated library that utilises the league/csv library., (*2)

It is created and maintained by the Netsells team, (*3)

Install

Install Csvme using Composer., (*4)

$ composer require netsells/csvme

Usage

Basic Usage

Csvme always expects an array of objects and optionally a layout closure for the header row., (*5)

$csv = new Csvme();

$csv->withHeader(['ID', 'Total', 'Number of Items', 'Created At'])
    ->withLayout(function(Order $order) {
        return [
            $order->id,
            $order->total,
            $order->items->count(),
            $order->created_at->format('d-m-Y'),
        ];
    })
    ->withItems($orders)
    ->output();

CSV Composers

It is possible to use an external class to offload the layout of the CSV to a dedicated file., (*6)

$csv = new Csvme();
$csv->output(new OrderExportComposer($orders));
<?php

use Netsells\Csvme\Csvme;
use Netsells\Csvme\CsvComposer;

class OrderExportComposer implements CsvComposer
{
    /**
     * The orders.
     *
     * @var array
     */
    protected $orders;

    /**
     * Create a new csv composer.
     *
     * @param  array  $orders
     * @return void
     */
    public function __construct($orders)
    {
        $this->orders = $orders;
    }

    /**
     * Configure the CSV
     *
     * @param  Csvme  $csv
     * @return void
     */
    public function compose(Csvme $csv)
    {
        $csv->withHeader(['ID', 'Total', 'Number of Items', 'Created At'])
            ->withLayout(function(Order $order) {
                return [
                    $order->id,
                    $order->total,
                    $order->items->count(),
                    $order->created_at->format('d-m-Y'),
                ];
            })
            ->withItems($this->orders);
    }

}

CORS Headers

When using Csvme in api endpoints, you can add the Access-Control-Allow-Origin header using the setCorsHeader() method. The method defaults to allow all origins but a specific origin can be passed in as an argument in the following way., (*7)

$csv = new Csvme();

$csv->withHeader(['ID', 'Total', 'Number of Items', 'Created At'])
    ->withLayout(function(Order $order) {
        return [
            $order->id,
            $order->total,
            $order->items->count(),
            $order->created_at->format('d-m-Y'),
        ];
    })
    ->withItems($orders)
    ->setCorsHeader('https://test.com')
    ->output();

The Versions

06/08 2017

dev-master

9999999-dev

An opinionated library to make exporting data in CSV format super simple.

  Sources   Download

MIT

The Requires

 

by Craig Sansam
by Sam Jordan

06/08 2017

1.0.0

1.0.0.0

An opinionated library to make exporting data in CSV format super simple.

  Sources   Download

MIT

The Requires

 

by Craig Sansam
by Sam Jordan