decss/item-parser

解析带有参数或选项的CSV中的项目/产品。以HTML表格形式显示结果

1.2.0 2021-08-25 22:07 UTC

This package is auto-updated.

Last update: 2024-09-26 04:54:00 UTC


README

ItemParser是一个简单的PHP类,用于从CSV中解析产品和其它记录及其参数或选项(如颜色、尺寸等),结果以数组形式呈现或以HTML表格形式显示

ItemParser Preview

查看实时演示

examples文件夹中查看使用示例

功能

解析器功能

  • 从csv解析数据到数组
  • 以表格视图显示解析结果
  • 解析参数如sizecolormaterialcategory等,从单元格如S; M; L; XL到数组[id => 1, value => "S"]
  • 检测缺失参数,并允许替换或忽略它
  • 配置每列的类型、顺序或跳过它
  • 按值或别名搜索参数
  • 跳过指定的行或列

抽屉功能

  • 手动选择、更改或跳过每列
  • 将参数显示为标签
  • 将标签标记为忽略替换未找到
  • 将单元格标记为有效无效
  • 缩短链接和图像URL
  • 缩短长文本
  • 隐藏有效、无效或自定义的行

需求

composer.json中所述的生产需求

  • php >= 5.5
  • php mbstring 扩展
  • php json 扩展
  • php iconv 扩展
  • parsecsv/php-parsecsv

安装

使用Composer,在命令行运行以下命令

composer require decss/item-parser

在您的PHP脚本中包含Composer的自动加载文件

require_once __DIR__ . '/vendor/autoload.php';

不使用Composer

不推荐。要使用ItemParser,您必须添加一行require 'itemparser.lib.php';。注意,您将需要引入所有ItemParser依赖项,如ParseCsv

解析结果

解析结果是行的数组(行)。每一行对应于CSV中的相应行,通常如下所示

0 => [
    "row"    => 1,          // line number in CSV
    "valid"  => true,       // true if all row's Fields is valid, false if any is invalid
    "skip"   => false,      // true only if you skip this row by special method
    "fields" => []          // array of row fields (cells)
] 

跳过的行可以是有效的("valid" => true)或无效的("valid" => false),反之亦然。

如上所述,"fields"是Field项目的数组。每个Field可以不同,具体取决于其类型、配置和内容。所有行字段都会在结果中呈现,即使Field未解析或跳过或无效也是如此 - 没有关系。

空字段

这是一个跳过或未配置的Field的示例

14 => [
    "text"  => "cell text", // Original CSV cell text
    "name"  => null,        // Field name from Parser Fields config
    "type"  => null         // Field type
]

文本字段

所以这里有2种Field类型:textparam。下面是配置的textField的示例

1 => [
    "text"  => "V_3J689910",
    "name"  => "item_sku",
    "type"  => "text",
    "valid" => true,        // true if Field is not required or required and have valid "value"
    "value" => "V_3J689910" // Unlike "text", "value" is the processed value of a cell.
]

"value" - 是您应该用"text"代替的内容

参数字段和值

接下来是"param" Field

3 => [
    "text" => "Black; Not a color; Grey; ",
    "name" => "item_color",
    "type" => "param",
    "valid" => false,
    "value" => [
        0 => [
            "valid" => true,    // true if param was found in Field params
            "skip" => false,    // true if this value was skipped in Field missings config
            "replace" => false, // true if this value was replaced in Field missings config
            "id" => 1,          // Param ID, if it's value was found by in Field params
            "value" => "Black", // Param or Replaced param value
            "text" => "Black"   // Param text extracted from cell text value
        ],
        1 => [
            "valid" => false,
            "skip" => false,
            "replace" => false,
            "id" => null,
            "value" => null,
            "text" => "Not a color"
        ],
        2 => ["valid" => true, "skip" => false, "replace" => false, "id" => 3, "value" => "Grey", "text" => "Grey"]
    ]
]

所以您可以看到,param Field的"value"是一个数组。下面是找到的[0,2]和未找到的[1]颜色的示例。如果有2个或更多相同的颜色(例如"Black; Red; Black"),所有这些颜色都将是有效的,但重复的颜色将被跳过。

使用方法

解析器使用方法

use ItemParser\Parser;

