bakame / csv-doctrine-collection-bridge
Csv Doctrine Collection bridge using League\Csv
Fund package maintenance!
nyamsprod
Requires
- php: >=7.2
- doctrine/collections: ^1.5
- league/csv: ^9.6
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- phpstan/phpstan: ^0.12
- phpstan/phpstan-phpunit: ^0.12
- phpstan/phpstan-strict-rules: ^0.12
- phpunit/phpunit: ^9.2
This package is auto-updated.
Last update: 2024-10-18 12:12:34 UTC
README
This package contains:
- a class to convert League Csv objects into Doctrine Collections objects.
- a class to enable using Doctrine Collections powerful Expression API on League Csv objects.
<?php use Bakame\Csv\Extension as CsvExtension; use Doctrine\Common\Collections\Criteria; use League\Csv\Reader; $csv = Reader::createFromPath('/path/to/my/file.csv'); $csv->setHeaderOffset(0); $csv->setDelimiter(';'); $criteria = Criteria::create() ->andWhere(Criteria::expr()->eq('prenom', 'Adam')) ->orderBy( [ 'annee' => 'ASC', 'foo' => 'desc', ] ) ->setFirstResult(0) ->setMaxResults(10) ; //you can do $resultset = CsvExtension\CriteriaConverter::convert($criteria)->process($csv); $result = new CsvExtension\RecordCollection($resultset); //or $collection = new CsvExtension\RecordCollection($csv); $result = $collection->matching($criteria);
System Requirements
- league/csv >= 9.6
- doctrine/collection >= 1.6.0
but the latest stable version of each dependency is recommended.
Installation
$ composer require bakame/csv-doctrine-collection-bridge
Usage
Converting a League\Csv\Reader
into a Doctrine Collection object.
<?php use Bakame\Csv\Extension\RecordCollection; use League\Csv\Reader; $csv = Reader::createFromPath('/path/to/my/file.csv'); $csv->setHeaderOffset(0); $csv->setDelimiter(';'); $collection = new RecordCollection($csv);
Converting a League\Csv\ResultSet
into a Doctrine Collection object.
<?php $csv = Reader::createFromPath('/path/to/my/file.csv'); $csv->setHeaderOffset(0); $csv->setDelimiter(';'); $stmt = (new Statement()) ->where(function (array $row) { return isset($row['email']) && false !== strpos($row['email'], '@github.com'); }); $collection = new RecordCollection($stmt->process($csv));
Using Doctrine Criteria to filter a League\Csv\Reader
object
You can simply use the provided Bakame\Csv\Extension\criteria_convert
function to convert a Doctrine\Common\Collections\Criteria
object into a League\Csv\Statement
one.
<?php use Bakame\Csv\Extension\CriteriaConverter; use Doctrine\Common\Collections\Criteria; use League\Csv\Reader; $csv = Reader::createFromPath('/path/to/my/file.csv'); $csv->setHeaderOffset(0); $csv->setDelimiter(';'); $criteria = Criteria::create() ->andWhere(Criteria::expr()->eq('name', 'Adam')) ->orderBy(['years', 'ASC']) ->setFirstResult(0) ->setMaxResults(10) ; $stmt = CriteriaConverter::convert($criteria); $resultset = $stmt->process($csv);
CriteriaConverter advanced usages
<?php use Doctrine\Common\Collections\Criteria; use League\Csv\Statement; public static CriteriaConverter::convert(Criteria $criteria, Statement $stmt = null): Statement public static CriteriaConverter::addWhere(Criteria $criteria, Statement $stmt = null): Statement public static CriteriaConverter::addOrderBy(Criteria $criteria, Statement $stmt = null): Statement public static CriteriaConverter::addInterval(Criteria $criteria, Statement $stmt = null): Statement
CriteriaConverter::convert
converts theCriteria
object into aStatement
object.CriteriaConverter::addWhere
adds theCriteria::getWhereExpression
filters to the submittedStatement
object.CriteriaConverter::addOrderBy
adds theCriteria::getOrderings
filters to the submittedStatement
object.CriteriaConverter::addInterval
adds theCriteria::getFirstResult
andCriteria::getMaxResults
filters to the submittedStatement
object.
WARNING: While the Criteria
object is mutable the Statement
object is immutable. All returned Statement
objects are new instances
Contributing
Contributions are welcome and will be fully credited. Please see CONTRIBUTING for details.
Testing
The library has a :
- 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
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 File for more information.