fastbolt / entity-importer
A library for importing CSV files to doctrine entities
Installs: 1 953
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 3
Requires
- php: >=8.2
- ext-zlib: *
- doctrine/persistence: ^2.4|^3.0
- guzzlehttp/guzzle: ^7.5
- phpoffice/phpspreadsheet: ^2.1
- portphp/csv: ^2.0
- symfony/config: ^6.0
- symfony/filesystem: ^6.0
- symfony/framework-bundle: ^6.0
- symfony/string: ^6.0
- webmozart/assert: ^1.11
Requires (Dev)
- fastbolt/test-helpers: ^0.1.1
- fig-r/psr2r-sniffer: ^1.3
- phpmd/phpmd: ^2.11
- phpstan/phpstan: ^1.4
- phpunit/phpunit: ^9.5
- psalm/plugin-phpunit: ^0.18.4
- psalm/plugin-symfony: ^v5.0
- slevomat/coding-standard: ^7.0
- spryker/code-sniffer: ^0.17.4
- squizlabs/php_codesniffer: ^3.6
- vimeo/psalm: ^5.6
- weirdan/doctrine-psalm-plugin: ^v2.8
README
Entity importing library
This library aims to provide an easy way to import files into doctrine entities.
Prerequisites
For now, the bundle is tested using PHP 7.4, 8.0 and 8.1.
Internally, we rely on PortPHP and Doctrine
Installation
The library can be installed via composer:
composer require fastbolt/entity-importer
Configuration
If not configured automatically, the bundle needs to be enabled in your project's bundles.php
file:
<?php return [ Fastbolt\EntityImporter\EntityImporterBundle::class => ['all' => true], ];
Implementation
The only necessary code for new importers is the Fastbolt\EntityImporter\EntityImporterDefinition
implementation. We recommend
extending the abstract Fastbolt\EntityImporter\AbstractEntityImporterDefinition
class.
Currently, two import sources are available: Csv and Xlsx.
Usage
After implementing the above interface, the console command entity_importer:import
will automatically recognize the new implementation.
When executing the command without any arguments, it will display all available implementations:
Implementation example:
<?php namespace App\Component\Data\Import\EntityImporter; use App\Entity\Branch;use App\Entity\Material;use App\Entity\MaterialGroup;use App\Repository\BranchRepository;use App\Repository\MaterialGroupRepository;use App\Repository\MaterialRepository;use DateTime;use Doctrine\Persistence\ObjectRepository;use Fastbolt\EntityImporter\AbstractEntityImporterDefinition;use Fastbolt\EntityImporter\Reader\CsvReader;use Fastbolt\EntityImporter\Types\ImportSourceDefinition\Csv; /** * @template-implements Material */ class MaterialImporterDefinition extends AbstractEntityImporterDefinition { /** * @var Csv */ private Csv $importSourceDefinition; /** * @var MaterialRepository */ private MaterialRepository $repository; /** * @var MaterialGroupRepository */ private MaterialGroupRepository $materialGroupRepository; /** * @var BranchRepository */ private BranchRepository $branchRepository; /** * @param MaterialRepository $repository * @param MaterialGroupRepository $materialGroupRepository * @param BranchRepository $branchRepository */ public function __construct( MaterialRepository $repository, MaterialGroupRepository $materialGroupRepository, BranchRepository $branchRepository ) { $this->repository = $repository; $this->materialGroupRepository = $materialGroupRepository; $this->branchRepository = $branchRepository; $this->importSourceDefinition = new Csv('MDMATERIALS.csv', CsvReader::TYPE); } /** * @inheritDoc */ public function getName(): string { return 'materials'; } /** * @inheritDoc */ public function getDescription(): string { return 'Importer for Materials'; } /** * @inheritDoc */ public function getEntityClass(): string { return Material::class; } /** * @inheritDoc */ public function getIdentifierColumns(): array { return ['materialNumber', 'branch']; } /** * @inheritDoc */ public function getFields(): array { return [ 'materialGroup', 'branch', 'materialNumber', 'shortMaterialNumber', 'purchasingTextDe', 'purchasingTextEn', 'priceGroup', 'priceUnit', 'packingUnit', 'weight', 'purchasingAbcMark', 'language', 'availableSince', 'customsTariffNumber', 'ean', 'isOnlineMaterial', 'rangeIndicator', ]; } /** * @inheritDoc */ public function getImportSourceDefinition(): Csv { return $this->importSourceDefinition; } /** * @inheritDoc */ public function getRepository(): ObjectRepository { return $this->repository; } /** * @inheritDoc */ public function getFieldConverters(): array { return [ 'branch' => function ($value): Branch { return $this->branchRepository->find($value); }, 'materialGroup' => function ($value): MaterialGroup { return $this->materialGroupRepository->findOneBy(['name' => $value]); }, 'priceUnit' => static function ($value): int { return (int)trim($value); }, 'availableSince' => static function ($value): ?DateTime { if ('00000000' === $value) { return null; } return DateTime::createFromFormat('Ymd', $value); }, 'packingUnit' => static function ($value): int { return (int)trim($value); }, 'weight' => static function ($value): float { return (float)trim(str_replace(',', '', $value)); }, ]; } /** * @inheritDoc */ public function getSkippedFields(): array { return [ 'language', ]; } /** * @inheritDoc */ public function getEntityModifier(): ?callable { return static function (Material $material) { $material->setIsProtected(true); }; } }