aderemi/multiple-rows-processor

Laravel 多行操作,支持 XML、JSON、CSV 或纯文本

dev-staging 2018-11-10 12:33 UTC

This package is auto-updated.

Last update: 2024-09-11 02:11:33 UTC


README

Laravel 多行处理器是一个用于 Laravel 的包,用于通过 CSV 文件在数据库级别管理多记录处理。这使得通过 CSV、XML、JSON 和纯文本文件进行多次上传的应用程序更容易维护。

安装

从您的终端运行以下命令

composer require "Aderemi/Multiple-Rows-Processor: ^1.0.0"

或者在您的 composer.json 文件的 require 部分添加此内容

"Aderemi/Multiple-Rows-Processor: ^1.0.0"

然后运行 composer update

使用方法

首先,创建您的 Sheet 类。注意,您的 sheet 类必须扩展 MultipleRows\Contract\MultipleRows 并实现四个方法:rule(string $method, array $data)getUniqueIDField()processor()model()。您还必须定义 5 个常量,它们是您表单处理表头的标识符。

<?php
namespace MultipleRows\Tests;


use MultipleRows\Contract\MultipleRows;

class TestSheet extends MultipleRows
{
    const CREATE_HEADER = ['unique_test_id', 'name', 'status'];
    const UPDATE_HEADER = ['unique_test_id'];
    const DELETE_HEADER = ['unique_test_id', 'name'];
    const STATUS_CHANGE_HEADER = ['unique_test_id'];
    const UNIQUE_FIELDS = ['unique_test_id', 'name'];

    /**
     * @param string $method
     * @param array $data
     * @return array
     */
    protected function rules(string $method, array $data): array
    {
        switch($method){
            case "POST":
                return [
                    'unique_test_id' => "required|unique:" . TestModel::getTableName(),
                ];
            case "PUT" :
                return [
                    'unique_test_id' => "required|exists:" . TestModel::getTableName(),
                ];
        }
    }

    /**
     * @return string
     */
    public function getUniqueIDField(): string
    {
        return 'unique_test_id';
    }

    /**
     * @return string
     */
    protected function model()
    {
        return new TestModel();
    }

    /**
     * @return string
     */
    protected function processor()
    {
        return new TestProcessor();
    }
}
?>
php```
Create the process which will extend MultipleRows\Behaviour\Processor

```php
<?php
use MultipleRows\Behaviour\Processor;

class TestProcessor extends Processor
{

}
?>

php```

## Available Methods

The following methods are available:

##### MultipleRows\Behaviour

```php

以下方法可用:

MultipleRows\Behaviour\Processor
public function beforeCreate(array $data) // If you over-ride this method it means you want to handle your create by yourself
public function beforeUpdate(array $data) // If you over-ride this method it means you want to handle your update by yourself

当前支持的 JSON 格式是:

// 1
{
  "header" : ["sku", "name", "price"],
  "body" : [
    ["394AG", "Tomatoes", 300],
    ["344AG", "Big Tomatoes", 500]
  ]
}
// 2
[
{
  "sku"   : "324AF",
  "name"  : "Tin Tomatoes",
  "price" : 100
},
{
  "sku"   : "334AF",
  "name"  : "Sachet Tomatoes",
  "price" : 70
},
{
  "sku"   : "324AF",
  "name"  : "Mashed Tomatoes",
  "price" : 400
}
]
// and 3
[
  ["sku", "name", "price"],
  ["321GE", "Pepper", 320],
  ["323GE", "Seasoning", 20]
]  

XML 正在建设中,仍在编辑中...[仍在进行中]