goetas/xsd2php
最新稳定版本:2.1.0
Composer 安装命令:
composer require goetas/xsd2php
包简介
Convert XSD (XML Schema) definitions into PHP classes
关键字:
README 文档
README
This repository is DEPRECATED, please use goetas-webservices/xsd2php
Convert XSD into PHP classes.
With goetas/xsd2php you can convert any XSD/WSDL definition into PHP classes.
XSD2PHP can also generate JMS Serializer compatible metadata that can be used to serialize/unserialize the object instances.
Installation
There is one recommended way to install xsd2php via Composer:
- adding the dependency to your
composer.jsonfile:
"require-dev": { .. "goetas/xsd2php":"^2.1", .. }
Usage
With this example we will convert OTA XSD definitions into PHP classes.
Suppose that you have allo XSD files in /home/my/ota.
Generate PHP classes
vendor/bin/xsd2php convert:php \ `/home/my/ota/OTA_HotelAvail*.xsd \ --ns-map='http://www.opentravel.org/OTA/2003/05;Mercurio/OTA/2007B/' \ --ns-dest='Mercurio/OTA/2007B/;src/Mercurio/OTA/V2007B' \ --alias-map='http://www.opentravel.org/OTA/2003/05;CustomOTADateTimeFormat;Vendor/Project/CustomDateClass'
What about namespaces?
http://www.opentravel.org/OTA/2003/05will be converted intoMercurio/OTA/2007BPHP namespace
Where place the files?
Mercurio/OTA/2007Bclasses will be placed intosrc/Mercurio/OTA/V2007Bdirectory
What about custom types?
--alias-map='http://www.opentravel.org/OTA/2003/05;CustomOTADateTimeFormat;Vendor/Project/CustomDateClass'will instruct XSD2PHP to not generate any class forCustomOTADateTimeFormattype inside thehttp://www.opentravel.org/OTA/2003/05namespace. All reference to this type are replaced with theVendor/Project/CustomDateClassclass.
Use composer scripts to generate classes
"scripts": { "build": "xsd2php convert:php '/home/my/ota/OTA_HotelAvail*.xsd' --ns-map='http://www.opentravel.org/OTA/2003/05;Mercurio/OTA/2007B/' --ns-dest='Mercurio/OTA/2007B/;src/Mercurio/OTA/V2007B'" }
Now you can build your classes with composer build.
Serialize / Unserialize
XSD2PHP can also generate for you JMS Serializer metadata that you can use to serialize/unserialize the generated PHP class instances.
vendor/bin/xsd2php convert:jms-yaml \ `/home/my/ota/OTA_HotelAvail*.xsd \ --ns-map='http://www.opentravel.org/OTA/2003/05;Mercurio/OTA/2007B/' \ --ns-dest='Mercurio/OTA/2007B/;src/Metadata/JMS;' \ --alias-map='http://www.opentravel.org/OTA/2003/05;CustomOTADateTimeFormat;Vendor/Project/CustomDateClass'
What about namespaces?
http://www.opentravel.org/OTA/2003/05will be converted intoMercurio/OTA/2007BPHP namespace
Where place the files?
http://www.opentravel.org/OTA/2003/05will be placed intosrc/Metadata/JMSdirectory
What about custom types?
-
--alias-map='http://www.opentravel.org/OTA/2003/05;CustomOTADateTimeFormat;Vendor/Project/CustomDateClass'will instruct XSD2PHP to not generate any metadata information forCustomOTADateTimeFormattype inside thehttp://www.opentravel.org/OTA/2003/05namespace. All reference to this type are replaced with theVendor/Project/CustomDateClassclass. You have to provide a custom serializer for this type -
Add xsd2php dependency to satisfy BaseTypesHandler and XmlSchemaDateHandler.
"require" : { "goetas-webservices/xsd2php-runtime":"^0.2.2", }
use JMS\Serializer\SerializerBuilder; use JMS\Serializer\Handler\HandlerRegistryInterface; use GoetasWebservices\Xsd\XsdToPhpRuntime\Jms\Handler\BaseTypesHandler; use GoetasWebservices\Xsd\XsdToPhpRuntime\Jms\Handler\XmlSchemaDateHandler; $serializerBuilder = SerializerBuilder::create(); $serializerBuilder->addMetadataDir('metadata dir', 'DemoNs'); $serializerBuilder->configureHandlers(function (HandlerRegistryInterface $handler) use ($serializerBuilder) { $serializerBuilder->addDefaultHandlers(); $handler->registerSubscribingHandler(new BaseTypesHandler()); // XMLSchema List handling $handler->registerSubscribingHandler(new XmlSchemaDateHandler()); // XMLSchema date handling // $handler->registerSubscribingHandler(new YourhandlerHere()); }); $serializer = $serializerBuilder->build(); // deserialize the XML into Demo\MyObject object $object = $serializer->deserialize('<some xml/>', 'DemoNs\MyObject', 'xml'); // some code .... // serialize the Demo\MyObject back into XML $newXml = $serializer->serialize($object, 'xml');
Dealing with xsd:anyType or xsd:anySimpleType
If your XSD contains xsd:anyType or xsd:anySimpleType types you have to specify a handler for this.
When you generate the JMS metadata you have to specify a custom handler:
bin/xsd2php.php convert:jms-yaml \ ... various params ... \ --alias-map='http://www.w3.org/2001/XMLSchema;anyType;MyCustomAnyTypeHandler' \ --alias-map='http://www.w3.org/2001/XMLSchema;anyType;MyCustomAnySimpleTypeHandler' \
Now you have to create a custom serialization handler:
use JMS\Serializer\XmlSerializationVisitor; use JMS\Serializer\XmlDeserializationVisitor; use JMS\Serializer\Handler\SubscribingHandlerInterface; use JMS\Serializer\GraphNavigator; use JMS\Serializer\VisitorInterface; use JMS\Serializer\Context; class MyHandler implements SubscribingHandlerInterface { public static function getSubscribingMethods() { return array( array( 'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, 'format' => 'xml', 'type' => 'MyCustomAnyTypeHandler', 'method' => 'deserializeAnyType' ), array( 'direction' => GraphNavigator::DIRECTION_SERIALIZATION, 'format' => 'xml', 'type' => 'MyCustomAnyTypeHandler', 'method' => 'serializeAnyType' ) ); } public function serializeAnyType(XmlSerializationVisitor $visitor, $data, array $type, Context $context) { // serialize your object here } public function deserializeAnyType(XmlDeserializationVisitor $visitor, $data, array $type) { // deserialize your object here } }
Naming Strategy
There are two types of naming strategies: short and long. The default is short, this naming strategy can however generate naming conflicts.
The long naming strategy will suffix elements with Element and types with Type.
MyNamespace\Userwill becomeMyNamespace\UserElementMyNamespace\UserTypewill becomeMyNamespace\UserTypeType
An XSD for instance with a type named User, a type named UserType, a root element named User and UserElement, will only work when using the long naming strategy.
- If you don't have naming conflicts and you want to have short and descriptive class names, use the
--naming-strategy=shortoption. - If you have naming conflicts use the
--naming-strategy=longoption. - If you want to be safe, use the
--naming-strategy=longoption.
Note
The code in this project is provided under the MIT license. For professional support contact goetas@gmail.com or visit https://www.goetas.com
goetas/xsd2php 适用场景与选型建议
goetas/xsd2php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 337.68k 次下载、GitHub Stars 达 151, 最近一次更新时间为 2012 年 09 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「xml」 「jms」 「serializer」 「converter」 「xsd」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 goetas/xsd2php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 goetas/xsd2php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 goetas/xsd2php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Bridge between JMS Serializer Bundle and Superdesk Web Publisher.
This bridge allows you to use jms payment plugins but in payum like way.
Load DOM document safety
MongoDB support for JMSPaymentCoreBundle
Promoting the interoperability of MQs objects. Based on Java JMS, modernized with psalm typehints.
A nice answer factory for Laravel based on the jms/serializer
统计信息
- 总下载量: 337.68k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 153
- 点击次数: 26
- 依赖项目数: 11
- 推荐数: 0
其他信息
- 授权协议: LGPL
- 更新时间: 2012-09-13