Skip to content
A Doctrine Collection Bridge to work with League CSV
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github
src
tests
.editorconfig
.gitattributes
.gitignore
.php_cs
.travis.yml
LICENSE
README.md
composer.json
phpstan.src.neon
phpstan.tests.neon
phpunit.xml

README.md

League CSV - Doctrine Collection Bridge

Latest Version Software License Build Status

This package contains:

<?php

use Bakame\Csv\Extension\RecordCollection;
use Doctrine\Common\Collections\Criteria;
use League\Csv\Reader;
use function Bakame\Csv\Extension\criteria_convert;

$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'])
    ->setFirstResult(0)
    ->setMaxResults(10)
;

//you can do

$resultset = criteria_convert($criteria)->process($csv);
$result = new RecordCollection($resultset);

//or

$collection = new RecordCollection($csv);
$result = $collection->matching($criteria);

System Requirements

You need:

  • PHP >= 7.1 but the latest stable version of PHP is recommended.

Installation

$ composer require bakame/league-csv-doctrine-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 Doctrine\Common\Collections\Criteria;
use League\Csv\Reader;
use function Bakame\Csv\Extension\criteria_convert;

$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 = criteria_convert($criteria);
$resultset = $stmt->process($csv);

CriteriaConverter advanced usages

The Bakame\Csv\Extension\criteria_convert function is an alias of the CriteriaConverter::convert method.

<?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 the Criteria object into a Statement object.
  • CriteriaConverter::addWhere adds the Criteria::getWhereExpression filters to the submitted Statement object.
  • CriteriaConverter::addOrderBy adds the Criteria::getOrderings filters to the submitted Statement object.
  • CriteriaConverter::addInterval adds the Criteria::getFirstResult and Criteria::getMaxResults filters to the submitted Statement 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.

You can’t perform that action at this time.