2017 © Pedro Peláez
 

library xsd2php

Convert XSD (XML Schema) definitions into PHP classes and JMS metadata

image

madmages/xsd2php

Convert XSD (XML Schema) definitions into PHP classes and JMS metadata

  • Saturday, July 14, 2018
  • by madmages
  • Repository
  • 1 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

xsd2php

Convert XSD/WSDL into PHP classes., (*1)

XSD2PHP can also generate JMS Serializer compatible metadata that can be used to serialize/unserialize the object instances., (*2)

Fork of goetas-webservices/xsd2php, (*3)

Installation

There is one recommended way to install xsd2php via Composer:, (*4)

  • adding the dependency to your composer.json file:
  "require": {
      ..
      "goetas-webservices/xsd2php-runtime":"^0.2.2",
      ..
  },
  "require-dev": {
      ..
      "madmages/xsd2php":"^1.1",
      ..
  },

Usage example

use Madmages\Xsd\XsdToPhp\App;
use Madmages\Xsd\XsdToPhp\Config;

include 'vendor/autoload.php';

$config = (new Config)
    ->addNamespace('http://zakupki.gov.ru/223fz/types/1', 'ZGR\\Types', 'classes', 'jms')
    ->handleGeneratedClass(function ($class) {
        return $class;
    })
    ->handleGeneratedMethod(function ($method) {
        return $method;
    });

App::run(['xsd/Types.xsd'], $config);

Serialize / Unserialize

XSD2PHP can also generate for you JMS Serializer metadata that you can use to serialize/unserialize the generated PHP class instances., (*5)

The parameter aliases in the configuration file, will instruct XSD2PHP to not generate any metadata information or PHP class for the {http://www.example.org/test/}MyCustomXSDType type. All reference to this type are replaced with the MyCustomMappedPHPType name., (*6)

You have to provide a custom serializer for this type/alis., (*7)

Here is an example on how to configure JMS serializer to handle custom types, (*8)

use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\Handler\HandlerRegistryInterface;

use Madmages\Xsd\XsdToPhpRuntime\Jms\Handler\BaseTypesHandler;
use Madmages\Xsd\XsdToPhpRuntime\Jms\Handler\XmlSchemaDateHandler;

$serializerBuilder = SerializerBuilder::create();
$serializerBuilder->addMetadataDir('metadata dir', 'TestNs');
$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/>', 'TestNs\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., (*9)

When you generate the JMS metadata you have to specify a custom handler:, (*10)

use Madmages\Xsd\XsdToPhp\App;
use Madmages\Xsd\XsdToPhp\Config;

include 'vendor/autoload.php';

// aliases xsd_type => php_type
$aliases = [
    'anyType'       => 'MyCustomAnyTypeHandler'
    'anySimpleType' => 'MyCustomAnySimpleTypeHandler'
];

$config = (new Config)
        ->addNamespace('http://zakupki.gov.ru/223fz/types/1', 'ZGR\\Types', 'classes', 'jms', $aliases)

App::run(['xsd/Types.xsd'], $config);

Now you have to create a custom serialization handler:, (*11)

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., (*12)

The long naming strategy will suffix elements with Element and types with Type., (*13)

  • MyNamesapce\User will become MyNamesapce\UserElement
  • MyNamesapce\UserType will become MyNamesapce\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., (*14)

  • If you don't have naming conflicts and you want to have short and descriptive class names, use the short option.
  • If you have naming conflicts use the long option.
  • If you want to be safe, use the long option.

The Versions

14/07 2018

dev-second_wave

dev-second_wave

Convert XSD (XML Schema) definitions into PHP classes and JMS metadata

  Sources   Download

MIT

The Requires

 

The Development Requires

by Asmir Mustafic
by Aleksandr Kalin

php xml serializer converter xsd jms

19/06 2018

dev-master

9999999-dev

Convert XSD (XML Schema) definitions into PHP classes and JMS metadata

  Sources   Download

MIT

The Requires

 

The Development Requires

by Asmir Mustafic
by Aleksandr Kalin

php xml serializer converter xsd jms

19/06 2018

1.1.0

1.1.0.0

Convert XSD (XML Schema) definitions into PHP classes and JMS metadata

  Sources   Download

MIT

The Requires

 

The Development Requires

by Asmir Mustafic
by Aleksandr Kalin

php xml serializer converter xsd jms