定制 fastbolt/entity-importer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fastbolt/entity-importer

最新稳定版本:v1.1.1

Composer 安装命令:

composer require fastbolt/entity-importer

包简介

A library for importing CSV files to doctrine entities

README 文档

README

Composer version

Code Climate maintainability Test Coverage

Type Coverage Psalm Level

Github Build

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:

Import command, overview of available importers

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

统计信息

  • 总下载量: 3.12k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-03-18