ribal/onix
Composer 安装命令:
composer require ribal/onix
包简介
An ONIX 3.0 Parser
README 文档
README
This package is a simple PHP library for reading ONIX 3.0 files in the general formats Short and Ref. Helper functions in the individual units make it easy to read out the details that you need from the data record. Text elements in different formats as well as the different ONIX date formats are automatically parsed and can therefore be used easily.
This package is currently under development. Although most fields should be detected and parsed by the library, some fields still need more work.
Installation
PHP 7.1 or greater is required.
Installation is recommended to be done via composer by running:
composer require ribal/onix
Usage
To parse an ONIX message in XML format, a new parser is instantiated. The XML file is then passed to the parser. The result is the complete ONIX message that can now be processed.
$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();
Code Lists
The library contains all current code lists in issue 61. Codes that are specified in the ONIX file are automatically read out, so that you receive a readable version of the codes.
Let's say, you have a product in your message showing a specific 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();
Multi-Language Code Lists
According to the website of the official publisher of the ONIX format, the code lists are provided in several languages. The following languages are currently supported:
| Language | Language Code |
|---|---|
| English (default) | en |
| Spanish | es |
| German | de |
| French | fr |
| Italian | it |
| Norwegian | nb |
| Turkish | tr |
Please note that the code lists were automatically scraped from the EDITEUR website. Therefore, some translations into the individual languages can be incorrect or partly missing. Feel free to submit a pull request with your fixed language of the code lists.
To use a specific language, just pass the language code as argument in the parser constructor:
// Create parser using german code list values $parser = new \Ribal\Onix\Parser('de');
Measurements
The PHP ONIX Parser reads all of the given Measurements from the ONIX file and assigns the correct codes for them. Using that, you can either loop through all given Measurements:
// 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" ); }
Using shorthand functions you can also search for measurements of type height, width, thickness or weight:
/** @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
Built With
This library uses parts of the Symfony Serializer to parse the XML files and convert them into PHP objects.
To Do
This library is still work in progress. Here's what's coming in the next releases:
- [.] Optimize translations
- [.] Add more shorthand functions (like
$product->getDescriptionText()) - [.] Add a writer to create ONIX files from PHP objects
License
This project is licensed under the MIT License - see the LICENSE file for details
ribal/onix 适用场景与选型建议
ribal/onix 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.07k 次下载、GitHub Stars 达 25, 最近一次更新时间为 2020 年 04 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ribal/onix 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ribal/onix 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 4.07k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 25
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-04-10