// 1. Init Parser and set CSV file path
$csvPath = 'file.csv';
$parser = new Parser($csvPath);

// 2. Config columns
$parser->textField('item_name')->required();
$parser->textField('item_sku')->required();
$parser->textField('item_price')->required();
$parser->textField('item_link');
$parser->textField('item_image1');
$parser->textField('item_image2');
// 2.1 Config param column
// Param array
$colors = [
    ['id' => 1, 'value' => 'Red'],
    ['id' => 2, 'value' => 'Green'],
    ['id' => 3, 'value' => 'Blue'],
    ['id' => 4, 'value' => 'Gold', 'alias' => ['Gold sand', 'Golden-Orange']],
];
// Param Missing - skip or replace colors, that was not found in $colors
$colorsMissing = [
    'Orange' => -1, //  Skip this color
    'Golden' => 4,  //  Replace "Golden" to "Gold" (id = 4)
];
$parser->paramField('item_color', [$colors, $colorsMissing])->required();

// 3. Run parse and get results
$result = $parser->parse();

抽屉使用方法

use ItemParser\Drawer;

// Create Drawer and config columns (optional)
$drawer = new Drawer($parser, [
    'item_name' => ['title' => 'Product Name'],
    'item_link' => ['display' => 'link'],
    'item_image1' => ['display' => 'image'],
]);

// Display results
echo '<table class="parse-table">'
    . '<thead>' . $drawer->head() . '</thead>'
    . '<tbody>' . $drawer->body() . '</tbody>'
    . '</table>';

详细使用方法

创建解析器并设置内容

// Set CSV file path
$parser = new Parser('file.csv');
// or
$parser = new Parser;
$parser->setCsvPath('file.csv');
// also you can use preconfigured parseCsv class
$parseCsv = new \ParseCsv\Csv();
$parseCsv->limit = 5;
$parseCsv->delimiter = "\t";
$parser = new Parser('file.csv', $parseCsv);

// Set SCV content
$parser = new Parser;
$content = file_get_contents('file.csv');
$parser->setCsvContent($content);

您可以访问到Csv()实例的ParseCsv库,并直接进行配置

$csvObj = $parser->getCsvObj();
$csvObj->delimiter = ';,';      // Set CSV rows delimiter characters

配置列

// Add text field
$parser->textField('column_name');
// Add required text field
$parser->textField('column2_name')->required();

// Add param field
$parser->paramField('item_size', [$sizes]);
// Add required param field with missing colors and set possible delimiters for params
$parser->paramField('item_color', [$colors, $colorsMissing])->required()->delimiters([';', ',', '/']);

查看示例以了解类似 $colors$colorsMissing 的参数如何工作

配置解析器选项

// Skip first 2 rows
$parser->skipRows([0,1]);

// Skip columns and set order
$parser->fieldsOrder([
    0 => 'item_name',
    1 => 'item_sku',
    2 => 'item_price',
    3 => 'item_color',
    4 => 'item_size',
    // 5, 6 - skip
    7 => 'item_material',
    8 => 'item_desc',
    9 => 'item_link',
    10 => 'item_image1',
    11 => 'item_image2',
    12 => 'item_image3',
    // further will be skipped
]);

解析和结果

// Do parsing and get results
$result = $parser->parse();

// Get results after parsing
$result = $parser->result();

使用抽屉(Drawer)

// Create Drawer and config it
$drawer = new Drawer($parser, [
    'item_name' => ['title' => 'Product Name'],
    'item_sku' => ['title' => 'Product SKU'],
    'item_price' => ['title' => 'Price'],
    'item_size' => ['title' => 'Sizes'],
    'item_color' => ['title' => 'Colors'],
    'item_desc' => ['title' => 'Description', 'display' => 'text'],
    'item_link' => ['display' => 'link'],
    'item_image1' => ['display' => 'image'],
    'item_image2' => ['display' => 'image'],
    'item_image3' => ['display' => 'image'],
]);

// Hide valid rows
$drawer->hideValid();
// Hide invalid rows
$drawer->hideInvalid();
// Hide custom rows
drawer->hideRows([0, 6, 7, 8]);

// Display missing selects
echo $drawer->missing();

// Display table column names
echo $drawer->head();
// Display table column names with Field selects
echo $drawer->head('select');

// Display table rows
echo $drawer->body();

致谢