1. Go to this page and download the library: Download kunicmarko/importer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use KunicMarko\Importer\ImporterFactory;
use KunicMarko\Importer\Reader\CsvReader;
use KunicMarko\Importer\Reader\JsonReader;
use KunicMarko\Importer\Reader\XmlReader;
use KunicMarko\Importer\Reader\XlsxReader;
$importerFactory = new ImporterFactory();
$importerFactory->addReader(new CsvReader());
$importerFactory->addReader(new JsonReader());
$importerFactory->addReader(new XmlReader());
$importerFactory->addReader(new XlsxReader());
$importer = $importerFactory->getImporter('csv');
$importer->fromString('some,csv,string')
->useImportConfiguration(new YourImportConfiguration())
->import();
use KunicMarko\Importer\ImportConfiguration;
class ImportUserConfiguration implements ImportConfiguration
{
public function map(array $item, array $additionalData)
{
$user = new User();
$user->setUsername($item['username']);
//..
return $user;
}
public function save(array $items, array $additionalData): void
{
//save your users
}
}
use KunicMarko\Importer\ImportConfiguration;
use KunicMarko\Importer\BeforeImport;
use Iterator;
class ImportSomethingConfiguration implements ImportConfiguration, BeforeImport
{
public function before(Iterator $items, array $additionalData): Iterator
{
//start from 2nd line
$items->next();
return $items;
}
}
use KunicMarko\Importer\ImportConfiguration;
use KunicMarko\Importer\ChunkImport;
class ImportChunkSomethingConfiguration implements ImportConfiguration, ChunkImport
{
public function chunkSize(): int
{
return 50;
}
public function save(array $items, array $additionalData): void
{
//save will be called multiple times with 50 or less items
}
}
use KunicMarko\Importer\ImporterFactory;
class UserImport
{
private $importerFactory;
public function __construct(ImporterFactory $importerFactory)
{
$this->importerFactory = $importerFactory;
}
public function import()
{
$importer = $importerFactory->getImporter('csv');
$importer->fromFile('path/to/file.csv')
->useImportConfiguration(new YourImportConfiguration())
->import();
}
}
use KunicMarko\Importer\ImporterFactory;
class UserImport
{
private $importerFactory;
public function __construct(ImporterFactory $importerFactory)
{
$this->importerFactory = $importerFactory;
}
public function import()
{
$importer = $importerFactory->getImporter('csv');
$importer->fromString('some,csv,string')
->useImportConfiguration(new YourImportConfiguration())
->import();
}
}
use KunicMarko\Importer\ImporterFactory;
class UserImport
{
private $importerFactory;
public function __construct(ImporterFactory $importerFactory)
{
$this->importerFactory = $importerFactory;
}
public function import()
{
$importer = $importerFactory->getImporter('csv');
$importer->fromString('some,csv,string')
->useImportConfiguration(new YourImportConfiguration())
->withAdditionalData(['user' => 'kunicmarko20'])
->import();
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.