Get support for goetas/xsd2php
If you're new to LTH, please see our FAQ for more information on what it is we do.
Support Options
Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:
View Open IssuesTake a look to see if anyone else has experienced the same issue as you and if they managed to solve it.
Open an IssueMake sure to read any relevant guidelines for opening issues on this repo before posting a new issue.
Sponsor directlyCheck out the page and see if there are any options to sponsor this project or it's developers directly.
goetas/xsd2php
xsd2php
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.json
file:
"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/05
will be converted intoMercurio/OTA/2007B
PHP namespace
Where place the files?
-
Mercurio/OTA/2007B
classes will be placed intosrc/Mercurio/OTA/V2007B
directory
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 forCustomOTADateTimeFormat
type inside thehttp://www.opentravel.org/OTA/2003/05
namespace. All reference to this type are replaced with theVendor/Project/CustomDateClass
class.
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/05
will be converted intoMercurio/OTA/2007B
PHP namespace
Where place the files?
-
http://www.opentravel.org/OTA/2003/05
will be placed intosrc/Metadata/JMS
directory
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 forCustomOTADateTimeFormat
type inside thehttp://www.opentravel.org/OTA/2003/05
namespace. All reference to this type are replaced with theVendor/Project/CustomDateClass
class. 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\User
will becomeMyNamespace\UserElement
-
MyNamespace\UserType
will 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=short
option. - If you have naming conflicts use the
--naming-strategy=long
option. - If you want to be safe, use the
--naming-strategy=long
option.
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
Our Mission
We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.
From the Blog
Interesting Articles
-
Generating income from open source
Jun 23 • 8 min read
-
2023 State of OSS
Apr 23 • 45 min read ★
-
A funding experiment...
Aug 19 • 10 min read
-
But You Said I could
Aug 19 • 2 min read
Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated
p-e622a1a2