• This repository has been archived on 07/Jan/2023
  • Stars
    star
    149
  • Rank 248,619 (Top 5 %)
  • Language
    PHP
  • Created about 12 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Convert XSD into PHP classes and serialize into XML (deserialize too)

xsd2php

This repository is DEPRECATED, please use goetas-webservices/xsd2php

Build Status Code Coverage Scrutinizer Code Quality

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 into Mercurio/OTA/2007B PHP namespace

Where place the files?

  • Mercurio/OTA/2007B classes will be placed into src/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 for CustomOTADateTimeFormat type inside the http://www.opentravel.org/OTA/2003/05 namespace. All reference to this type are replaced with the Vendor/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 into Mercurio/OTA/2007B PHP namespace

Where place the files?

  • http://www.opentravel.org/OTA/2003/05 will be placed into src/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 for CustomOTADateTimeFormat type inside the http://www.opentravel.org/OTA/2003/05 namespace. All reference to this type are replaced with the Vendor/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 become MyNamespace\UserElement
  • MyNamespace\UserType will become MyNamespace\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 [email protected] or visit https://www.goetas.com

More Repositories

1

twital

Twital is a "plugin" for Twig that adds some sugar syntax, which makes its templates similar to PHPTal or VueJS.
PHP
127
star
2

webservices

Pure PHP SOAP server and client
PHP
22
star
3

MultipartUploadBundle

Symfony multipart/related/mixed/alternative content type handler (rfc1341).
PHP
22
star
4

twital-bundle

Twital integration for Symfony
PHP
19
star
5

to-swift-mime-parser

Parse a generic mail stream, and convert it to a SwiftMailer Message http://swiftmailer.org/
PHP
12
star
6

messenger-doctrine-outbox

Transactional outbox implementation for Symfony messenger (https://microservices.io/patterns/data/transactional-outbox.html)
PHP
11
star
7

xsd-reader

Read XSD into PHP (see http://goetas.github.io/xsd-reader/)
PHP
9
star
8

apache-fop

Symfony2 Apache FOP Bundle
PHP
7
star
9

xml-xsd-encoder

Convert PHP to XML and viceversa using XML Schema definition as encoding style
PHP
5
star
10

jms-serializer-phpstan-extension

JMS/Serializer extension for PHPStan
PHP
3
star
11

doctrine2xsd

Convert Doctrine2 XML mapping into XSD XML Schema
PHP
2
star
12

TemplatedUriRouter

Symfony RFC-6570 compatible router and URL Generator
PHP
2
star
13

fit-tracker

Demo application with Symfony 2.8, API, AngularJS, REST, PHPUnit
JavaScript
1
star
14

xmldom

PHP DOMDocument improved
PHP
1
star
15

docker-nginx-redirect

Handle HTTP redirects with this simple docker image
Dockerfile
1
star
16

atal

ATal is a templating engine for PHP 5.3+. Atal is inspired to PHPTAL but solves the problem in a more modern way.
PHP
1
star
17

fo-simil-css

Simil CSS for XSL FO
PHP
1
star
18

composer-resolve

1
star
19

xhtml-clsss-to-style

Convert CSS rules into local styles (HTML)
PHP
1
star
20

wsdl-reader

PHP
1
star
21

symfony-mem-leak

https://travis-ci.org/goetas/symfony-mem-leak
PHP
1
star
22

goetas-xsd-reader

PHP
1
star
23

xsd2php-symfony

PHP
1
star
24

TemplatedUriBundle

Symfony Bundle that provides a RFC-6570 compatible router and URL Generator.
PHP
1
star