soluti/data-exporter-bundle

A bundle that provides common interface for exporting data to common formats

dev-master 2017-01-24 09:12 UTC

This package is not auto-updated.

Last update: 2021-10-11 00:07:23 UTC


README

A bundle that provides common interface for exporting data to common formats.

For now it supports only CSV. More adapter may be added later.

Installation

A) Download and install

To install SolutiDataExporterBundle run the following command

$ php composer.phar require soluti/data-exporter-bundle

B) Enable the bundle

Enable the required bundles in the kernel:

<?php
// app/AppKernel.php

public function registerBundles()
{
    ...
    new Soluti\DataExporterBundle\SolutiDataExporterBundle(),
    ...
}

Usage

1. Define a mapper for your data

namespace AppBundle\Export;

use AppBundle\Model\Client;
use Soluti\DataExporterBundle\Mapper\MapperInterface;

class UserMapper implements MapperInterface
{
    public function getFileName()
    {
        return 'users' . '_' . date('Y-m-d_H-i');
    }

    /**
     * @param User $user
     * @return array
     */
    public function map($user)
    {
        return [
            'id' => $user->getId(),
            'firstName' => $user->getFirstName(),
            'lastName' => $user->getLastName(),
            'email' => $user->getEmail(),
        ];
    }
}

2. Create a response in your controller

    public function exportAction()
    {
        $usersIterator = []; // Some iterable result

        $exporter = $this->get('soluti.data_exporter.adapter.csv');

        return $exporter->export($usersIterator, new UserMapper());
    }