Skip to content

bakame-php/csv-doctrine-collections-bridge

Repository files navigation

League CSV - Doctrine Collection Bridge

Latest Version Software License Build Status

This package contains:

<?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 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.