kunicmarko/importer

此包已被 废弃 并不再维护。未建议替代包。

能够处理不同文件类型的导入器。

0.2.0 2018-07-22 18:41 UTC

This package is auto-updated.

Last update: 2020-11-16 04:33:32 UTC


README

简化从多种文件类型(csv、json、xml、excel)的导入。

支持 Symfony、Lumen 和 Laravel。

PHP Version Latest Stable Version Latest Unstable Version

Build Status Coverage Status

文档

安装

1. 使用 Composer 添加依赖

composer require kunicmarko/importer

Symfony

在您的 config/bundles.php 中注册包

return [
    //...
    KunicMarko\Importer\Bridge\Symfony\ImporterBundle::class => ['all' => true],
];

默认情况下,禁用 excel 导入,安装 "phpoffice/phpspreadsheet" 以启用它。

Laravel

在您的 config/app.php 中注册服务提供者

providers' => [
    //...
    KunicMarko\Importer\Bridge\Laravel\ImporterServiceProvider::class,
],

默认情况下,禁用 excel 导入,安装 "phpoffice/phpspreadsheet" 以启用它。

Lumen

在您的 bootstrap/app.php 中注册服务提供者

$app->register(KunicMarko\Importer\Bridge\Lumen\ImporterServiceProvider::class);

默认情况下,禁用 excel 导入,安装 "phpoffice/phpspreadsheet" 以启用它。

无框架

将您想使用的读取器添加到工厂,并获取您的导入器

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();

如果您想使用 excel 导入,请安装 "phpoffice/phpspreadsheet"。

使用方法

导入配置

导入配置定义了数据如何映射以及数据如何保存。它们必须实现 KunicMarko\Importer\ImportConfiguration 接口。

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();
    }
}

Excel 导入不支持从字符串导入。

传递附加数据

有时您可能想将附加数据传递给您的导入配置。

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();
    }
}

扩展

您始终可以添加您自己的自定义读取器,只需实现 KunicMarko\Importer\Reader\Reader 接口,并在导入器工厂上调用 addReader() 方法。