├── LICENSE ├── composer.json └── src ├── CriteriaConverter.php ├── RecordCollection.php ├── functions.php └── functions_include.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 ignace nyamagana butera 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bakame/csv-doctrine-collection-bridge", 3 | "type": "library", 4 | "description" : "Csv Doctrine Collection bridge using League\\Csv", 5 | "license": "MIT", 6 | "homepage": "https://github.com/bakame-php/csv-doctrine-collections-bridge", 7 | "authors": [ 8 | { 9 | "name" : "Ignace Nyamagana Butera", 10 | "email" : "nyamsprod@gmail.com", 11 | "homepage" : "https://nyamsprod.com" 12 | } 13 | ], 14 | "keywords": [ 15 | "csv", 16 | "collections", 17 | "iterator", 18 | "array", 19 | "criteria", 20 | "doctrine" 21 | ], 22 | "funding": [ 23 | { 24 | "type": "github", 25 | "url": "https://github.com/sponsors/nyamsprod" 26 | } 27 | ], 28 | "require": { 29 | "league/csv": "^9.6", 30 | "doctrine/collections": "^1.6" 31 | }, 32 | "require-dev": { 33 | "phpunit/phpunit": "^9.2", 34 | "friendsofphp/php-cs-fixer": "^2.16", 35 | "phpstan/phpstan": "^0.12", 36 | "phpstan/phpstan-strict-rules": "^0.12", 37 | "phpstan/phpstan-phpunit": "^0.12" 38 | }, 39 | "autoload": { 40 | "psr-4": { 41 | "Bakame\\Csv\\Extension\\" : "src" 42 | }, 43 | "files": ["src/functions_include.php"] 44 | }, 45 | "autoload-dev": { 46 | "psr-4": { 47 | "BakameTest\\Csv\\Extension\\": "tests" 48 | } 49 | }, 50 | "scripts": { 51 | "phpcs-dry": "php-cs-fixer fix -vvvv --diff --dry-run --allow-risky=yes;", 52 | "phpcs-fix": "php-cs-fixer fix -vvv --allow-risky=yes;", 53 | "phpstan": "phpstan analyse -l max -c phpstan.neon src", 54 | "phpunit": "phpunit --coverage-text", 55 | "test": [ 56 | "@phpcs-dry", 57 | "@phpstan", 58 | "@phpunit" 59 | ] 60 | }, 61 | "extra": { 62 | "branch-alias": { 63 | "dev-master": "1.x-dev" 64 | } 65 | }, 66 | "config": { 67 | "sort-packages": true 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/CriteriaConverter.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Bakame\Csv\Extension; 13 | 14 | use Doctrine\Common\Collections\Criteria; 15 | use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor; 16 | use League\Csv\Statement; 17 | use function array_reverse; 18 | 19 | final class CriteriaConverter 20 | { 21 | /** 22 | * Returns the Statement object created from the current Criteria object. 23 | * 24 | */ 25 | public static function convert(Criteria $criteria, Statement $stmt = null): Statement 26 | { 27 | $stmt = self::addWhere($criteria, $stmt); 28 | $stmt = self::addOrderBy($criteria, $stmt); 29 | 30 | return self::addInterval($criteria, $stmt); 31 | } 32 | 33 | /** 34 | * Returns a Statement instance with the Criteria::getWhereExpression filter. 35 | * 36 | * This method MUST retain the state of the Statement instance, and return 37 | * an new Statement instance with the added Criteria::getWhereExpression filter. 38 | * 39 | */ 40 | public static function addWhere(Criteria $criteria, Statement $stmt = null): Statement 41 | { 42 | $stmt = $stmt ?? new Statement(); 43 | $expr = $criteria->getWhereExpression(); 44 | if (null === $expr) { 45 | return $stmt; 46 | } 47 | 48 | return $stmt->where((new ClosureExpressionVisitor())->dispatch($expr)); 49 | } 50 | 51 | /** 52 | * Returns a Statement instance with the Criteria::getOrderings filter. 53 | * 54 | * This method MUST retain the state of the Statement instance, and return 55 | * an new Statement instance with the added Criteria::getOrderings filter. 56 | * 57 | */ 58 | public static function addOrderBy(Criteria $criteria, Statement $stmt = null): Statement 59 | { 60 | $next = null; 61 | foreach (array_reverse($criteria->getOrderings()) as $field => $ordering) { 62 | $next = ClosureExpressionVisitor::sortByField( 63 | $field, 64 | Criteria::DESC === $ordering ? -1 : 1, 65 | $next 66 | ); 67 | } 68 | 69 | $stmt = $stmt ?? new Statement(); 70 | if (null === $next) { 71 | return $stmt; 72 | } 73 | 74 | return $stmt->orderBy($next); 75 | } 76 | 77 | /** 78 | * Returns a Statement instance with the Criteria interval parameters. 79 | * 80 | * This method MUST retain the state of the Statement instance, and return 81 | * an new Statement instance with the added Criteria::getFirstResult 82 | * and Criteria::getMaxResults filters paramters. 83 | * 84 | */ 85 | public static function addInterval(Criteria $criteria, Statement $stmt = null): Statement 86 | { 87 | $offset = $criteria->getFirstResult() ?? 0; 88 | $length = $criteria->getMaxResults() ?? -1; 89 | $stmt = $stmt ?? new Statement(); 90 | 91 | return $stmt->offset($offset)->limit($length); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/RecordCollection.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Bakame\Csv\Extension; 13 | 14 | use Doctrine\Common\Collections\AbstractLazyCollection; 15 | use Doctrine\Common\Collections\ArrayCollection; 16 | use Doctrine\Common\Collections\Criteria; 17 | use Doctrine\Common\Collections\Selectable; 18 | use League\Csv\TabularDataReader; 19 | 20 | final class RecordCollection extends AbstractLazyCollection implements Selectable 21 | { 22 | /** 23 | * @var TabularDataReader 24 | */ 25 | private $tabularDataReader; 26 | 27 | public function __construct(TabularDataReader $tabularDataReader) 28 | { 29 | $this->tabularDataReader = $tabularDataReader; 30 | } 31 | 32 | /** 33 | * {@inheritDoc} 34 | */ 35 | protected function doInitialize(): void 36 | { 37 | $this->collection = new ArrayCollection(); 38 | foreach ($this->tabularDataReader as $offset => $record) { 39 | $this->collection[$offset] = $record; 40 | } 41 | unset($this->tabularDataReader); 42 | } 43 | 44 | /** 45 | * {@inheritDoc} 46 | */ 47 | public function matching(Criteria $criteria): ArrayCollection 48 | { 49 | $this->initialize(); 50 | 51 | /** @var ArrayCollection $collection */ 52 | $collection = $this->collection; 53 | 54 | /** @var ArrayCollection $newCollection */ 55 | $newCollection = $collection->matching($criteria); 56 | 57 | return $newCollection; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/functions.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | declare(strict_types=1); 13 | 14 | namespace Bakame\Csv\Extension; 15 | 16 | use Doctrine\Common\Collections\Criteria; 17 | use League\Csv\Statement; 18 | 19 | /** 20 | * Returns the Statement object created from the current Criteria object. 21 | * 22 | * @see CriteriaConverter::convert 23 | * 24 | */ 25 | function criteria_convert(Criteria $criteria, Statement $stmt = null): Statement 26 | { 27 | return CriteriaConverter::convert($criteria, $stmt); 28 | } 29 | -------------------------------------------------------------------------------- /src/functions_include.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | if (!function_exists('Bakame\Csv\Extension\criteria_convert')) { 13 | require __DIR__.'/functions.php'; 14 | } 15 | --------------------------------------------------------------------------------