bakame/psr7-csv-factory

a factory to return league csv object from PSR-7 StreamInterface

dev-master / 1.x-dev 2018-10-29 13:56 UTC

README

This package enables converting a PSR-7 StreamInterface objects into a PHP stream. This make it possible to work with functions and class which expect a PHP stream resource like League CSV object package.

The included StreamWrapper class is heavily inspired/copied from the excellent Guzzle/Psr7 package written by Michael Dowling which uses the MIT License

Requirements

You need PHP >= 7.0 but the latest stable version of PHP is recommended.

Installation

Install bakame/psr7-adapter using Composer.

$ composer require bakame/psr7-adapter

Documentation

resource_from

<?php

use Psr\Http\Message\StreamInterface;
use function Bakame\Psr7\Adapter\resource_from;

function resource_from(StreamInterface $stream, int $flag = 0): resource;

returns a PHP stream resource from a PSR-7 StreamInterface object.

Parameters

  • $stream : a object implementing PSR-7 StreamInterface interface.
  • $flag : FILE_APPEND flag from file_put_contents

Returned values

A PHP stream resource

Exception

A Bakame\Psr7\Adapter\Exception will be triggered when the following situations are encountered:

  • If the StreamInterface is not readable and writable
  • If the stream resource could not be created.

Usage example

Here's a simple usage with League\Csv and Slim\Framework.

<?php

use League\Csv\Reader;
use League\Csv\Writer;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\App;
use function Bakame\Psr7\Adapter\resource_from;

$app = new App();
$app->post('/csv-delimiter-converter', function (Request $request, Response $response): Response {

    //let's create a CSV Reader object from the submitted file
    $input_csv = $request->getUploadedFiles()['csv'];
    $csv = Reader::createFromStream(resource_from($input_csv->getStream()));
    $csv->setDelimiter(';');

    $flag = 0;
    $psr7stream = $response->getBody();
    if ('' !== $csv->getInputBOM()) {
        $flag = FILE_APPEND;
        $psr7stream->write($csv->getInputBOM());
    }

    //let's create a CSV Writer object from the response body
    $output = Writer::createFromStream(resource_from($psr7stream, $flag));
    //we convert the delimiter from ";" to "|"
    $output->setDelimiter('|');
    $output->insertAll($csv);

    //we add CSV header to enable downloading the converter document
    return $response
        ->withHeader('Content-Type', 'text/csv, charset=utf-8')
        ->withHeader('Content-Transfer-Encoding', 'binary')
        ->withHeader('Content-Description', 'File Transfer')
        ->withHeader('Content-Disposition', 'filename=csv-'.date_create()->format('Ymdhis').'.csv')
    ;
});

$app->run();

In both cases, the StreamInterface objects are never detached or removed from their parent objects (ie the Request object or the Response object), the CSV objects operate on their StreamInterface property using the adapter stream returned by resource_from.

Testing

A PHPUnit test suite and a coding style compliance test suite using PHP CS Fixer are avaiable. To run the tests, run the following command from the project folder.

$ composer test

Contributing

Contributions are welcome and will be fully credited. Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email nyamsprod@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see LICENSE for more information.