kematjaya / import-bundle
import from phpspreadsheet data to database
Installs: 1 674
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- doctrine/collections: ^1.6
- doctrine/orm: ^2.7
- phpoffice/phpspreadsheet: ^1.15
- symfony/config: ^4.0|^5.0
- symfony/dependency-injection: ^4.0|^5.0
- symfony/filesystem: ^4.0|^5.0
- symfony/form: ^5.4
- symfony/http-client: ^4.0|^5.0
- symfony/http-kernel: ^4.0|^5.0
Requires (Dev)
- phpstan/phpstan: ^0.12.64
- phpunit/phpunit: ^9.0
- symfony/var-dumper: ^4.0|^5.0
README
- run in console
composer require kematjaya/import-bundle
config
- add to bundles.php
// config/bundles.php
....
Kematjaya\ImportBundle\ImportBundle::class => ['all' => true]
....
using service
- on controller
...
use Kematjaya\ImportBundle\Manager\ImportManagerInterface;
...
public function import(ImportManagerInterface $importManager)
{
$source = new YourDataSource();
$transformer = new YourTransformer();
$manager->->process($source, $transformer);
}
data source
...
use Kematjaya\ImportBundle\DataSource\RemoteDataSource;
use Kematjaya\ImportBundle\DataSource\SpreadSheetDataSource;
...
...
$remoteSource = new RemoteDataSource('https://jsonplaceholder.typicode.com/posts'); // from remote source
$excelSource = new SpreadSheetDataSource('D://test.xlsx'); // from excel file
...
data transformer
- create data transformer class
// src/DataTransformer/PostDataTransformer
...
use App\Entity\Post;
use Kematjaya\ImportBundle\DataTransformer\AbstractDataTransformer;
...
class PostDataTrasnformer extends AbstractDataTransformer
{
public function fromArray(array $data)
{
$datas = $this->checkConstraints($data);
$entity = (new Post())
->setId($datas['id'])
->setUserId($datas['userId'])
->setTitle($datas['title'])
->setBody($datas['body']);
return $entity;
}
protected function getColumns(): array
{
return [
[
self::KEY_FIELD => 'id', // key in TargetObject
self::KEY_INDEX => 0, // key in array source
self::KEY_CONSTRAINT => [
self::CONSTRAINT_REQUIRED => true // for required data
self::CONSTRAINT_REFERENCE_CLASS => User::class // relation class
self::CONSTRAINT_REFERENCE_FIELD => 'code' // field in reference class
]
]
];
}
}