bakame / psr7-csv-factory
a factory to return league csv object from PSR-7 StreamInterface
Installs: 12 141
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: >=7.3
- psr/http-message: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- league/csv: ^9.7.0
- nyholm/psr7: ^1.4
- phpstan/phpstan: ^0.12.10
- phpstan/phpstan-phpunit: ^0.12.21
- phpstan/phpstan-strict-rules: ^0.12.10
- phpunit/phpunit: ^9.5.0
This package is auto-updated.
Last update: 2024-11-04 21:30:54 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.3 but the latest stable version of PHP is recommended.
Installation
Install bakame/psr7-adapter
using Composer.
$ composer require bakame/psr7-adapter
Documentation
StreamWrapper::streamToResource
StreamWrapper::streamToAppendableResource
<?php use Psr\Http\Message\StreamInterface; use Bakame\Psr7\Adapter\StreamWrapper; public static StreamWrapper::streamToResource(StreamInterface $stream): resource; public static StreamWrapper::streamToAppendableResource(StreamInterface $stream): resource;
returns a PHP stream resource from a PSR-7 StreamInterface
object.
Parameters
$stream
: a object implementing PSR-7StreamInterface
interface.
Returned values
A PHP stream resource
Exception
A Bakame\Psr7\Adapter\UnableToWrapStream
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
.
<?php use Bakame\Psr7\Adapter\StreamWrapper; use League\Csv\Reader; use League\Csv\Writer; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; final class CsvDelimiterSwitcherAction { public function __construct( private string $inputDelimiter, private string $outputDelimiter ) { } public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface { //let's create a CSV Reader object from the submitted file $inputCsv = $request->getUploadedFiles()['csv']; $reader = Reader::createFromStream( StreamWrapper::streamToResource($inputCsv->getStream()) ); $reader->setDelimiter($inputDelimiter); $psr7stream = $response->getBody(); $psr7stream->write($reader->getInputBOM()); //let's create a CSV Writer object from the response body //because we already wrote to the stream we need to have an appendable resource $writer = Writer::createFromStream( StreamWrapper::streamToAppendableResource($psr7stream); ); //we convert the delimiter $writer->setDelimiter($outputDelimiter); $writer->insertAll($reader); //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-'.(new DateTimeImmutable())->format('Ymdhis').'.csv') ; } }
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
The package has:
- a PHPUnit test suite
- a coding style compliance test suite using PHP CS Fixer.
- a code analysis compliance test suite using PHPStan.
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.