goetas / xsd2php
Requires
- php: >=5.5
- doctrine/inflector: ^1.0
- goetas-webservices/xsd-reader: ^0.1.2
- symfony/console: ^2.1|^3.0
- symfony/yaml: ^2.1|^3.0
- zendframework/zend-code: ~2.3
Requires (Dev)
- goetas-webservices/xsd2php-runtime: ^0.2.2
- jms/serializer: ^1.2
- phpunit/phpunit: ^4.8|^5.0
This package is auto-updated.
Last update: 2022-02-01 12:29:49 UTC
README
此存储库已废弃,请使用 goetas-webservices/xsd2php
将XSD转换为PHP类。
使用 goetas/xsd2php,您可以将任何XSD/WSDL定义转换为PHP类。
XSD2PHP还可以生成与JMS Serializer兼容的元数据,可用于序列化/反序列化对象实例.
安装
推荐通过 Composer 安装 xsd2php
- 将依赖项添加到您的
composer.json文件中
"require-dev": { .. "goetas/xsd2php":"^2.1", .. }
用法
通过此示例,我们将将OTA XSD定义转换为PHP类。
假设您在 /home/my/ota 中有所有XSD文件。
生成PHP类
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'
命名空间怎么办?
http://www.opentravel.org/OTA/2003/05将转换为Mercurio/OTA/2007BPHP命名空间
文件放在哪里?
Mercurio/OTA/2007B类将被放置在src/Mercurio/OTA/V2007B目录中
自定义类型怎么办?
--alias-map='http://www.opentravel.org/OTA/2003/05;CustomOTADateTimeFormat;Vendor/Project/CustomDateClass'将指示 XSD2PHP 不要在http://www.opentravel.org/OTA/2003/05命名空间内为CustomOTADateTimeFormat类型生成任何类。所有对此类型的引用都将替换为Vendor/Project/CustomDateClass类。
使用Composer脚本生成类
"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'" }
现在您可以使用 composer build 构建您的类。
序列化/反序列化
XSD2PHP还可以为您生成JMS Serializer元数据,您可以使用它来序列化/反序列化生成的PHP类实例。
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'
命名空间怎么办?
http://www.opentravel.org/OTA/2003/05将转换为Mercurio/OTA/2007BPHP命名空间
文件放在哪里?
http://www.opentravel.org/OTA/2003/05将放置在src/Metadata/JMS目录中
自定义类型怎么办?
-
--alias-map='http://www.opentravel.org/OTA/2003/05;CustomOTADateTimeFormat;Vendor/Project/CustomDateClass'将指示 XSD2PHP 不要在http://www.opentravel.org/OTA/2003/05命名空间内为CustomOTADateTimeFormat类型生成任何元数据信息。所有对此类型的引用都将替换为Vendor/Project/CustomDateClass类。您必须为此类型提供一个 自定义序列化器。 -
添加xsd2php依赖项以满足BaseTypesHandler和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');
处理 xsd:anyType 或 xsd:anySimpleType
如果您的XSD包含 xsd:anyType 或 xsd:anySimpleType 类型,您必须指定一个处理器。
在生成JMS元数据时,您必须指定一个自定义处理器
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' \
现在您必须创建一个自定义序列化处理器
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 } }
命名策略
命名策略有两种类型:短 和 长。默认为 短,但这种命名策略可能会产生命名冲突。
长 命名策略将在元素后缀为 Element,类型后缀为 Type。
MyNamespace\User将变为MyNamespace\UserElementMyNamespace\UserType将变为MyNamespace\UserTypeType
例如,一个具有名为 User 的类型、名为 UserType 的类型、根元素名为 User 和 UserElement 的 XSD,只有在使用 长 命名策略时才能正常工作。
- 如果没有命名冲突且想要有简短且描述性的类名,请使用
--naming-strategy=short选项。 - 如果有命名冲突,请使用
--naming-strategy=long选项。 - 为了安全起见,请使用
--naming-strategy=long选项。
注意
本项目的代码在 MIT 许可下提供。如需专业支持,请联系 goetas@gmail.com 或访问 https://www.goetas.com