ribal/onix

ONIX 3.0 解析器

v1.0.2 2021-08-19 10:08 UTC

This package is auto-updated.

Last update: 2024-09-08 14:39:55 UTC


README

本包是一个简单的PHP库,用于读取ONIX 3.0文件(通用格式:Short和Ref)。各个单元中的辅助函数使您能够轻松地从数据记录中提取所需详情。不同格式的文本元素以及不同的ONIX日期格式都将自动解析,因此可以轻松使用。

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

安装

需要PHP 7.1或更高版本。

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

composer require ribal/onix

使用方法

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

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

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

/** @var Ribal\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 \Ribal\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 Ribal\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文件的写入器

许可证

本项目采用MIT许可证 - 有关详细信息,请参阅LICENSE文件