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);
- league/csv >= 9.6
- doctrine/collection >= 1.6.0
but the latest stable version of each dependency is recommended.
$ composer require bakame/csv-doctrine-collection-bridge
<?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);
<?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));
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);
<?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
Contributions are welcome and will be fully credited. Please see CONTRIBUTING for details.
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
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.