kjantzer / ponipar
基于流的PHP ONIX解析库。
0.2.0
2018-10-19 15:03 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-14 18:58:24 UTC
README
主要功能
- 基于流:可以读取任意长度的ONIX文件,因为它不会将整个文件保存在内存中
- 便捷的输入方式:可以从文件、URL、流、stdin或字符串传递您的数据
- 基于回调:您为感兴趣的ONIX文件的每个方面定义一个可调用的(函数、方法、闭包),然后在PONIpar解析时调用它(目前,仅实现了Product回调)
- 通用:内部使用引用名称,但也读取(和转换)短标签(目前不完整,目前仅完全支持引用名称)
- 高级:PONIpar处理XML解析事件,并为您的回调提供已解析的高级对象实例,如“Product”和“Contributor”(尚未完成)
- 现代:命名空间PHP 5.3代码
- 灵活:由于PONIpar不强迫您以某种特定方式处理数据,您可以自由地编写最佳满足您需求的方式
- 国际化:将每个输入字符集转换为UTF-8,因此只提供UTF-8字符串(尚未实现)
当前状态
PONIpar正在部分开发中(足够用于基本使用)。它可以识别<Product>
元素,并为每个找到的元素调用用户定义的回调,传递当前允许通过标准DOM调用访问产品数据的、高级的Product
对象,以及一或两个高级便捷类和方法。第一个高级类(用于ProductIdentifiers)已经存在。
其他高级类已经构建,但仍在开发中,可能需要改进。您可以通过查看ProductSubitem目录来查看哪些类可用。
您可以在生产环境中使用它,但您将想要运行测试以确保所需的数据被正确解析。一些<Product>
属性将需要手动检索。
待办事项
- 添加更多
ProductSubitems
示例用法
在文件顶部设置我们打算引用的类。我们这样做是为了可以使用更短的类名。
use PONIpar\Parser; use PONIpar\ProductSubitem\ProductIdentifier; use PONIpar\ProductSubitem\Title; use PONIpar\ProductSubitem\Contributor; use PONIpar\ProductSubitem\Extent; use PONIpar\ProductSubitem\SupplyDetail;
创建一个函数来处理获取每个<Product>
的数据
$parse_product = function($product){ $isbn_13 = $product->getIdentifier(ProductIdentifier::TYPE_ISBN13); // there can be multiple titles $titles = $product->getTitles(); $main_title = ''; // find the main title foreach ($titles as $item) { if( $item->getType() == Title::TYPE_DISTINCTIVE_TITLE ) $main_title = $item->getValue(); } // get list of contributor names $contributors = $product->getContributors(); $contributor_names = array_map(function($c){ return $c->getName(); }, $contributors); $bisac = $product->getMainSubjectBISAC(); $description = $product->getMainDescription(); $is_active = $product->isActive(); // get supply info $supply_details = $product->getSupplyDetails(); $supply_detail = $supply_details[0]; $supply_detail->getOnSaleDate(); $supply_detail->getPrices(); }
开始解析ONIX文件。上面的parse_product
函数将为每个<Product>
调用。
$parser = new Parser(); $parser->useFile($file); $parser->setProductHandler($parse_product); $parser->parse();
要求
PONIpar至少需要PHP 5.3和“XML解析器”扩展。
作者
PONIpar由UEBERBIT GmbH创作,并由Blackstone Publishing, Inc.进行额外开发。