dominservice/onix-parser

ONIX 3.0 解析器

1.0.1 2023-07-31 10:11 UTC

This package is auto-updated.

Last update: 2024-09-30 01:52:25 UTC


README

此包是一个简单的PHP库,用于读取通用格式短和参考的ONIX 3.0文件。各个单元中的辅助函数使得从数据记录中读取所需详细信息变得容易。不同格式的文本元素以及不同的ONIX日期格式将自动解析,因此可以轻松使用。

此包目前正在开发中。尽管大多数字段应该由库检测和解析,但一些字段仍需要更多工作。

安装

PHP >=8.1

建议通过运行以下命令使用composer进行安装:

composer require dominservice/onix-parser

用法

要解析XML格式的ONIX消息,请实例化一个新的解析器。然后将XML文件传递给解析器。结果是完整的ONIX消息,现在可以进行处理。

$parser = new \Dso\Onix\Parser();

/** @var Dso\Onix\Message\Message; */
$message = $parser->parseString(
    file_get_contents('sample.xml')
);

/** @var Dso\Onix\Product\Product[] */
$products = $message->getProducts();

代码列表

库包含所有当前代码列表在第61期。在ONIX文件中指定的代码将被自动读取,因此您将收到可读的代码版本。

假设,您的消息中有一个产品显示特定的NotificationType

<ONIXMessage>
    <Product>
        <NotificationType>03</NotificationType>
        [...]
    </Product>
</ONIXMessage>
// Either get the Code object and read it's code and/or value
$type = $product->getNotificationType();
$code = $code->getCode();   // "03"
$value = $code->getValue(); // "Notification confirmed on publication"

// or directly get the value as string
$value = (string) $product->getNotificationType();

多语言代码列表

根据ONIX格式的官方发布者网站,代码列表提供多种语言。目前支持以下语言

请注意,代码列表是从EDITEUR网站自动抓取的。因此,个别语言的翻译可能不正确或部分缺失。请随时提交包含您修正语言代码列表的pull request。

要使用特定语言,只需在解析器构造函数中将语言代码作为参数传递

// Create parser using german code list values
$parser = new \Dso\Onix\Parser('de');

测量

PHP ONIX解析器读取ONIX文件中提供的所有测量值,并分配正确的代码。使用它,您可以通过循环读取所有提供的测量值

// get the DescriptiveDetail portion of the product
$descriptiveDetail = $product->getDescriptiveDetail();

foreach ($descriptiveDetail->getMeasures() as $measure) {
	echo sprintf('%s: %s %s',                      // -> "Height: 210 Centimeters"
		(string) $measure->getMeasureType(),       // e.g. "Height"
		$measure->getMeasurement(),                // e.g. "210"
		(string) $measure->getMeasureUnitCode()    // e.g. "Centimeters"
	);
}

使用缩写函数,您还可以搜索高度、宽度、厚度或重量的测量值

/** @var Dso\Onix\Product\Measure */
$height = $descriptiveDetail->getHeight();
$width = $descriptiveDetail->getWidth();
$thickness = $descriptiveDetail->getThickness();
$weight = $descriptiveDetail->getWeight();

echo sprintf("Height: %s %s", $height->getMeasurement(), $height->getMeasureUnitCode()->getCode());
// --> Height: 210 cm

构建

此库使用Symfony Serializer的部分来解析XML文件并将它们转换为PHP对象。

待办事项

此库仍在开发中。以下是下一个版本中即将推出的一些功能

  • [.] 优化翻译
  • [.] 添加更多缩写函数(如 $product->getDescriptionText()
  • [.] 添加创建ONIX文件的写入器,从PHP对象创建

许可

此项目受MIT许可证的许可 - 有关详细信息,请参阅LICENSE文件